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

Ritorna true se l'evento è stato accettato (o eseguito sincrono), false se scartato perché la coda era piena (fallback_policy: :discard). Mai backpressure sulla request.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/closeyourit/background_worker.rb', line 18

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

  unless accepted
    CloseYourIt.stats.increment(:dropped)
    CloseYourIt.internal_logger.warn("CloseYourIt background worker: coda piena, evento scartato")
  end

  accepted
end

#shutdown(timeout = 1) ⇒ Object



34
35
36
37
38
39
# File 'lib/closeyourit/background_worker.rb', line 34

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

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