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.
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 |
# File 'lib/quebert/worker.rb', line 7 def initialize 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 |
Instance Method Details
#safe_stop ⇒ Object
29 30 31 32 33 34 35 36 37 38 |
# File 'lib/quebert/worker.rb', line 29 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
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/quebert/worker.rb', line 12 def start Signal.trap('TERM') { safe_stop } Signal.trap('INT') { safe_stop } logger.info "Worker started with #{backend.class.name} backend\n" while @controller = backend.reserve do begin @controller.perform rescue Exception => e exception_handler ? exception_handler.call(e) : raise(e) end @controller = nil stop if @terminate_sent end end |
#stop ⇒ Object
40 41 42 43 |
# File 'lib/quebert/worker.rb', line 40 def stop logger.info "Worker stopping\n" exit 0 end |