Class: Allora::Scheduler

Inherits:
Object
  • Object
show all
Defined in:
lib/allora/scheduler.rb

Overview

Worker daemon, dealing with a Backend to execute jobs at regular intervals.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Scheduler

Initialize the Scheduler with the given options.

Options:

backend:  an instance of any Backend class (defaults to Memory)
interval: a floating point specifying how frequently to poll (defaults to 0.333)
logger:   an instance of a ruby Logger, or nil to disable
*other:   any additonal parameters are passed to the Backend

Parameters:

  • options (Hash)

    options for the scheduler, if any



40
41
42
43
44
45
46
47
48
# File 'lib/allora/scheduler.rb', line 40

def initialize(opts = {})
  require "logger"

  @backend  = create_backend(opts)
  @interval = opts.fetch(:interval, 0.333)
  @logger   = opts.fetch(:logger, default_logger)
  @jobs     = {}
  @shutdown = false
end

Instance Attribute Details

#backendObject (readonly)

Returns the value of attribute backend.



28
29
30
# File 'lib/allora/scheduler.rb', line 28

def backend
  @backend
end

#jobsObject (readonly)

Returns the value of attribute jobs.



27
28
29
# File 'lib/allora/scheduler.rb', line 27

def jobs
  @jobs
end

Instance Method Details

#add(name, opts_or_job, &block) ⇒ Job

Register a new job for the given options.

Examples:

s.add("foo", :every => 5.seconds) { puts "Running!" }
s.add("bar", :cron => "*/15 * 1,10,20 * *") { puts "Bonus!" }

Parameters:

  • name (String)

    a unique name to give this job (used for locking)

  • opts_or_job (Hash, Job)

    options specifying when to run the job (:every, or :cron), or a Job instance.

Returns:

  • (Job)

    the job instance added to the schedule



64
65
66
67
68
# File 'lib/allora/scheduler.rb', line 64

def add(name, opts_or_job, &block)
  log "Loading into schedule '#{name}' #{opts_or_job.inspect}"

  jobs[name.to_s] = create_job(opts_or_job, &block)
end

#joinObject

Join the currently running scheduler Thread.

This should be invoked to prevent the parent Thread from terminating.



102
103
104
# File 'lib/allora/scheduler.rb', line 102

def join
  @thread.join
end

#startThread

Starts running the scheduler in a new Thread, and returns that Thread.

Returns:

  • (Thread)

    the scheduler polling Thread



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/allora/scheduler.rb', line 74

def start
  log "Starting scheduler process, using #{@backend.class}"

  %w[TERM INT QUIT].each { |sig| Signal.trap(sig) { stop } }

  @thread = Thread.new do
    loop do
      @backend.reschedule(@jobs).each do |name, job|
        log "Running job '#{name}'"
        job.execute
      end

      Thread.exit if @shutdown

      sleep(@interval)
    end
  end
end

#stopObject

Stop the currently running scheduler Thread



94
95
96
97
# File 'lib/allora/scheduler.rb', line 94

def stop
  log "Exiting scheduler process"
  @shutdown = true
end