Class: Quebert::Worker
- Inherits:
-
Object
- Object
- Quebert::Worker
- Includes:
- Logging
- Defined in:
- lib/quebert/worker.rb
Instance Attribute Summary collapse
-
#backend ⇒ Object
writeonly
Sets the attribute backend.
-
#exception_handler ⇒ Object
writeonly
Sets the attribute exception_handler.
-
#queues ⇒ Object
Returns the value of attribute queues.
Instance Method Summary collapse
-
#initialize {|_self| ... } ⇒ Worker
constructor
A new instance of Worker.
- #safe_stop ⇒ Object
-
#start ⇒ Object
Start the worker backend and intercept exceptions if a handler is provided.
- #stop ⇒ Object
Constructor Details
#initialize {|_self| ... } ⇒ Worker
Returns a new instance of Worker.
7 8 9 10 |
# File 'lib/quebert/worker.rb', line 7 def initialize @queues = [] yield self if block_given? end |
Instance Attribute Details
#backend=(value) ⇒ Object
Sets the attribute backend
5 6 7 |
# File 'lib/quebert/worker.rb', line 5 def backend=(value) @backend = value end |
#exception_handler=(value) ⇒ Object
Sets the attribute exception_handler
5 6 7 |
# File 'lib/quebert/worker.rb', line 5 def exception_handler=(value) @exception_handler = value end |
#queues ⇒ Object
Returns the value of attribute queues.
5 6 7 |
# File 'lib/quebert/worker.rb', line 5 def queues @queues end |
Instance Method Details
#safe_stop ⇒ Object
42 43 44 45 46 47 48 49 50 51 |
# File 'lib/quebert/worker.rb', line 42 def safe_stop if @terminate_sent logger.info "Ok! I get the point. Shutting down immediately." stop else logger.info "Finishing current job then shutting down." @terminate_sent = true stop unless @controller end end |
#start ⇒ Object
Start the worker backend and intercept exceptions if a handler is provided
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/quebert/worker.rb', line 13 def start Signal.trap('TERM') { safe_stop } Signal.trap('INT') { safe_stop } logger.info "Worker started with #{backend.class.name} backend\n" backend.queues = queues if backend.respond_to?(:queues=) while @controller = backend.reserve do begin @controller.perform rescue => error if exception_handler exception_handler.call( error, :controller => @controller, :pid => $$, :worker => self ) else raise end end @controller = nil stop if @terminate_sent end end |
#stop ⇒ Object
53 54 55 56 |
# File 'lib/quebert/worker.rb', line 53 def stop logger.info "Worker stopping\n" exit 0 end |