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
# 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 @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)


64
65
66
67
# File 'lib/sentry/background_worker.rb', line 64

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



49
50
51
52
53
54
55
56
57
# File 'lib/sentry/background_worker.rb', line 49

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



59
60
61
62
# File 'lib/sentry/background_worker.rb', line 59

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