class DaemonKit::Cron

Thin wrapper around rufus-scheduler gem, specifically designed to ease configuration of a scheduler and provide some added simplicity. It also logs any exceptions that occur inside the scheduled blocks, ensuring your code isn't running blind.

For more information on rufus-scheduler, please visit the RDoc's at rufus.rubyforge.org/rufus-scheduler/

To use the evented scheduler, call #DaemonKit::EM.run prior to setting up your first schedule.

Attributes

exception_handler[RW]
scheduler[R]

Public Class Methods

handle_exception( &block ) click to toggle source

Define a block for receiving exceptions from inside the scheduler

# File lib/daemon_kit/cron.rb, line 33
def handle_exception( &block )
  instance.exception_handler = block
end
instance() click to toggle source
# File lib/daemon_kit/cron.rb, line 23
def instance
  @instance ||= new
end
new() click to toggle source
# File lib/daemon_kit/cron.rb, line 53
def initialize
  @scheduler = Rufus::Scheduler.start_new

  def @scheduler.handle_exception( job, exception )
    DaemonKit::Cron.instance.handle_exception( job, exception )
  end
end
run() click to toggle source

Once the scheduler has been configured, call run to block the current thread and keep the process alive for the scheduled tasks to run

# File lib/daemon_kit/cron.rb, line 42
def run
  DaemonKit.logger.info "Starting rufus-scheduler"

  if instance.is_a?( Rufus::Scheduler::PlainScheduler )
    instance.scheduler.join
  else
    Thread.stop
  end
end
scheduler() click to toggle source

Access to the scheduler instance

# File lib/daemon_kit/cron.rb, line 28
def scheduler
  instance.scheduler
end

Public Instance Methods

handle_exception( job, exception ) click to toggle source
# File lib/daemon_kit/cron.rb, line 61
def handle_exception( job, exception )
  DaemonKit.logger.error( "Cron: job #{job.job_id} caught exception: '#{exception}'" )
  DaemonKit.logger.exception( exception )
  @exception_handler.call( job, exception ) unless @exception_handler.nil?
end