The core of a rufus-scheduler. See implementations like Rufus::Scheduler::PlainScheduler and Rufus::Scheduler::EmScheduler for directly usable stuff.
Methods
public class
public instance
- all_jobs
- at
- cron
- cron_jobs
- every
- find_by_tag
- handle_exception
- in
- jobs
- schedule
- schedule_at
- schedule_every
- schedule_in
- unschedule
protected instance
Included modules
Attributes
| options | [R] | classical options hash |
Public class methods
Instantiates a Rufus::Scheduler.
# File lib/rufus/sc/scheduler.rb, line 103 103: def initialize (opts={}) 104: 105: @options = opts 106: 107: @jobs = JobQueue.new 108: @cron_jobs = CronJobQueue.new 109: 110: @frequency = @options[:frequency] || 0.330 111: end
Instantiates and starts a new Rufus::Scheduler.
# File lib/rufus/sc/scheduler.rb, line 115 115: def self.start_new (opts={}) 116: 117: s = self.new(opts) 118: s.start 119: s 120: end
Public instance methods
Returns a map job_id => job of all the jobs currently in the scheduler
# File lib/rufus/sc/scheduler.rb, line 234 234: def all_jobs 235: 236: jobs.merge(cron_jobs) 237: 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 148 148: def at (t, s=nil, opts={}, &block) 149: 150: add_job(AtJob.new(self, t, combine_opts(s, opts), &block)) 151: 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 175 175: def cron (cronstring, s=nil, opts={}, &block) 176: 177: add_cron_job(CronJob.new(self, cronstring, combine_opts(s, opts), &block)) 178: end
Returns a map job_id => job for cron jobs
# File lib/rufus/sc/scheduler.rb, line 227 227: def cron_jobs 228: 229: @cron_jobs.to_h 230: 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 162 162: def every (t, s=nil, opts={}, &block) 163: 164: add_job(EveryJob.new(self, t, combine_opts(s, opts), &block)) 165: end
Returns a list of jobs with the given tag
# File lib/rufus/sc/scheduler.rb, line 241 241: def find_by_tag (tag) 242: 243: all_jobs.values.select { |j| j.tags.include?(tag) } 244: end
Feel free to override this method. The default implementation simply outputs the error message to STDOUT
# File lib/rufus/sc/scheduler.rb, line 197 197: def handle_exception (job, exception) 198: 199: if self.respond_to?(:log_exception) 200: # 201: # some kind of backward compatibility 202: 203: log_exception(exception) 204: 205: else 206: 207: puts '=' * 80 208: puts "scheduler caught exception :" 209: puts exception 210: puts '=' * 80 211: end 212: 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 134 134: def in (t, s=nil, opts={}, &block) 135: 136: add_job(InJob.new(self, t, combine_opts(s, opts), &block)) 137: end
Returns a map job_id => job for at/in/every jobs
# File lib/rufus/sc/scheduler.rb, line 220 220: def jobs 221: 222: @jobs.to_h 223: end
Unschedules a job (cron or at/every/in job) given its id.
Returns the job that got unscheduled.
# File lib/rufus/sc/scheduler.rb, line 185 185: def unschedule (job_id) 186: 187: @jobs.unschedule(job_id) || @cron_jobs.unschedule(job_id) 188: end
Protected instance methods
# File lib/rufus/sc/scheduler.rb, line 301 301: def add_cron_job (job) 302: 303: complain_if_blocking_and_timeout(job) 304: 305: @cron_jobs << job 306: 307: job 308: end
# File lib/rufus/sc/scheduler.rb, line 290 290: def add_job (job) 291: 292: complain_if_blocking_and_timeout(job) 293: 294: return if job.params[:discard_past] && Time.now.to_f >= job.at 295: 296: @jobs << job 297: 298: job 299: end
# File lib/rufus/sc/scheduler.rb, line 283 283: def at_step 284: 285: while job = @jobs.job_to_trigger 286: job.trigger 287: end 288: end
# File lib/rufus/sc/scheduler.rb, line 248 248: def combine_opts (schedulable, opts) 249: 250: if schedulable.respond_to?(:trigger) 251: 252: opts[:schedulable] = schedulable 253: 254: elsif schedulable != nil 255: 256: opts = schedulable.merge(opts) 257: end 258: 259: opts 260: end
Raises an error if the job has the params :blocking and :timeout set
# File lib/rufus/sc/scheduler.rb, line 312 312: def complain_if_blocking_and_timeout (job) 313: 314: raise( 315: ArgumentError.new('cannot set a :timeout on a :blocking job') 316: ) if job.params[:blocking] and job.params[:timeout] 317: end
calls every second
# File lib/rufus/sc/scheduler.rb, line 272 272: def cron_step 273: 274: now = Time.now 275: return if now.sec == @last_cron_second 276: @last_cron_second = now.sec 277: # 278: # ensuring the crons are checked within 1 second (not 1.2 second) 279: 280: @cron_jobs.trigger_matching_jobs(now) 281: end
The method that does the “wake up and trigger any job that should get triggered.
# File lib/rufus/sc/scheduler.rb, line 265 265: def step 266: cron_step 267: at_step 268: 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 326 326: def trigger_job (blocking, &block) 327: 328: if blocking 329: block.call 330: else 331: Thread.new { block.call } 332: end 333: end