Class: Gitlab::Database::LoadBalancing::SidekiqServerMiddleware

Inherits:
Object
  • Object
show all
Includes:
WalTrackingReceiver
Defined in:
lib/gitlab/database/load_balancing/sidekiq_server_middleware.rb

Constant Summary collapse

JobReplicaNotUpToDate =
Class.new(::Gitlab::SidekiqMiddleware::RetryError)
REPLICA_WAIT_SLEEP_SECONDS =
0.5

Instance Method Summary collapse

Methods included from WalTrackingReceiver

#databases_in_sync?

Instance Method Details

#call(worker, job, _queue) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/gitlab/database/load_balancing/sidekiq_server_middleware.rb', line 13

def call(worker, job, _queue)
  # ActiveJobs have wrapped class stored in 'wrapped' key
  resolved_class = job['wrapped']&.safe_constantize || worker.class
  strategy = select_load_balancing_strategy(resolved_class, job)

  job['load_balancing_strategy'] = strategy.to_s

  if use_primary?(strategy)
    ::Gitlab::Database::LoadBalancing::Session.current.use_primary!
  elsif strategy == :retry
    raise JobReplicaNotUpToDate, "Sidekiq job #{resolved_class} JID-#{job['jid']} couldn't use the replica."\
      " Replica was not up to date."
  else
    # this means we selected an up-to-date replica, but there is nothing to do in this case.
  end

  yield
ensure
  clear
end