Class: Snmp2mkr::EngineThreads::Worker

Inherits:
Object
  • Object
show all
Defined in:
lib/snmp2mkr/engine_threads/worker.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#loggerObject (readonly)

Returns the value of attribute logger.



10
11
12
# File 'lib/snmp2mkr/engine_threads/worker.rb', line 10

def logger
  @logger
end

#queueObject (readonly)

Returns the value of attribute queue.



10
11
12
# File 'lib/snmp2mkr/engine_threads/worker.rb', line 10

def queue
  @queue
end

#threadObject (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

#joinObject



16
17
18
19
# File 'lib/snmp2mkr/engine_threads/worker.rb', line 16

def join
  return unless running?
  @thread.join
end

#main_loopObject



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

Returns:

  • (Boolean)


12
13
14
# File 'lib/snmp2mkr/engine_threads/worker.rb', line 12

def running?
  @thread && @thread.alive?
end

#startObject



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