class Rufus::Scheduler::SchedulerCore
directly usable stuff.
Rufus::Scheduler::PlainScheduler and Rufus::Scheduler::EmScheduler for
The core of a rufus-scheduler. See implementations like
def self.start_new (opts={})
Instantiates and starts a new Rufus::Scheduler.
def self.start_new (opts={}) s = self.new(opts) s.start s end
def add_cron_job (job)
def add_cron_job (job) complain_if_blocking_and_timeout(job) @cron_jobs << job job end
def add_job (job)
def add_job (job) complain_if_blocking_and_timeout(job) return if job.params[:discard_past] && Time.now.to_f >= job.at @jobs << job job end
def all_jobs
Returns a map job_id => job of all the jobs currently in the scheduler
def all_jobs jobs.merge(cron_jobs) end
def at (t, s=nil, opts={}, &block)
pizza is for Thursday at 2000 (if the shop brochure is right).
end
puts 'order pizza'
scheduler.at 'Thu Mar 26 19:30:00 2009' do
Schedules a job at a given point in time.
def at (t, s=nil, opts={}, &block) add_job(AtJob.new(self, t, combine_opts(s, opts), &block)) end
def combine_opts (schedulable, opts)
def combine_opts (schedulable, opts) if schedulable.respond_to?(:trigger) || schedulable.respond_to?(:call) opts[:schedulable] = schedulable elsif schedulable != nil opts = schedulable.merge(opts) end opts end
def complain_if_blocking_and_timeout (job)
Raises an error if the job has the params :blocking and :timeout set
def complain_if_blocking_and_timeout (job) raise( ArgumentError.new('cannot set a :timeout on a :blocking job') ) if job.params[:blocking] and job.params[:timeout] end
def cron (cronstring, s=nil, opts={}, &block)
end
puts 'activate security system'
# every day of the week at 00:22
scheduler.cron '0 22 * * 1-5' do
Schedules a job given a cron string.
def cron (cronstring, s=nil, opts={}, &block) add_cron_job(CronJob.new(self, cronstring, combine_opts(s, opts), &block)) end
def cron_jobs
Returns a map job_id => job for cron jobs
def cron_jobs @cron_jobs.to_h end
def every (t, s=nil, opts={}, &block)
checking blood pressure every 5 months and 1 week.
end
puts 'check blood pressure'
scheduler.every '5m1w' do
Schedules a recurring job every t.
def every (t, s=nil, opts={}, &block) add_job(EveryJob.new(self, t, combine_opts(s, opts), &block)) end
def find_by_tag (tag)
Returns a list of jobs with the given tag
def find_by_tag (tag) all_jobs.values.select { |j| j.tags.include?(tag) } end
def get_queue (type, opts)
(made it into a method for easy override)
Returns a job queue instance.
def get_queue (type, opts) q = if type == :cron opts[:cron_job_queue] || Rufus::Scheduler::CronJobQueue.new else opts[:job_queue] || Rufus::Scheduler::JobQueue.new end q.scheduler = self if q.respond_to?(:scheduler=) q end
def handle_exception (job, exception)
outputs the error message to STDOUT
Feel free to override this method. The default implementation simply
def handle_exception (job, exception) if self.respond_to?(:log_exception) # # some kind of backward compatibility log_exception(exception) else puts '=' * 80 puts "scheduler caught exception :" puts exception exception.backtrace.each { |l| puts l } puts '=' * 80 end end
def in (t, s=nil, opts={}, &block)
will order an espresso (well sort of) in 20 minutes.
end
puts "order ristretto"
scheduler.in '20m' do
Schedules a job in a given amount of time.
def in (t, s=nil, opts={}, &block) add_job(InJob.new(self, t, combine_opts(s, opts), &block)) end
def initialize (opts={})
Instantiates a Rufus::Scheduler.
def initialize (opts={}) @options = opts @jobs = get_queue(:at, opts) @cron_jobs = get_queue(:cron, opts) @frequency = @options[:frequency] || 0.330 end
def jobs
Returns a map job_id => job for at/in/every jobs
def jobs @jobs.to_h end
def step
triggered.
The method that does the "wake up and trigger any job that should get
def step @cron_jobs.trigger_matching_jobs @jobs.trigger_matching_jobs end
def trigger_job (blocking, &block)
EmScheduler blocking triggers for the next tick. Not the same thing ...
TODO : clarify, the blocking here blocks the whole scheduler, while
Else, it will call the block in a dedicated thread.
call the block and return when the block is done.
The default, plain, implementation. If 'blocking' is true, will simply
def trigger_job (blocking, &block) if blocking block.call else Thread.new { block.call } end end
def unschedule (job_id)
Returns the job that got unscheduled.
Unschedules a job (cron or at/every/in job) given its id.
def unschedule (job_id) @jobs.unschedule(job_id) || @cron_jobs.unschedule(job_id) end