lib/openwfe/util/scheduler.rb in openwferu-scheduler-0.9.15.1127 vs lib/openwfe/util/scheduler.rb in openwferu-scheduler-0.9.15.1128
- old
+ new
@@ -51,21 +51,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.
#
- # Two examples :
+ # Some examples :
#
# scheduler.schedule_in("3d") do
# regenerate_monthly_report()
# end
# #
# # will call the regenerate_monthly_report method
# # in 3 days from now
#
- # and
+ # scheduler.schedule "0 22 * * 1-5" do
+ # log.info "activating security system..."
+ # activate_security_system()
+ # end
#
+ # job_id = scheduler.schedule_at "Sun Oct 07 14:24:01 +0900 2009" do
+ # init_self_destruction_sequence()
+ # end
+ #
+ # an example that uses a Schedulable class :
+ #
# class Regenerator < Schedulable
# def trigger (frequency)
# self.send(frequency)
# end
# def monthly
@@ -120,10 +129,11 @@
# scheduler.schedule "0 24 * * *", :tags => "new_day" do
# do_this_or_that()
# end
#
# jobs = find_jobs 'backup'
+ # jobs.each { |job| job.unschedule }
#
# Multiple tags may be attached to a single job :
#
# scheduler.schedule_in "2h", :tags => [ "backup", "important" ] do
# init_backup_sequence()
@@ -388,11 +398,11 @@
#
# schedule
b = to_block(params, &block)
- job = CronJob.new(cron_id, cron_line, tags, &b)
+ job = CronJob.new(self, cron_id, cron_line, tags, &b)
@cron_jobs[job.job_id] = job
job.job_id
end
end
@@ -534,11 +544,11 @@
job_id = params[:job_id]
tags = params[:tags]
b = to_block(params, &block)
- job = jobClass.new(at, job_id, tags, &b)
+ job = jobClass.new(self, at, job_id, tags, &b)
unschedule(job_id) if job_id
if at < (Time.new.to_f + @precision)
job.trigger() unless params[:discard_past]
@@ -742,15 +752,16 @@
@@last_given_id = 0
#
# as a scheduler is fully transient, no need to
# have persistent ids, a simple counter is sufficient
- attr_accessor \
- :job_id, :tags, :block
+ attr_accessor :job_id, :tags, :block
+ attr_reader :scheduler
- def initialize (job_id, tags, &block)
+ def initialize (scheduler, job_id, tags, &block)
+ @scheduler = scheduler
@block = block
if job_id
@job_id = job_id
else
@@ -771,18 +782,25 @@
# Returns true if this job sports the given tag
#
def has_tag? (tag)
@tags.include?(tag)
end
+
+ #
+ # Removes (cancels) this job from its scheduler.
+ #
+ def unschedule
+ @scheduler.unschedule(@job_id)
+ end
end
class AtJob < Job
attr_accessor :at
- def initialize (at, at_id, tags, &block)
- super(at_id, tags, &block)
+ def initialize (scheduler, at, at_id, tags, &block)
+ super(scheduler, at_id, tags, &block)
@at = at
end
def trigger
@block.call @job_id, @at
@@ -794,12 +812,12 @@
class CronJob < Job
attr_accessor :cron_line
- def initialize (cron_id, line, tags, &block)
+ def initialize (scheduler, cron_id, line, tags, &block)
- super(cron_id, tags, &block)
+ super(scheduler, cron_id, tags, &block)
if line.kind_of?(String)
@cron_line = CronLine.new(line)
elsif line.kind_of?(CronLine)
@cron_line = line