The base class for all types of jobs.
The block to call when triggering
The identifier for this job.
Returns the thread instance of the last triggered job. May be null (especially before the first trigger).
The job parameters (passed via the schedule method)
A reference to the scheduler owning this job
The initial, raw, scheduling info (at / in / every / cron)
Instantiating the job.
# File lib/rufus/sc/jobs.rb, line 73 def initialize (scheduler, t, params, &block) @scheduler = scheduler @t = t @params = params @block = block || params[:schedulable] raise ArgumentError.new( 'no block or :schedulable passed, nothing to schedule' ) unless @block @params[:tags] = Array(@params[:tags]) @job_id = params[:job_id] || "#{self.class.name}_#{self.object_id.to_s}" determine_at end
Generally returns the string/float/integer used to schedule the job (seconds, time string, date string)
# File lib/rufus/sc/jobs.rb, line 109 def schedule_info @t end
Triggers the job.
# File lib/rufus/sc/jobs.rb, line 116 def trigger (t=Time.now) @last = t job_thread = nil @scheduler.send(:trigger_job, @params[:blocking]) do # # Note that #trigger_job is protected, hence the #send # (Only jobs know about this method of the scheduler) job_thread = Thread.current @last_job_thread = job_thread begin trigger_block job_thread = nil rescue Exception => e @scheduler.handle_exception(self, e) end end # note that add_job and add_cron_job ensured that :blocking is # not used along :timeout if to = @params[:timeout] @scheduler.in(to, :tags => 'timeout') do # at this point, @job_thread might be set job_thread.raise(Rufus::Scheduler::TimeOutError) if job_thread && job_thread.alive? end end end
Simply encapsulating the blockcall/trigger operation, for easy override.
# File lib/rufus/sc/jobs.rb, line 159 def trigger_block @block.respond_to?(:call) ? @block.call(self) : @block.trigger(@params.merge(:job => self)) end
Unschedules this job.
# File lib/rufus/sc/jobs.rb, line 167 def unschedule @scheduler.unschedule(self.job_id) end