Class Rufus::Scheduler::SchedulerCore

  1. lib/rufus/sc/scheduler.rb
Parent: Object

The core of a rufus-scheduler. See implementations like Rufus::Scheduler::PlainScheduler and Rufus::Scheduler::EmScheduler for directly usable stuff.

Included modules

  1. LegacyMethods

Attributes

options [R] classical options hash

Public class methods

new (opts={})

Instantiates a Rufus::Scheduler.

[show source]
     # 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
start_new (opts={})

Instantiates and starts a new Rufus::Scheduler.

[show source]
     # 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

all_jobs ()

Returns a map job_id => job of all the jobs currently in the scheduler

[show source]
     # File lib/rufus/sc/scheduler.rb, line 234
234:     def all_jobs
235: 
236:       jobs.merge(cron_jobs)
237:     end
at (t, s=nil, opts={}, &block)

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).

[show source]
     # 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
cron (cronstring, s=nil, opts={}, &block)

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
[show source]
     # 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
cron_jobs ()

Returns a map job_id => job for cron jobs

[show source]
     # File lib/rufus/sc/scheduler.rb, line 227
227:     def cron_jobs
228: 
229:       @cron_jobs.to_h
230:     end
every (t, s=nil, opts={}, &block)

Schedules a recurring job every t.

scheduler.every '5m1w' do
  puts 'check blood pressure'
end

checking blood pressure every 5 months and 1 week.

[show source]
     # 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
find_by_tag (tag)

Returns a list of jobs with the given tag

[show source]
     # 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
handle_exception (job, exception)

Feel free to override this method. The default implementation simply outputs the error message to STDOUT

[show source]
     # 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
in (t, s=nil, opts={}, &block)

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.

[show source]
     # 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
jobs ()

Returns a map job_id => job for at/in/every jobs

[show source]
     # File lib/rufus/sc/scheduler.rb, line 220
220:     def jobs
221: 
222:       @jobs.to_h
223:     end
schedule (cronstring, s=nil, opts={}, &block)

Alias for cron

schedule_at (t, s=nil, opts={}, &block)

Alias for at

schedule_every (t, s=nil, opts={}, &block)

Alias for every

schedule_in (t, s=nil, opts={}, &block)

Alias for in

unschedule (job_id)

Unschedules a job (cron or at/every/in job) given its id.

Returns the job that got unscheduled.

[show source]
     # 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

add_cron_job (job)
[show source]
     # 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
add_job (job)
[show source]
     # 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
at_step ()
[show source]
     # 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
combine_opts (schedulable, opts)
[show source]
     # 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
complain_if_blocking_and_timeout (job)

Raises an error if the job has the params :blocking and :timeout set

[show source]
     # 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
cron_step ()

calls every second

[show source]
     # 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
step ()

The method that does the “wake up and trigger any job that should get triggered.

[show source]
     # File lib/rufus/sc/scheduler.rb, line 265
265:     def step
266:       cron_step
267:       at_step
268:     end
trigger_job (blocking, &block)

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 …

[show source]
     # 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