Class: Adrian::Dispatcher
- Inherits:
-
Object
- Object
- Adrian::Dispatcher
- Defined in:
- lib/adrian/dispatcher.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#running ⇒ Object
readonly
Returns the value of attribute running.
Instance Method Summary collapse
- #delegate_work(item, worker_class) ⇒ Object
-
#initialize(options = {}) ⇒ Dispatcher
constructor
A new instance of Dispatcher.
- #on_done(&blk) ⇒ Object
- #on_failure(*exceptions, &blk) ⇒ Object
- #start(queue, worker_class) ⇒ Object
- #stop ⇒ Object
- #trap_stop_signals ⇒ Object
- #work_done(item, worker, exception = nil) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Dispatcher
Returns a new instance of Dispatcher.
7 8 9 10 11 12 13 |
# File 'lib/adrian/dispatcher.rb', line 7 def initialize( = {}) @failure_handler = FailureHandler.new @stop_when_done = !![:stop_when_done] @stop_when_signalled = .fetch(:stop_when_signalled, true) @sleep = [:sleep] || 0.5 @options = end |
Instance Attribute Details
#running ⇒ Object (readonly)
Returns the value of attribute running.
5 6 7 |
# File 'lib/adrian/dispatcher.rb', line 5 def running @running end |
Instance Method Details
#delegate_work(item, worker_class) ⇒ Object
59 60 61 62 63 |
# File 'lib/adrian/dispatcher.rb', line 59 def delegate_work(item, worker_class) worker = worker_class.new(item) worker.report_to(self) worker.perform end |
#on_done(&blk) ⇒ Object
19 20 21 |
# File 'lib/adrian/dispatcher.rb', line 19 def on_done(&blk) @failure_handler.add_rule(nil, &blk) end |
#on_failure(*exceptions, &blk) ⇒ Object
15 16 17 |
# File 'lib/adrian/dispatcher.rb', line 15 def on_failure(*exceptions, &blk) @failure_handler.add_rule(*exceptions, &blk) end |
#start(queue, worker_class) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/adrian/dispatcher.rb', line 23 def start(queue, worker_class) trap_stop_signals if @stop_when_signalled @running = true while @running do begin item = queue.pop rescue Adrian::Queue::ItemTooOldError => e if handler = @failure_handler.handle(e) handler.call(e.item, nil, e) end item = nil next end if item delegate_work(item, worker_class) else if @stop_when_done stop else sleep(@sleep) if @sleep end end end end |
#stop ⇒ Object
50 51 52 |
# File 'lib/adrian/dispatcher.rb', line 50 def stop @running = false end |
#trap_stop_signals ⇒ Object
54 55 56 57 |
# File 'lib/adrian/dispatcher.rb', line 54 def trap_stop_signals Signal.trap('TERM') { stop } Signal.trap('INT') { stop } end |
#work_done(item, worker, exception = nil) ⇒ Object
65 66 67 68 69 70 71 |
# File 'lib/adrian/dispatcher.rb', line 65 def work_done(item, worker, exception = nil) if handler = @failure_handler.handle(exception) handler.call(item, worker, exception) else raise exception if exception end end |