Class: Sentry::BackgroundWorker

Inherits:
Object
  • Object
show all
Includes:
LoggingHelper
Defined in:
lib/sentry/background_worker.rb

Constant Summary collapse

DEFAULT_MAX_QUEUE =
30

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration) ⇒ BackgroundWorker

Returns a new instance of BackgroundWorker.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/sentry/background_worker.rb', line 17

def initialize(configuration)
  @shutdown_timeout = 1
  @number_of_threads = configuration.background_worker_threads
  @max_queue = configuration.background_worker_max_queue
  @sdk_logger = configuration.sdk_logger
  @debug = configuration.debug
  @shutdown_callback = nil

  @executor =
    if configuration.async
      log_debug("config.async is set, BackgroundWorker is disabled")
      Concurrent::ImmediateExecutor.new
    elsif @number_of_threads == 0
      log_debug("config.background_worker_threads is set to 0, all events will be sent synchronously")
      Concurrent::ImmediateExecutor.new
    else
      log_debug("Initializing the Sentry background worker with #{@number_of_threads} threads")

      executor = Concurrent::ThreadPoolExecutor.new(
        min_threads: 0,
        max_threads: @number_of_threads,
        max_queue: @max_queue,
        fallback_policy: :discard
      )

      @shutdown_callback = proc do
        executor.shutdown
        executor.wait_for_termination(@shutdown_timeout)
      end

      executor
    end
end

Instance Attribute Details

#max_queueObject (readonly)

Returns the value of attribute max_queue.



11
12
13
# File 'lib/sentry/background_worker.rb', line 11

def max_queue
  @max_queue
end

#number_of_threadsObject (readonly)

Returns the value of attribute number_of_threads.



11
12
13
# File 'lib/sentry/background_worker.rb', line 11

def number_of_threads
  @number_of_threads
end

#shutdown_timeoutObject

Returns the value of attribute shutdown_timeout.



13
14
15
# File 'lib/sentry/background_worker.rb', line 13

def shutdown_timeout
  @shutdown_timeout
end

Instance Method Details

#full?Boolean

Returns:

  • (Boolean)


67
68
69
70
# File 'lib/sentry/background_worker.rb', line 67

def full?
  @executor.is_a?(Concurrent::ThreadPoolExecutor) &&
    @executor.remaining_capacity == 0
end

#perform(&block) ⇒ Object

if you want to monkey-patch this method, please override ‘_perform` instead



52
53
54
55
56
57
58
59
60
# File 'lib/sentry/background_worker.rb', line 52

def perform(&block)
  @executor.post do
    begin
      _perform(&block)
    rescue Exception => e
      log_error("exception happened in background worker", e, debug: @debug)
    end
  end
end

#shutdownObject



62
63
64
65
# File 'lib/sentry/background_worker.rb', line 62

def shutdown
  log_debug("Shutting down background worker")
  @shutdown_callback&.call
end