Parent

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 100
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 112
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 232
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 145
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 172
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 225
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 159
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 239
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 194
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 131
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 218
def jobs

  @jobs.to_h
end
schedule(cronstring, s=nil, opts={}, &block) click to toggle source
Alias for: cron
schedule_at(t, s=nil, opts={}, &block) click to toggle source
Alias for: at
schedule_every(t, s=nil, opts={}, &block) click to toggle source
Alias for: every
schedule_in(t, s=nil, opts={}, &block) click to toggle source
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 182
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 297
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 286
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 263
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 308
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 250
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 280
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 322
def trigger_job (blocking, &block)

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

[Validate]

Generated with the Darkfish Rdoc Generator 2.