Class: TopologicalInventory::Providers::Common::Operations::AsyncWorker

Inherits:
Object
  • Object
show all
Includes:
Logging, Mixins::Statuses
Defined in:
lib/topological_inventory/providers/common/operations/async_worker.rb

Instance Method Summary collapse

Methods included from Mixins::Statuses

#operation_status

Methods included from Logging

#logger

Constructor Details

#initialize(processor, queue: nil, metrics: nil) ⇒ AsyncWorker

Returns a new instance of AsyncWorker.



13
14
15
16
17
# File 'lib/topological_inventory/providers/common/operations/async_worker.rb', line 13

def initialize(processor, queue: nil, metrics: nil)
  @processor = processor
  @queue     = queue || Queue.new
  @metrics   = metrics
end

Instance Method Details

#enqueue(msg) ⇒ Object



29
30
31
# File 'lib/topological_inventory/providers/common/operations/async_worker.rb', line 29

def enqueue(msg)
  queue << msg
end

#listenObject



33
34
35
36
37
38
39
# File 'lib/topological_inventory/providers/common/operations/async_worker.rb', line 33

def listen
  loop do
    # the queue thread waits for a message to come during `Queue#pop`
    msg = queue.pop
    process_message(msg)
  end
end

#startObject



19
20
21
22
23
# File 'lib/topological_inventory/providers/common/operations/async_worker.rb', line 19

def start
  return if thread.present?

  @thread = Thread.new { listen }
end

#stopObject



25
26
27
# File 'lib/topological_inventory/providers/common/operations/async_worker.rb', line 25

def stop
  thread&.exit
end