lib/rufus/scheduler.rb in rufus-scheduler-1.0.4 vs lib/rufus/scheduler.rb in rufus-scheduler-1.0.5

- old
+ new

@@ -234,13 +234,30 @@ # # By default, the precision is 0.250, with means the scheduler # will check for jobs to execute 4 times per second. # - attr_accessor :precision + attr_reader :precision # + # Setting the precision ( 0.0 < p <= 1.0 ) + # + def precision= (f) + + raise "precision must be 0.0 < p <= 1.0" \ + if f <= 0.0 or f > 1.0 + + @precision = f + end + + #-- + # Set by default at 0.00045, it's meant to minimize drift + # + #attr_accessor :correction + #++ + + # # As its name implies. # attr_accessor :stopped @@ -260,15 +277,17 @@ @scheduler_thread = nil @precision = 0.250 # every 250ms, the scheduler wakes up (default value) begin - @precision = Float(params[:scheduler_precision]) + self.precision = Float(params[:scheduler_precision]) rescue Exception => e # let precision at its default value end + #@correction = 0.00045 + @exit_when_no_more_jobs = false @dont_reschedule_every = false @last_cron_second = -1 @@ -294,14 +313,19 @@ loop do break if @stopped + t0 = Time.now.to_f + step - sleep @precision - # TODO : adjust precision + d = Time.now.to_f - t0 # + @correction + + next if d > @precision + + sleep (@precision - d) end end end # @@ -663,11 +687,11 @@ # # Returns true if the given string seems to be a cron string. # def Scheduler.is_cron_string (s) - s.match(".+ .+ .+ .+ .+") + s.match ".+ .+ .+ .+ .+" end #protected private @@ -884,11 +908,11 @@ now = Time.new if @exit_when_no_more_jobs - if @pending_jobs.size < 1 + if @pending_jobs.size < 1 @stopped = true return end @@ -909,10 +933,11 @@ #puts "step() @cron_jobs.size #{@cron_jobs.size}" @cron_jobs.each do |cron_id, cron_job| #puts "step() cron_id : #{cron_id}" + #trigger(cron_job) if cron_job.matches?(now, @precision) trigger(cron_job) if cron_job.matches?(now) end end # @@ -1164,12 +1189,14 @@ # # This is the method called by the scheduler to determine if it # has to fire this CronJob instance. # def matches? (time) + #def matches? (time, precision) - @cron_line.matches? time + #@cron_line.matches?(time, precision) + @cron_line.matches?(time) end # # As the name implies. # @@ -1238,21 +1265,33 @@ end # # Returns true if the given time matches this cron line. # + # (the precision is passed as well to determine if it's + # worth checking seconds and minutes) + # def matches? (time) + #def matches? (time, precision) time = Time.at(time) \ if time.kind_of?(Float) or time.kind_of?(Integer) - return false if no_match?(time.sec, @seconds) - return false if no_match?(time.min, @minutes) - return false if no_match?(time.hour, @hours) - return false if no_match?(time.day, @days) - return false if no_match?(time.month, @months) - return false if no_match?(time.wday, @weekdays) + return false \ + if no_match?(time.sec, @seconds) + #if precision <= 1 and no_match?(time.sec, @seconds) + return false \ + if no_match?(time.min, @minutes) + #if precision <= 60 and no_match?(time.min, @minutes) + return false \ + if no_match?(time.hour, @hours) + return false \ + if no_match?(time.day, @days) + return false \ + if no_match?(time.month, @months) + return false \ + if no_match?(time.wday, @weekdays) true end # @@ -1379,13 +1418,13 @@ def no_match? (value, cron_values) return false if not cron_values cron_values.each do |v| - return false if value == v + return false if value == v # ok, it matches end - true + true # no match found end end end