Class: CloseYourIt::BackgroundWorker

Inherits:
Object
  • Object
show all
Defined in:
lib/closeyourit/background_worker.rb

Overview

Esegue l’invio fire-and-forget. Con ‘threads == 0` esegue sincrono (test/dev); altrimenti usa una thread-pool con coda bounded e `fallback_policy: :discard` (se la coda è piena l’evento si perde, mai backpressure sulla request).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(threads:, max_queue: 30) ⇒ BackgroundWorker

Returns a new instance of BackgroundWorker.



12
13
14
# File 'lib/closeyourit/background_worker.rb', line 12

def initialize(threads:, max_queue: 30)
  @executor = build_executor(threads.to_i, max_queue)
end

Instance Attribute Details

#executorObject (readonly)

Returns the value of attribute executor.



10
11
12
# File 'lib/closeyourit/background_worker.rb', line 10

def executor
  @executor
end

Instance Method Details

#perform(&block) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/closeyourit/background_worker.rb', line 16

def perform(&block)
  @executor.post do
    block.call
  rescue Exception => e # rubocop:disable Lint/RescueException
    # Mai propagare: la telemetria non deve poter crashare l'app ospite.
    CloseYourIt.logger.error("CloseYourIt background worker: #{e.class}: #{e.message}")
  end
end

#shutdown(timeout = 1) ⇒ Object



25
26
27
28
29
30
# File 'lib/closeyourit/background_worker.rb', line 25

def shutdown(timeout = 1)
  return unless @executor.respond_to?(:shutdown)

  @executor.shutdown
  @executor.wait_for_termination(timeout)
end