Class: Gitlab::SidekiqConfig::WorkerRouter

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/sidekiq_config/worker_router.rb

Defined Under Namespace

Classes: RuleEvaluator

Constant Summary collapse

InvalidRoutingRuleError =
Class.new(StandardError)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(routing_rules) ⇒ WorkerRouter

call-seq:

router = WorkerRouter.new([
  ["resource_boundary=cpu", 'cpu_boundary'],
  ["feature_category=pages", nil],
  ["feature_category=source_code_management", ''],
  ["*", "default"]
])
router.route(ACpuBoundaryWorker) # Return "cpu_boundary"
router.route(JustAPagesWorker)   # Return "just_a_pages_worker"
router.route(PostReceive)        # Return "post_receive"
router.route(RandomWorker)       # Return "default"

This class is responsible for routing a Sidekiq worker to a certain queue defined in the input routing rules. The input routing rules, as described above, is an order-matter array of tuples [query, queue_name].

  • The query syntax follows “worker matching query” detailedly

denoted in doc/administration/operations/extra_sidekiq_processes.md.

  • The queue_name must be a valid Sidekiq queue name. If the queue name

is nil, or an empty string, the worker is routed to the queue generated by the name of the worker instead.

Rules are evaluated from first to last, and as soon as we find a match for a given worker we stop processing for that worker (first match wins). If the worker doesn’t match any rule, it falls back the queue name generated from the worker name

For further information, please visit:

https://gitlab.com/gitlab-com/gl-infra/scalability/-/issues/1016


58
59
60
# File 'lib/gitlab/sidekiq_config/worker_router.rb', line 58

def initialize(routing_rules)
  @rule_evaluators = parse_routing_rules(routing_rules)
end

Class Method Details

.globalObject



19
20
21
22
23
24
25
# File 'lib/gitlab/sidekiq_config/worker_router.rb', line 19

def self.global
  @global_worker_router ||= new(::Gitlab.config.sidekiq.routing_rules)
rescue InvalidRoutingRuleError, ::Gitlab::SidekiqConfig::WorkerMatcher::UnknownPredicate => e
  ::Gitlab::ErrorTracking.track_and_raise_for_dev_exception(e)

  @global_worker_router = new([])
end

.queue_name_from_worker_name(worker_klass) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/gitlab/sidekiq_config/worker_router.rb', line 9

def self.queue_name_from_worker_name(worker_klass)
  base_queue_name =
    worker_klass.name
      .delete_prefix('Gitlab::')
      .delete_suffix('Worker')
      .underscore
      .tr('/', '_')
  [worker_klass.queue_namespace, base_queue_name].compact.join(':')
end

Instance Method Details

#route(worker_klass) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/gitlab/sidekiq_config/worker_router.rb', line 62

def route(worker_klass)
  # A medium representation to ensure the backward-compatibility of
  # WorkerMatcher
   = (worker_klass)
  @rule_evaluators.each do |evaluator|
    if evaluator.matcher.match?()
      return evaluator.queue_name.presence || queue_name_from_worker_name(worker_klass)
    end
  end

  queue_name_from_worker_name(worker_klass)
end