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.



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
50
# File 'lib/sentry/background_worker.rb', line 18

def initialize(configuration)
  @shutdown_timeout = 1
  @number_of_threads = configuration.background_worker_threads
  @max_queue = configuration.background_worker_max_queue
  @logger = configuration.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

#loggerObject (readonly)

Deprecated.

Use Sentry.logger to retrieve the current logger instead.



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

def logger
  @logger
end

#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.



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

def shutdown_timeout
  @shutdown_timeout
end

Instance Method Details

#full?Boolean

Returns:

  • (Boolean)


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

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



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

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



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

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