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
URGENT_REPLICA_WAIT_SLEEP_SECONDS =
0.1
SLEEP_ATTEMPTS =
3
URGENT_SLEEP_ATTEMPTS =
5

Instance Method Summary collapse

Methods included from WalTrackingReceiver

#databases_in_sync?

Instance Method Details

#call(worker, job, _queue) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/gitlab/database/load_balancing/sidekiq_server_middleware.rb', line 17

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::SessionMap
      .with_sessions(Gitlab::Database::LoadBalancing.base_models)
      .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
    set_per_database_strategy(resolved_class)
  end

  yield
ensure
  clear
end