lib/rufus/scheduler.rb in rufus-scheduler-1.0.1 vs lib/rufus/scheduler.rb in rufus-scheduler-1.0.2

- old
+ new

@@ -43,12 +43,30 @@ # # schedule_at() and schedule() await either a Schedulable instance and # params (usually an array or nil), either a block, which is more in the # Ruby way. # + # == The gem "openwferu-scheduler" + # + # This scheduler was previously known as the "openwferu-scheduler" gem. + # + # To ensure that code tapping the previous gem still runs fine with + # "rufus-scheduler", this new gem has 'pointers' for the old class + # names. + # + # require 'rubygems' + # require 'openwfe/util/scheduler' + # s = OpenWFE::Scheduler.new + # + # will still run OK with "rufus-scheduler". + # # == Examples + # + # require 'rubygems' + # require 'rufus/scheduler' # + # # scheduler.schedule_in("3d") do # regenerate_monthly_report() # end # # # # will call the regenerate_monthly_report method @@ -197,10 +215,23 @@ # $mail = $inbox.fetch_last_mail # # params[:dont_reschedule] = true if $mail # end # + # == 'Every jobs', :first_at and :first_in + # + # Since rufus-scheduler 1.0.2, the schedule_every methods recognizes two + # optional parameters, :first_at and :first_in + # + # scheduler.schedule_every "2d", :first_in => "5h" do + # # schedule something every two days, start in 5 hours... + # end + # + # scheduler.schedule_every "2d", :first_at => "5h" do + # # schedule something every two days, start in 5 hours... + # end + # class Scheduler # # By default, the precision is 0.250, with means the scheduler # will check for jobs to execute 4 times per second. @@ -373,21 +404,36 @@ # scheduler.schedule_every "500", :try_again => false do # do_some_prone_to_error_stuff() # # won't get rescheduled in case of exception # end # + # Since rufus-scheduler 1.0.2, the params :first_at and :first_in are + # accepted. + # + # scheduler.schedule_every "2d", :first_in => "5h" do + # # schedule something every two days, start in 5 hours... + # end + # def schedule_every (freq, params={}, &block) f = duration_to_f freq params = prepare_params params schedulable = params[:schedulable] params[:every] = freq - last_at = params[:last_at] - next_at = if last_at - last_at + f + first_at = params.delete :first_at + first_in = params.delete :first_in + + previous_at = params[:previous_at] + + next_at = if first_at + first_at + elsif first_in + Time.now.to_f + duration_to_f(first_in) + elsif previous_at + previous_at + f else Time.now.to_f + f end do_schedule_at(next_at, params) do |job_id, at| @@ -421,11 +467,11 @@ # # ok, reschedule ... params[:job_id] = job_id - params[:last_at] = at + params[:previous_at] = at schedule_every params[:every], params, &block # # yes, this is a kind of recursion @@ -660,32 +706,26 @@ # def do_schedule_at (at, params={}, &block) #puts "0 at is '#{at.to_s}' (#{at.class})" - at = Rufus::to_ruby_time(at) \ - if at.kind_of?(String) + at = at_to_f at - at = Rufus::to_gm_time(at) \ - if at.kind_of?(DateTime) - - at = at.to_f \ - if at.kind_of?(Time) - #puts "1 at is '#{at.to_s}' (#{at.class})"}" jobClass = params[:every] ? EveryJob : AtJob job_id = params[:job_id] - b = to_block(params, &block) + b = to_block params, &block - job = jobClass.new(self, at, job_id, params, &b) + job = jobClass.new self, at, job_id, params, &b #do_unschedule(job_id) if job_id if at < (Time.new.to_f + @precision) + job.trigger() unless params[:discard_past] return nil end @schedule_queue << job @@ -703,9 +743,21 @@ def duration_to_f (s) return s if s.kind_of?(Float) return Rufus::parse_time_string(s) if s.kind_of?(String) Float(s.to_s) + end + + # + # Ensures an 'at' instance is translated to a float + # (to be compared with the float coming from time.to_f) + # + def at_to_f (at) + + at = Rufus::to_ruby_time(at) if at.kind_of?(String) + at = Rufus::to_gm_time(at) if at.kind_of?(DateTime) + at = at.to_f if at.kind_of?(Time) + at end # # Returns a block. If a block is passed, will return it, else, # if a :schedulable is set in the params, will return a block