Class: Snmp2mkr::EngineThreads::Worker
- Inherits:
-
Object
- Object
- Snmp2mkr::EngineThreads::Worker
- Defined in:
- lib/snmp2mkr/engine_threads/worker.rb
Instance Attribute Summary collapse
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#queue ⇒ Object
readonly
Returns the value of attribute queue.
-
#thread ⇒ Object
readonly
Returns the value of attribute thread.
Instance Method Summary collapse
-
#initialize(queue:, logger: Logger.new($stdout)) ⇒ Worker
constructor
A new instance of Worker.
- #join ⇒ Object
- #main_loop ⇒ Object
- #process(job) ⇒ Object
- #running? ⇒ Boolean
- #start ⇒ Object
Constructor Details
#initialize(queue:, logger: Logger.new($stdout)) ⇒ Worker
Returns a new instance of Worker.
4 5 6 7 8 |
# File 'lib/snmp2mkr/engine_threads/worker.rb', line 4 def initialize(queue:, logger: Logger.new($stdout)) @queue = queue @logger = logger @thread = nil end |
Instance Attribute Details
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
10 11 12 |
# File 'lib/snmp2mkr/engine_threads/worker.rb', line 10 def logger @logger end |
#queue ⇒ Object (readonly)
Returns the value of attribute queue.
10 11 12 |
# File 'lib/snmp2mkr/engine_threads/worker.rb', line 10 def queue @queue end |
#thread ⇒ Object (readonly)
Returns the value of attribute thread.
10 11 12 |
# File 'lib/snmp2mkr/engine_threads/worker.rb', line 10 def thread @thread end |
Instance Method Details
#join ⇒ Object
16 17 18 19 |
# File 'lib/snmp2mkr/engine_threads/worker.rb', line 16 def join return unless running? @thread.join end |
#main_loop ⇒ Object
26 27 28 29 30 31 32 33 34 |
# File 'lib/snmp2mkr/engine_threads/worker.rb', line 26 def main_loop while job = queue.pop process job end rescue Exception => e logger.error "#{e.inspect}\n\t#{e.backtrace.join("\n\t")}" sleep 1 retry end |
#process(job) ⇒ Object
36 37 38 39 |
# File 'lib/snmp2mkr/engine_threads/worker.rb', line 36 def process(job) logger.debug "processing job #{job.inspect}" job.perform! end |
#running? ⇒ Boolean
12 13 14 |
# File 'lib/snmp2mkr/engine_threads/worker.rb', line 12 def running? @thread && @thread.alive? end |
#start ⇒ Object
21 22 23 24 |
# File 'lib/snmp2mkr/engine_threads/worker.rb', line 21 def start return if running? @thread = Thread.new(&method(:main_loop)) end |