Sha256: ec11c717a8aacdae797b8a6d4dae8d9476332b0bb0d42509aa31dffefce96f96

Contents?: true

Size: 1.83 KB

Versions: 15

Compression:

Stored size: 1.83 KB

Contents

module DaemonKit

  # 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 http://rufus.rubyforge.org/rufus-scheduler/
  #
  # To use the evented scheduler, call #DaemonKit::EM.run prior to
  # setting up your first schedule.
  class Cron

    @instance = nil
    @exception_handler = nil

    attr_reader :scheduler
    attr_accessor :exception_handler

    class << self

      def instance
        @instance ||= new
      end

      # Access to the scheduler instance
      def scheduler
        instance.scheduler
      end

      # Define a block for receiving exceptions from inside the scheduler
      def handle_exception( &block )
        instance.exception_handler = block
      end

      private :new

      # Once the scheduler has been configured, call #run to block the
      # current thread and keep the process alive for the scheduled
      # tasks to run
      def run
        DaemonKit.logger.info "Starting rufus-scheduler"

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

    def initialize
      @scheduler = Rufus::Scheduler.start_new

      def @scheduler.handle_exception( job, exception )
        DaemonKit::Cron.instance.handle_exception( job, exception )
      end
    end

    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
  end
end

Version data entries

15 entries across 15 versions & 2 rubygems

Version Path
daemon-kit-0.3.3 lib/daemon_kit/cron.rb
daemon-kit-0.3.2 lib/daemon_kit/cron.rb
daemon-kit-0.3.1 lib/daemon_kit/cron.rb
daemon-kit-0.3.0 lib/daemon_kit/cron.rb
daemon-kit-0.3.0.rc2 lib/daemon_kit/cron.rb
daemon-kit-0.3.0.rc1 lib/daemon_kit/cron.rb
daemon-kit-0.2.3 lib/daemon_kit/cron.rb
daemon-kit-0.2.1 lib/daemon_kit/cron.rb
daemon-kit-0.2.0 lib/daemon_kit/cron.rb
daemon-kit-0.1.8.2 lib/daemon_kit/cron.rb
amqp-daemon-kit-1.0.1 lib/daemon_kit/cron.rb
amqp-daemon-kit-0.1.8.2 lib/daemon_kit/cron.rb
amqp-daemon-kit-0.1.8.1 lib/daemon_kit/cron.rb
daemon-kit-0.1.8.1 lib/daemon_kit/cron.rb
daemon-kit-0.1.8 lib/daemon_kit/cron.rb