module LoopDance::Commands
Attributes
run[RW]
timeout[R]
Public Class Methods
super_attr_accessor(*syms)
click to toggle source
# File lib/loop_dance/commands.rb, line 5 def self.super_attr_accessor(*syms) attr_writer *syms syms.each do |sym| next if sym.is_a?(Hash) class_eval "def #{sym} value = (nil;flag=true); if flag; return @#{sym}; else; @#{sym} = value; end ; end" end end
Public Instance Methods
dance()
click to toggle source
# File lib/loop_dance/commands.rb, line 31 def dance loop_init if @tasks.empty? log "No tasks defined.", true else while (@run) do @tasks.each_with_index do |task, index| break unless @run if task.time_to_run? log "Run task ##{index} for every #{task.interval.inspect}" task.run end break unless @run end log "Sleep for #{@timeout.inspect}" sleep @timeout.to_i if @run end end log "shutting down", true end
every( interval, &block )
click to toggle source
# File lib/loop_dance/commands.rb, line 24 def every( interval, &block ) @tasks = [] unless @tasks @tasks << LoopDance::Task.new( self, interval, &block ) find_minimal_timeout interval @maximal_timeout = interval if !@maximal_timeout || @maximal_timeout < interval end
inherited(subclass)
click to toggle source
# File lib/loop_dance/commands.rb, line 20 def inherited(subclass) subclass.trap_signals = true end
log(text, forced=false)
click to toggle source
# File lib/loop_dance/commands.rb, line 60 def log(text, forced=false) puts "#{Time.now} #{self}: #{text}" if forced || !mute_log end
pid_file()
click to toggle source
# File lib/loop_dance/commands.rb, line 56 def pid_file "log/#{name.underscore}.pid" end
print_status()
click to toggle source
# File lib/loop_dance/commands.rb, line 64 def print_status puts "#{self}: timeout: #{@timeout.inspect}, status: #{self.controller.running? ? 'running' : 'stopped'}\n" if tasks.empty? puts " no tasks defined" else tasks.each_with_index do |task,index| puts " Task ##{index} runs every #{task.interval.inspect}" end end end
stop_me()
click to toggle source
# File lib/loop_dance/commands.rb, line 52 def stop_me @run = false end
Private Instance Methods
do_trap_signals()
click to toggle source
# File lib/loop_dance/commands.rb, line 96 def do_trap_signals log "Trap signals" sigtrap = proc { log "caught trapped signal, shutting down", true @run = false } ["SIGTERM", "SIGINT", "SIGHUP"].each do |signal| trap signal, sigtrap end end
find_minimal_timeout( interval )
click to toggle source
# File lib/loop_dance/commands.rb, line 79 def find_minimal_timeout( interval ) return @timeout = interval if @timeout.blank? minimal = interval < @timeout ? interval : @timeout while not timeout_devides? minimal minimal-=1 end @timeout = minimal end
loop_init()
click to toggle source
# File lib/loop_dance/commands.rb, line 107 def loop_init # we don't want to delay output to sdtout until the program stops, we want feedback! $stdout.sync=true write_pid do_trap_signals if trap_signals @run = true log "Process started and sleep for #{@timeout.inspect}. kill #{Process.pid} to stop", true end
timeout_devides?( timeout )
click to toggle source
# File lib/loop_dance/commands.rb, line 88 def timeout_devides?( timeout ) @tasks.each { |task| m = (task.interval / timeout).to_i return false unless m * timeout == task.interval } return true end
write_pid()
click to toggle source
# File lib/loop_dance/commands.rb, line 116 def write_pid file = File.new( pid_file, "w" ) file.print Process.pid.to_s file.close end