lib/openwfe/util/scheduler.rb in openwferu-0.9.4 vs lib/openwfe/util/scheduler.rb in openwferu-0.9.5

- old
+ new

@@ -39,13 +39,11 @@ # John Mettraux at openwfe.org # require 'monitor' -#require 'openwfe/utils' require 'openwfe/util/otime' -require 'openwfe/util/stoppable' module OpenWFE # @@ -86,11 +84,11 @@ # scheduler.schedule_in("4d", r, :monthly) # # # # will regenerate the monthly report in four days # class Scheduler - include MonitorMixin, Stoppable + include MonitorMixin attr_accessor \ :precision def initialize @@ -105,40 +103,39 @@ @precision = 0.250 # # every 250ms, the scheduler wakes up @last_cron_minute = -1 + + @stopped = false end # # Starts this scheduler (or restart it if it was previously stopped) # - def start + def sstart - #if @scheduler_thread - # @scheduler_thread.wakeup - # return - #end - @scheduler_thread = Thread.new do while true - break if self.is_stopped? - #print "." - #$stdout.flush + break if @stopped + #print "."; $stdout.flush step sleep(@precision) end end - - do_restart end # - # The scheduler is stoppable via stop() or do_stop() + # The scheduler is stoppable via sstop() # - alias :stop :do_stop + def sstop + @stopped = true + end + alias :start :sstart + alias :stop :sstop + # # Joins on the scheduler thread # def join @scheduler_thread.join @@ -169,11 +166,11 @@ if at < (Time.new.to_f + @precision) job.trigger() return nil end - + return push(job) \ if @pending_jobs.length < 1 # shortcut : check if the new job is posterior to # the last job pending @@ -260,12 +257,12 @@ # # outputs a message every weekday at 10pm # # Returns the job id attributed to this 'cron job', this id can # be used to unschedule the job. # - def schedule \ - (cron_line, cron_id=nil, schedulable=nil, params=nil, &block) + def schedule ( + cron_line, cron_id=nil, schedulable=nil, params=nil, &block) synchronize do # # is a job with the same id already scheduled ? @@ -366,28 +363,21 @@ minute = now.min # # cron entries - begin - if now.sec == 0 and minute > @last_cron_minute - # - # only consider cron entries at the second 0 of a - # minute + if now.sec == 0 and minute > @last_cron_minute + # + # only consider cron entries at the second 0 of a + # minute - @last_cron_minute = minute + @last_cron_minute = minute - @cron_entries.each do |cron_id, cron_entry| - #puts "step() cron_id : #{cron_id}" - cron_entry.trigger \ - if cron_entry.matches? now - end + @cron_entries.each do |cron_id, cron_entry| + #puts "step() cron_id : #{cron_id}" + trigger(cron_entry) if cron_entry.matches? now end - rescue Exception => e - #puts \ - # "step() caught exception\n" + - # OpenWFE::exception_to_s(e) end # # pending jobs @@ -410,14 +400,32 @@ #if job.at <= now # # obviously - job.trigger() + trigger(job) + @pending_jobs.delete_at(0) end end - end + end + + def trigger (entry) + Thread.new do + begin + entry.trigger + rescue Exception => e + message = + "trigger() caught exception\n" + + OpenWFE::exception_to_s(e) + if self.respond_to? :lwarn + lwarn { message } + else + puts message + end + end + end + end end # # This module adds a trigger method to any class that includes it. # The default implementation feature here triggers an exception.