class Rufus::Scheduler::SchedulerCore

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

Attributes

options[R]

classical options hash

Public Class Methods

new(opts={}) click to toggle source

Instantiates a Rufus::Scheduler.

# File lib/rufus/sc/scheduler.rb, line 103
def initialize (opts={})

  @options = opts

  @jobs = get_queue(:at, opts)
  @cron_jobs = get_queue(:cron, opts)

  @frequency = @options[:frequency] || 0.330
end
start_new(opts={}) click to toggle source

Instantiates and starts a new Rufus::Scheduler.

# File lib/rufus/sc/scheduler.rb, line 115
def self.start_new (opts={})

  s = self.new(opts)
  s.start
  s
end

Public Instance Methods

all_jobs() click to toggle source

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

# File lib/rufus/sc/scheduler.rb, line 235
def all_jobs

  jobs.merge(cron_jobs)
end
at(t, s=nil, opts={}, &block) click to toggle source

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
def at (t, s=nil, opts={}, &block)

  add_job(AtJob.new(self, t, combine_opts(s, opts), &block))
end
Also aliased as: schedule_at
cron(cronstring, s=nil, opts={}, &block) click to toggle source

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
def cron (cronstring, s=nil, opts={}, &block)

  add_cron_job(CronJob.new(self, cronstring, combine_opts(s, opts), &block))
end
Also aliased as: schedule
cron_jobs() click to toggle source

Returns a map job_id => job for cron jobs

# File lib/rufus/sc/scheduler.rb, line 228
def cron_jobs

  @cron_jobs.to_h
end
every(t, s=nil, opts={}, &block) click to toggle source

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
def every (t, s=nil, opts={}, &block)

  add_job(EveryJob.new(self, t, combine_opts(s, opts), &block))
end
Also aliased as: schedule_every
find_by_tag(tag) click to toggle source

Returns a list of jobs with the given tag

# File lib/rufus/sc/scheduler.rb, line 242
def find_by_tag (tag)

  all_jobs.values.select { |j| j.tags.include?(tag) }
end
handle_exception(job, exception) click to toggle source

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

# File lib/rufus/sc/scheduler.rb, line 197
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
in(t, s=nil, opts={}, &block) click to toggle source

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
def in (t, s=nil, opts={}, &block)

  add_job(InJob.new(self, t, combine_opts(s, opts), &block))
end
Also aliased as: schedule_in
jobs() click to toggle source

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

# File lib/rufus/sc/scheduler.rb, line 221
def jobs

  @jobs.to_h
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) click to toggle source

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
def unschedule (job_id)

  @jobs.unschedule(job_id) || @cron_jobs.unschedule(job_id)
end

Protected Instance Methods

add_cron_job(job) click to toggle source
# File lib/rufus/sc/scheduler.rb, line 300
def add_cron_job (job)

  complain_if_blocking_and_timeout(job)

  @cron_jobs << job

  job
end
add_job(job) click to toggle source
# File lib/rufus/sc/scheduler.rb, line 289
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
combine_opts(schedulable, opts) click to toggle source
# File lib/rufus/sc/scheduler.rb, line 266
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
complain_if_blocking_and_timeout(job) click to toggle source

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

# File lib/rufus/sc/scheduler.rb, line 311
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
get_queue(type, opts) click to toggle source

Returns a job queue instance.

(made it into a method for easy override)

# File lib/rufus/sc/scheduler.rb, line 253
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
step() click to toggle source

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

# File lib/rufus/sc/scheduler.rb, line 283
def step

  @cron_jobs.trigger_matching_jobs
  @jobs.trigger_matching_jobs
end
trigger_job(blocking, &block) click to toggle source

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 325
def trigger_job (blocking, &block)

  if blocking
    block.call
  else
    Thread.new { block.call }
  end
end