lib/openwfe/util/scheduler.rb in openwferu-0.9.6 vs lib/openwfe/util/scheduler.rb in openwferu-0.9.7
- old
+ new
@@ -79,14 +79,20 @@
# end
# end
#
# regenerator = Regenerator.new
#
- # scheduler.schedule_in("4d", r, :monthly)
+ # scheduler.schedule_in("4d", regenerator, :monthly)
# #
# # will regenerate the monthly report in four days
#
+ # There is also schedule_every() :
+ #
+ # scheduler.schedule_every("1h20m") do
+ # regenerate_latest_report()
+ # end
+ #
class Scheduler
include MonitorMixin
attr_accessor \
:precision
@@ -144,10 +150,18 @@
#
# Schedules a job by specifying at which time it should trigger.
# Returns the a job_id that can be used to unschedule the job.
#
def schedule_at (at, schedulable=nil, params=nil, &block)
+
+ schedule_at_with_id(at, nil, schedulable, params, &block)
+ end
+
+ #
+ # Do not call directly.
+ #
+ def schedule_at_with_id (at, at_id, schedulable=nil, params=nil, &block)
synchronize do
#puts "0 at is '#{at.to_s}' (#{at.class})"
at = OpenWFE::to_ruby_time(at) \
@@ -160,11 +174,11 @@
if at.kind_of? Time
#puts "1 at is '#{at.to_s}' (#{at.class})"}"
b = to_block(schedulable, params, &block)
- job = AtEntry.new(at, &b)
+ job = AtEntry.new(at, at_id, &b)
if at < (Time.new.to_f + @precision)
job.trigger()
return nil
end
@@ -185,10 +199,11 @@
end
return push(job)
end
end
+ protected :schedule_at_with_id
#
# 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.
#
@@ -204,29 +219,42 @@
# 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.
+ # You can however wrap the code within its own thread :
#
+ # scheduler.schedule_every("12s") do
+ # Thread.new do
+ # do_the_job()
+ # end
+ # end
+ #
def schedule_every (freq, schedulable=nil, params=nil, &block)
+ schedule_every_with_id(freq, nil, schedulable, params, &block)
+ end
+
+ #
+ # Do not call directly.
+ #
+ def schedule_every_with_id (freq, at_id, schedulable, params, &block)
+
f = duration_to_f(freq)
- job_id = schedule_in(f) do |eid, at|
+ job_id = schedule_at_with_id(Time.new.to_f + f, at_id) do |eid, at|
if schedulable
schedulable.trigger(params)
else
block.call eid, at
end
- schedule_every(f, schedulable, params, &block)
+ schedule_every_with_id(f, eid, schedulable, params, &block)
end
- schedulable.job_id = job_id \
- if schedulable and schedulable.respond_to? :job_id=
-
job_id
end
+ protected :schedule_every_with_id
#
# Unschedules an 'at' or a 'cron' job identified by the id
# it was given at schedule time.
#
@@ -314,20 +342,34 @@
# or CronEntry will be returned.
#
def get_job (job_id)
entry = @cron_entries[job_id]
- return c if c
+ return entry if entry
@pending_jobs.each do |entry|
return entry if entry.eid == job_id
end
return nil
end
#
+ # Finds a job (via get_job()) and then returns the wrapped
+ # schedulable if any.
+ #
+ def get_schedulable (job_id)
+
+ return nil unless job_id
+
+ j = get_job(job_id)
+
+ return j.schedulable if j.respond_to? :schedulable
+ return nil
+ end
+
+ #
# Returns the number of currently pending jobs in this scheduler.
#
def pending_job_count
@pending_jobs.size
end
@@ -361,13 +403,18 @@
return Float(s.to_s)
end
def to_block (schedulable, params, &block)
if schedulable
- lambda do
+ l = lambda do
schedulable.trigger(params)
end
+ class << l
+ attr_accessor :schedulable
+ end
+ l.schedulable = schedulable
+ l
else
block
end
end
@@ -518,11 +565,11 @@
class AtEntry < Entry
attr_accessor \
:at
- def initialize (at, &block)
- super(&block)
+ def initialize (at, at_id, &block)
+ super(at_id, &block)
@at = at
end
def trigger
@block.call @eid, @at