Add an option to log a message every day to be sure it is alive
This commit is contained in:
parent
fb7be672fa
commit
c5b5895e80
@ -1,10 +1,11 @@
|
|||||||
require 'biju'
|
require 'biju'
|
||||||
require 'logger'
|
require 'logger'
|
||||||
|
require 'date'
|
||||||
|
|
||||||
module SMSd
|
module SMSd
|
||||||
class CLI
|
class CLI
|
||||||
attr_accessor :machine
|
attr_accessor :machine, :last_pong
|
||||||
attr_reader :modem, :options, :logger
|
attr_reader :modem, :options, :logger, :running_since
|
||||||
|
|
||||||
def initialize(args = [], &block)
|
def initialize(args = [], &block)
|
||||||
@options = Options.parse(args)
|
@options = Options.parse(args)
|
||||||
@ -19,10 +20,14 @@ module SMSd
|
|||||||
end
|
end
|
||||||
|
|
||||||
def run
|
def run
|
||||||
|
@running_since = DateTime.now
|
||||||
|
|
||||||
catch_signals
|
catch_signals
|
||||||
Process.daemon if options[:daemonize]
|
Process.daemon if options[:daemonize]
|
||||||
|
|
||||||
loop do
|
loop do
|
||||||
|
pong if options[:alive]
|
||||||
|
|
||||||
sleep 5
|
sleep 5
|
||||||
break if @terminate
|
break if @terminate
|
||||||
|
|
||||||
@ -111,5 +116,14 @@ module SMSd
|
|||||||
|
|
||||||
logger.info "#{sms}: #{log}"
|
logger.info "#{sms}: #{log}"
|
||||||
end
|
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
|
||||||
end
|
end
|
||||||
|
@ -12,6 +12,11 @@ module SMSd
|
|||||||
opts.separator ''
|
opts.separator ''
|
||||||
opts.separator 'Specific options:'
|
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',
|
opts.on('-d', '--[no-]daemonize',
|
||||||
'Run in the background') do |daemon|
|
'Run in the background') do |daemon|
|
||||||
options[:daemonize] = daemon
|
options[:daemonize] = daemon
|
||||||
|
Loading…
x
Reference in New Issue
Block a user