-
-
Notifications
You must be signed in to change notification settings - Fork 479
/
background_worker.rb
41 lines (35 loc) · 1.17 KB
/
background_worker.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
require "concurrent/executor/thread_pool_executor"
require "concurrent/executor/immediate_executor"
require "concurrent/configuration"
module Sentry
class BackgroundWorker
include LoggingHelper
attr_reader :max_queue, :number_of_threads, :logger
def initialize(configuration)
@max_queue = 30
@number_of_threads = configuration.background_worker_threads
@logger = configuration.logger
@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("initialized a background worker with #{@number_of_threads} threads")
Concurrent::ThreadPoolExecutor.new(
min_threads: 0,
max_threads: @number_of_threads,
max_queue: @max_queue,
fallback_policy: :discard
)
end
end
def perform(&block)
@executor.post do
block.call
end
end
end
end