Object
The core of a rufus-scheduler. See implementations like Rufus::Scheduler::PlainScheduler and Rufus::Scheduler::EmScheduler for directly usable stuff.
Instantiates a Rufus::Scheduler.
# File lib/rufus/sc/scheduler.rb, line 100
100: def initialize(opts={})
101:
102: @options = opts
103:
104: @jobs = get_queue(:at, opts)
105: @cron_jobs = get_queue(:cron, opts)
106:
107: @frequency = @options[:frequency] || 0.330
108: end
Instantiates and starts a new Rufus::Scheduler.
# File lib/rufus/sc/scheduler.rb, line 112
112: def self.start_new(opts={})
113:
114: s = self.new(opts)
115: s.start
116: s
117: end
Returns a map job_id => job of all the jobs currently in the scheduler
# File lib/rufus/sc/scheduler.rb, line 232
232: def all_jobs
233:
234: jobs.merge(cron_jobs)
235: end
Schedules a job at a given point in time.
scheduler.at 'Thu Mar 26 19:30:00 2009' do
puts 'order pizza'
end
pizza is for Thursday at 2000 (if the shop brochure is right).
# File lib/rufus/sc/scheduler.rb, line 145
145: def at(t, s=nil, opts={}, &block)
146:
147: add_job(AtJob.new(self, t, combine_opts(s, opts), &block))
148: end
Schedules a job given a cron string.
scheduler.cron '0 22 * * 1-5' do
# every day of the week at 00:22
puts 'activate security system'
end
# File lib/rufus/sc/scheduler.rb, line 172
172: def cron(cronstring, s=nil, opts={}, &block)
173:
174: add_cron_job(CronJob.new(self, cronstring, combine_opts(s, opts), &block))
175: end
Returns a map job_id => job for cron jobs
# File lib/rufus/sc/scheduler.rb, line 225
225: def cron_jobs
226:
227: @cron_jobs.to_h
228: end
Schedules a recurring job every t.
scheduler.every '5m1w' do
puts 'check blood pressure'
end
checking blood pressure every 5 months and 1 week.
# File lib/rufus/sc/scheduler.rb, line 159
159: def every(t, s=nil, opts={}, &block)
160:
161: add_job(EveryJob.new(self, t, combine_opts(s, opts), &block))
162: end
Returns a list of jobs with the given tag
# File lib/rufus/sc/scheduler.rb, line 239
239: def find_by_tag(tag)
240:
241: all_jobs.values.select { |j| j.tags.include?(tag) }
242: end
Feel free to override this method. The default implementation simply outputs the error message to STDOUT
# File lib/rufus/sc/scheduler.rb, line 194
194: def handle_exception(job, exception)
195:
196: if self.respond_to?(:log_exception)
197: #
198: # some kind of backward compatibility
199:
200: log_exception(exception)
201:
202: else
203:
204: puts '=' * 80
205: puts "scheduler caught exception :"
206: puts exception
207: exception.backtrace.each { |l| puts l }
208: puts '=' * 80
209: end
210: end
Schedules a job in a given amount of time.
scheduler.in '20m' do
puts "order ristretto"
end
will order an espresso (well sort of) in 20 minutes.
# File lib/rufus/sc/scheduler.rb, line 131
131: def in(t, s=nil, opts={}, &block)
132:
133: add_job(InJob.new(self, t, combine_opts(s, opts), &block))
134: end
Returns a map job_id => job for at/in/every jobs
# File lib/rufus/sc/scheduler.rb, line 218
218: def jobs
219:
220: @jobs.to_h
221: end
Returns the current list of trigger threads (threads) dedicated to the execution of jobs.
# File lib/rufus/sc/scheduler.rb, line 247
247: def trigger_threads
248:
249: Thread.list.select { |t|
250: t["rufus_scheduler__trigger_thread__#{self.object_id}"] == true
251: }
252: end
# File lib/rufus/sc/scheduler.rb, line 307
307: def add_cron_job(job)
308:
309: complain_if_blocking_and_timeout(job)
310:
311: @cron_jobs << job
312:
313: job
314: end
# File lib/rufus/sc/scheduler.rb, line 296
296: def add_job(job)
297:
298: complain_if_blocking_and_timeout(job)
299:
300: return if job.params[:discard_past] && Time.now.to_f >= job.at
301:
302: @jobs << job
303:
304: job
305: end
# File lib/rufus/sc/scheduler.rb, line 273
273: def combine_opts(schedulable, opts)
274:
275: if schedulable.respond_to?(:trigger) || schedulable.respond_to?(:call)
276:
277: opts[:schedulable] = schedulable
278:
279: elsif schedulable != nil
280:
281: opts = schedulable.merge(opts)
282: end
283:
284: opts
285: end
Raises an error if the job has the params :blocking and :timeout set
# File lib/rufus/sc/scheduler.rb, line 318
318: def complain_if_blocking_and_timeout(job)
319:
320: raise(
321: ArgumentError.new('cannot set a :timeout on a :blocking job')
322: ) if job.params[:blocking] and job.params[:timeout]
323: end
Returns a job queue instance.
(made it into a method for easy override)
# File lib/rufus/sc/scheduler.rb, line 260
260: def get_queue(type, opts)
261:
262: q = if type == :cron
263: opts[:cron_job_queue] || Rufus::Scheduler::CronJobQueue.new
264: else
265: opts[:job_queue] || Rufus::Scheduler::JobQueue.new
266: end
267:
268: q.scheduler = self if q.respond_to?(:scheduler=)
269:
270: q
271: end
The method that does the “wake up and trigger any job that should get triggered.
# File lib/rufus/sc/scheduler.rb, line 290
290: def step
291:
292: @cron_jobs.trigger_matching_jobs
293: @jobs.trigger_matching_jobs
294: end
The default, plain, implementation. If ‘blocking’ is true, will simply call the block and return when the block is done. Else, it will call the block in a dedicated thread.
TODO : clarify, the blocking here blocks the whole scheduler, while EmScheduler blocking triggers for the next tick. Not the same thing …
# File lib/rufus/sc/scheduler.rb, line 332
332: def trigger_job(blocking, &block)
333:
334: if blocking
335: block.call
336: else
337: Thread.new { block.call }
338: end
339: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.