lib/openwfe/util/scheduler.rb in openwferu-0.9.5 vs lib/openwfe/util/scheduler.rb in openwferu-0.9.6
- old
+ new
@@ -192,21 +192,43 @@
# Schedules a job by stating in how much time it should trigger.
# Returns the a job_id that can be used to unschedule the job.
#
def schedule_in (duration, schedulable=nil, params=nil, &block)
- if duration.kind_of?(String)
- duration = OpenWFE::parse_time_string(duration)
- elsif not duration.kind_of?(Float)
- duration = Float(duration.to_s)
- end
+ duration = duration_to_f(duration)
return schedule_at(
Time.new.to_f + duration, schedulable, params, &block)
end
#
+ # Schedules a job in a loop. After an execution, it will not execute
+ # before the time specified in 'freq'.
+ #
+ # Note that if your job takes 2s to execute and the freq is set to
+ # 10s, it will in fact execute every 12s.
+ #
+ def schedule_every (freq, schedulable=nil, params=nil, &block)
+
+ f = duration_to_f(freq)
+
+ job_id = schedule_in(f) do |eid, at|
+ if schedulable
+ schedulable.trigger(params)
+ else
+ block.call eid, at
+ end
+ schedule_every(f, schedulable, params, &block)
+ end
+
+ schedulable.job_id = job_id \
+ if schedulable and schedulable.respond_to? :job_id=
+
+ job_id
+ end
+
+ #
# Unschedules an 'at' or a 'cron' job identified by the id
# it was given at schedule time.
#
def unschedule (job_id)
synchronize do
@@ -315,10 +337,30 @@
#
def cron_job_count
@cron_entries.size
end
+ #
+ # Returns true if the given string seems to be a cron string.
+ #
+ def Scheduler.is_cron_string (s)
+ return s.match(".+ .+ .+ .+ .+")
+ end
+
protected
+
+ #
+ # Ensures that a duration is a expressed as a Float instance.
+ #
+ # duration_to_f("10s")
+ #
+ # will yields 10.0
+ #
+ def duration_to_f (s)
+ return s if s.kind_of? Float
+ return OpenWFE::parse_time_string(s) if s.kind_of? String
+ return Float(s.to_s)
+ end
def to_block (schedulable, params, &block)
if schedulable
lambda do
schedulable.trigger(params)