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 72 def initialize(scheduler, t, params, &block) @scheduler = scheduler @t = t @params = params @block = block || params[:schedulable] raise_on_unknown_params @running = false @paused = false 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
# File lib/rufus/sc/jobs.rb, line 255 def self.known_params(*args) define_method :known_params do super() + args end end
Pauses this job (sets the paused flag to true).
Note that it will not pause the execution of a block currently ‘running’. Future triggering of the job will not occur until resume is called.
Note too that, during the pause time, the schedule kept the same. Calling resume will not force old triggers in.
# File lib/rufus/sc/jobs.rb, line 125 def pause @paused = true end
Returns true if this job is paused, false else.
A paused job is still scheduled, but does not trigger.
Note : paused? is not related to running?
# File lib/rufus/sc/jobs.rb, line 112 def paused? @paused end
Resumes this job (sets the paused flag to false).
This job will trigger again.
# File lib/rufus/sc/jobs.rb, line 134 def resume @paused = false end
Returns true if this job is currently running (in the middle of trigger)
Note : paused? is not related to running?
# File lib/rufus/sc/jobs.rb, line 99 def running @running end
Generally returns the string/float/integer used to schedule the job (seconds, time string, date string)
# File lib/rufus/sc/jobs.rb, line 157 def schedule_info @t end
Triggers the job.
# File lib/rufus/sc/jobs.rb, line 164 def trigger(t=Time.now) return if @paused @last = t job_thread = nil to_job = nil return if @running and (params[:allow_overlapping] == false) @running = true @scheduler.send(:trigger_job, @params) do # # Note that #trigger_job is protected, hence the #send # (Only jobs know about this method of the scheduler) job_thread = Thread.current job_thread[ "rufus_scheduler__trigger_thread__#{@scheduler.object_id}" ] = self @last_job_thread = job_thread begin trigger_block job_thread[ "rufus_scheduler__trigger_thread__#{@scheduler.object_id}" ] = nil job_thread = nil to_job.unschedule if to_job rescue Exception => e @scheduler.do_handle_exception(self, e) end @running = false end # note that add_job and add_cron_job ensured that :blocking is # not used along :timeout if to = @params[:timeout] to_job = @scheduler.in(to, :parent => self, :tags => 'timeout') do # at this point, @job_thread might be set if job_thread && job_thread.alive? job_thread.raise(Rufus::Scheduler::TimeOutError) end end end end
Simply encapsulating the blockcall/trigger operation, for easy override.
# File lib/rufus/sc/jobs.rb, line 228 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 236 def unschedule @scheduler.unschedule(self.job_id) end
# File lib/rufus/sc/jobs.rb, line 243 def known_params [ :allow_overlapping, :blocking, :discard_past, :job_id, :mutex, :schedulable, :tags, :timeout ] end
# File lib/rufus/sc/jobs.rb, line 262 def raise_on_unknown_params rem = @params.keys - known_params raise( ArgumentError, "unknown option#{rem.size > 1 ? 's' : '' }: " + "#{rem.map(&:inspect).join(', ')}", caller[3..-1] ) if rem.any? end