From c5b5895e807da92e86e6ef3ded15c3d679c399d3 Mon Sep 17 00:00:00 2001 From: Guillaume DOTT Date: Tue, 26 Nov 2013 17:00:15 +0100 Subject: [PATCH] Add an option to log a message every day to be sure it is alive --- lib/smsd/cli.rb | 18 ++++++++++++++++-- lib/smsd/cli/options.rb | 5 +++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/lib/smsd/cli.rb b/lib/smsd/cli.rb index 0ffd7d0..b1dc811 100644 --- a/lib/smsd/cli.rb +++ b/lib/smsd/cli.rb @@ -1,10 +1,11 @@ require 'biju' require 'logger' +require 'date' module SMSd class CLI - attr_accessor :machine - attr_reader :modem, :options, :logger + attr_accessor :machine, :last_pong + attr_reader :modem, :options, :logger, :running_since def initialize(args = [], &block) @options = Options.parse(args) @@ -19,10 +20,14 @@ module SMSd end def run + @running_since = DateTime.now + catch_signals Process.daemon if options[:daemonize] loop do + pong if options[:alive] + sleep 5 break if @terminate @@ -111,5 +116,14 @@ module SMSd logger.info "#{sms}: #{log}" end + + def pong + if last_pong.nil? || last_pong < (DateTime.now - 1) + now = DateTime.now + + logger.info "I have been alive for #{(now - running_since).to_f.round} days (#{running_since})." + self.last_pong = now + end + end end end diff --git a/lib/smsd/cli/options.rb b/lib/smsd/cli/options.rb index 0e18041..2feca5f 100644 --- a/lib/smsd/cli/options.rb +++ b/lib/smsd/cli/options.rb @@ -12,6 +12,11 @@ module SMSd opts.separator '' opts.separator 'Specific options:' + opts.on('-a', '--[no-]alive', + 'Send a message to the logger every day') do |alive| + options[:alive] = alive + end + opts.on('-d', '--[no-]daemonize', 'Run in the background') do |daemon| options[:daemonize] = daemon