debian-mirror-gitlab/lib/gitlab/sidekiq_config.rb

141 lines
4.5 KiB
Ruby
Raw Normal View History

2018-12-13 13:39:08 +05:30
# frozen_string_literal: true
2018-03-17 18:26:18 +05:30
require 'yaml'
module Gitlab
module SidekiqConfig
2020-03-13 15:44:24 +05:30
FOSS_QUEUE_CONFIG_PATH = 'app/workers/all_queues.yml'
EE_QUEUE_CONFIG_PATH = 'ee/app/workers/all_queues.yml'
SIDEKIQ_QUEUES_PATH = 'config/sidekiq_queues.yml'
QUEUE_CONFIG_PATHS = [
FOSS_QUEUE_CONFIG_PATH,
(EE_QUEUE_CONFIG_PATH if Gitlab.ee?)
].compact.freeze
2021-04-29 21:17:54 +05:30
# This maps workers not in our application code to queues. We need
# these queues in our YAML files to ensure we don't accidentally
# miss jobs from these queues.
#
# The default queue should be unused, which is why it maps to an
# invalid class name. We keep it in the YAML file for safety, just
# in case anything does get scheduled to run there.
DEFAULT_WORKERS = {
2021-06-08 01:23:25 +05:30
'_' => DummyWorker.new(
queue: 'default',
weight: 1, tags: []
),
'ActionMailer::MailDeliveryJob' => DummyWorker.new(
name: 'ActionMailer::MailDeliveryJob',
queue: 'mailers',
feature_category: :issue_tracking,
urgency: 'low',
weight: 2,
tags: []
)
2021-04-29 21:17:54 +05:30
}.transform_values { |worker| Gitlab::SidekiqConfig::Worker.new(worker, ee: false) }.freeze
2020-03-13 15:44:24 +05:30
class << self
include Gitlab::SidekiqConfig::CliMethods
def redis_queues
# Not memoized, because this can change during the life of the application
Sidekiq::Queue.all.map(&:name)
end
def config_queues
@config_queues ||= begin
config = YAML.load_file(Rails.root.join(SIDEKIQ_QUEUES_PATH))
config[:queues].map(&:first)
end
end
def cron_workers
@cron_workers ||= Settings.cron_jobs.map { |job_name, options| options['job_class'].constantize }
end
2019-02-15 15:39:39 +05:30
2020-03-13 15:44:24 +05:30
def workers
@workers ||= begin
result = []
2021-04-29 21:17:54 +05:30
result.concat(DEFAULT_WORKERS.values)
2020-03-13 15:44:24 +05:30
result.concat(find_workers(Rails.root.join('app', 'workers'), ee: false))
2019-02-15 15:39:39 +05:30
2020-03-13 15:44:24 +05:30
if Gitlab.ee?
result.concat(find_workers(Rails.root.join('ee', 'app', 'workers'), ee: true))
end
2019-02-15 15:39:39 +05:30
2020-03-13 15:44:24 +05:30
result
end
end
def workers_for_all_queues_yml
workers.partition(&:ee?).reverse.map(&:sort)
2019-02-15 15:39:39 +05:30
end
2018-03-17 18:26:18 +05:30
2020-03-13 15:44:24 +05:30
# YAML.load_file is OK here as we control the file contents
def all_queues_yml_outdated?
foss_workers, ee_workers = workers_for_all_queues_yml
2018-03-17 18:26:18 +05:30
2020-03-13 15:44:24 +05:30
return true if foss_workers != YAML.load_file(FOSS_QUEUE_CONFIG_PATH)
2018-03-17 18:26:18 +05:30
2020-03-13 15:44:24 +05:30
Gitlab.ee? && ee_workers != YAML.load_file(EE_QUEUE_CONFIG_PATH)
2018-03-17 18:26:18 +05:30
end
2020-03-13 15:44:24 +05:30
def queues_for_sidekiq_queues_yml
namespaces_with_equal_weights =
workers
.group_by(&:queue_namespace)
.map(&:last)
.select { |workers| workers.map(&:get_weight).uniq.count == 1 }
.map(&:first)
namespaces = namespaces_with_equal_weights.map(&:queue_namespace).to_set
remaining_queues = workers.reject { |worker| namespaces.include?(worker.queue_namespace) }
2018-03-17 18:26:18 +05:30
2020-03-13 15:44:24 +05:30
(namespaces_with_equal_weights.map(&:namespace_and_weight) +
remaining_queues.map(&:queue_and_weight)).sort
2018-03-17 18:26:18 +05:30
end
2020-03-13 15:44:24 +05:30
# YAML.load_file is OK here as we control the file contents
def sidekiq_queues_yml_outdated?
config_queues = YAML.load_file(SIDEKIQ_QUEUES_PATH)[:queues]
2018-03-17 18:26:18 +05:30
2020-03-13 15:44:24 +05:30
queues_for_sidekiq_queues_yml != config_queues
2019-12-21 20:55:43 +05:30
end
2018-03-17 18:26:18 +05:30
2021-09-30 23:02:18 +05:30
# Returns a hash of worker class name => mapped queue name
def worker_queue_mappings
workers
.reject { |worker| worker.klass.is_a?(Gitlab::SidekiqConfig::DummyWorker) }
.to_h { |worker| [worker.klass.to_s, ::Gitlab::SidekiqConfig::WorkerRouter.global.route(worker.klass)] }
end
# Like worker_queue_mappings, but only for the queues running in
# the current Sidekiq process
def current_worker_queue_mappings
worker_queue_mappings
.select { |worker, queue| Sidekiq.options[:queues].include?(queue) }
.to_h
end
2020-03-13 15:44:24 +05:30
private
def find_workers(root, ee:)
concerns = root.join('concerns').to_s
2018-03-17 18:26:18 +05:30
2020-03-13 15:44:24 +05:30
Dir[root.join('**', '*.rb')]
.reject { |path| path.start_with?(concerns) }
.map { |path| worker_from_path(path, root) }
.select { |worker| worker < Sidekiq::Worker }
.map { |worker| Gitlab::SidekiqConfig::Worker.new(worker, ee: ee) }
end
2018-03-17 18:26:18 +05:30
2020-03-13 15:44:24 +05:30
def worker_from_path(path, root)
2018-03-17 18:26:18 +05:30
ns = Pathname.new(path).relative_path_from(root).to_s.gsub('.rb', '')
ns.camelize.constantize
end
end
end
end