Class: Allora::Scheduler
- Inherits:
-
Object
- Object
- Allora::Scheduler
- Defined in:
- lib/allora/scheduler.rb
Overview
Worker daemon, dealing with a Backend to execute jobs at regular intervals.
Instance Attribute Summary collapse
-
#backend ⇒ Object
readonly
Returns the value of attribute backend.
-
#jobs ⇒ Object
readonly
Returns the value of attribute jobs.
Instance Method Summary collapse
-
#add(name, opts_or_job, &block) ⇒ Job
Register a new job for the given options.
-
#initialize(opts = {}) ⇒ Scheduler
constructor
Initialize the Scheduler with the given options.
-
#join ⇒ Object
Join the currently running scheduler Thread.
-
#start ⇒ Thread
Starts running the scheduler in a new Thread, and returns that Thread.
-
#stop ⇒ Object
Stop the currently running scheduler Thread.
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
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
#backend ⇒ Object (readonly)
Returns the value of attribute backend.
28 29 30 |
# File 'lib/allora/scheduler.rb', line 28 def backend @backend end |
#jobs ⇒ Object (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.
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 |
#join ⇒ Object
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 |
#start ⇒ Thread
Starts running the scheduler in a new Thread, and returns that 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 |
#stop ⇒ Object
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 |