The base class for all types of jobs.
Attributes
| block | [R] | The block to call when triggering |
| job_id | [R] | The identifier for this job. |
| job_thread | [R] | When the job is actually running, this attribute will hold the thread in which the job is running. Can be used to determine if a job is actually running. |
| last | [R] |
Last time the job executed (for an {At|In}Job, it
will mean ‘not executed’ if nil or when it got executed if set)
( Last time job got triggered (most useful with EveryJob, but can be useful with remaining instances of At/InJob (are they done ?)) ) |
| params | [R] | The job parameters (passed via the schedule method) |
| scheduler | [R] | A reference to the scheduler owning this job |
| t | [R] | The initial, raw, scheduling info (at / in / every / cron) |
Public class methods
Instantiating the job.
# File lib/rufus/sc/jobs.rb, line 74 74: def initialize (scheduler, t, params, &block) 75: 76: @scheduler = scheduler 77: @t = t 78: @params = params 79: @block = block || params[:schedulable] 80: 81: raise ArgumentError.new( 82: 'no block or :schedulable passed, nothing to schedule' 83: ) unless @block 84: 85: @params[:tags] = Array(@params[:tags]) 86: 87: @job_id = params[:job_id] || "#{self.class.name}_#{self.object_id.to_s}" 88: 89: determine_at 90: end
Public instance methods
Generally returns the string/float/integer used to schedule the job (seconds, time string, date string)
# File lib/rufus/sc/jobs.rb, line 110 110: def schedule_info 111: 112: @t 113: end
Returns the list of tags attached to the job.
# File lib/rufus/sc/jobs.rb, line 94 94: def tags 95: 96: @params[:tags] 97: end
Sets the list of tags attached to the job (Usually they are set via the schedule every/at/in/cron method).
# File lib/rufus/sc/jobs.rb, line 102 102: def tags= (tags) 103: 104: @params[:tags] = Array(tags) 105: end
Triggers the job.
# File lib/rufus/sc/jobs.rb, line 117 117: def trigger (t=Time.now) 118: 119: @last = t 120: 121: @scheduler.send(:trigger_job, @params[:blocking]) do 122: # 123: # Note that #trigger_job is protected, hence the #send 124: # (Only jobs know about this method of the scheduler) 125: 126: @job_thread = Thread.current 127: 128: begin 129: 130: #args = prepare_args 131: #@block.call(*args) 132: 133: #@block.call(self) 134: 135: @block.respond_to?(:call) ? 136: @block.call(self) : @block.trigger(@params) 137: 138: @job_thread = nil 139: 140: rescue Exception => e 141: 142: @scheduler.handle_exception(self, e) 143: end 144: end 145: 146: # note that add_job and add_cron_job ensured that :blocking is 147: # not used along :timeout 148: 149: if to = @params[:timeout] 150: 151: @scheduler.in(to, :tags => 'timeout') do 152: 153: # at this point, @job_thread might be set 154: 155: @job_thread.raise(Rufus::Scheduler::TimeOutError) \ 156: if @job_thread and @job_thread.alive? 157: end 158: end 159: end
Unschedules this job.
# File lib/rufus/sc/jobs.rb, line 163 163: def unschedule 164: 165: @scheduler.unschedule(self.job_id) 166: end