Add an option to log a message every day to be sure it is alive

This commit is contained in:
Guillaume DOTT 2013-11-26 17:00:15 +01:00
parent fb7be672fa
commit c5b5895e80
2 changed files with 21 additions and 2 deletions

View File

@ -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

View File

@ -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