Class: TopologicalInventory::Providers::Common::Operations::AsyncWorker
- Inherits:
-
Object
- Object
- TopologicalInventory::Providers::Common::Operations::AsyncWorker
- Includes:
- Logging, Mixins::Statuses
- Defined in:
- lib/topological_inventory/providers/common/operations/async_worker.rb
Instance Method Summary collapse
- #enqueue(msg) ⇒ Object
-
#initialize(processor, queue: nil, metrics: nil) ⇒ AsyncWorker
constructor
A new instance of AsyncWorker.
- #listen ⇒ Object
- #start ⇒ Object
- #stop ⇒ Object
Methods included from Mixins::Statuses
Methods included from Logging
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 |
#listen ⇒ Object
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 (msg) end end |
#start ⇒ Object
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 |
#stop ⇒ Object
25 26 27 |
# File 'lib/topological_inventory/providers/common/operations/async_worker.rb', line 25 def stop thread&.exit end |