2018-10-22 03:00:50 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-11-28 11:08:30 -05:00
|
|
|
require 'yaml'
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module SidekiqConfig
|
2020-01-22 07:08:40 -05:00
|
|
|
FOSS_QUEUE_CONFIG_PATH = 'app/workers/all_queues.yml'
|
|
|
|
EE_QUEUE_CONFIG_PATH = 'ee/app/workers/all_queues.yml'
|
2020-01-24 13:09:00 -05:00
|
|
|
SIDEKIQ_QUEUES_PATH = 'config/sidekiq_queues.yml'
|
2020-01-22 07:08:40 -05:00
|
|
|
|
|
|
|
QUEUE_CONFIG_PATHS = [
|
|
|
|
FOSS_QUEUE_CONFIG_PATH,
|
|
|
|
(EE_QUEUE_CONFIG_PATH if Gitlab.ee?)
|
|
|
|
].compact.freeze
|
|
|
|
|
2021-03-16 08:11:31 -04:00
|
|
|
# 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-04-28 14:10:12 -04:00
|
|
|
'_' => DummyWorker.new(
|
|
|
|
queue: 'default',
|
2021-09-20 20:09:28 -04:00
|
|
|
weight: 1,
|
|
|
|
tags: []
|
2021-04-28 14:10:12 -04:00
|
|
|
),
|
|
|
|
'ActionMailer::MailDeliveryJob' => DummyWorker.new(
|
|
|
|
name: 'ActionMailer::MailDeliveryJob',
|
|
|
|
queue: 'mailers',
|
|
|
|
urgency: 'low',
|
|
|
|
weight: 2,
|
|
|
|
tags: []
|
|
|
|
)
|
2021-03-16 08:11:31 -04:00
|
|
|
}.transform_values { |worker| Gitlab::SidekiqConfig::Worker.new(worker, ee: false) }.freeze
|
2020-01-22 07:08:40 -05:00
|
|
|
|
2020-01-15 13:08:34 -05:00
|
|
|
class << self
|
|
|
|
include Gitlab::SidekiqConfig::CliMethods
|
2018-11-19 16:29:19 -05:00
|
|
|
|
2020-01-15 13:08:34 -05:00
|
|
|
def redis_queues
|
|
|
|
# Not memoized, because this can change during the life of the application
|
|
|
|
Sidekiq::Queue.all.map(&:name)
|
2018-11-19 16:29:19 -05:00
|
|
|
end
|
2017-11-28 11:16:50 -05:00
|
|
|
|
2020-01-15 13:08:34 -05:00
|
|
|
def config_queues
|
|
|
|
@config_queues ||= begin
|
2020-01-24 13:09:00 -05:00
|
|
|
config = YAML.load_file(Rails.root.join(SIDEKIQ_QUEUES_PATH))
|
2020-01-15 13:08:34 -05:00
|
|
|
config[:queues].map(&:first)
|
|
|
|
end
|
2017-11-28 11:16:50 -05:00
|
|
|
end
|
|
|
|
|
2020-01-15 13:08:34 -05:00
|
|
|
def cron_workers
|
|
|
|
@cron_workers ||= Settings.cron_jobs.map { |job_name, options| options['job_class'].constantize }
|
|
|
|
end
|
2017-11-28 11:16:50 -05:00
|
|
|
|
2020-01-15 13:08:34 -05:00
|
|
|
def workers
|
|
|
|
@workers ||= begin
|
2020-01-22 07:08:40 -05:00
|
|
|
result = []
|
2021-03-16 08:11:31 -04:00
|
|
|
result.concat(DEFAULT_WORKERS.values)
|
2020-01-22 07:08:40 -05:00
|
|
|
result.concat(find_workers(Rails.root.join('app', 'workers'), ee: false))
|
|
|
|
|
|
|
|
if Gitlab.ee?
|
|
|
|
result.concat(find_workers(Rails.root.join('ee', 'app', 'workers'), ee: true))
|
|
|
|
end
|
|
|
|
|
2020-01-15 13:08:34 -05:00
|
|
|
result
|
|
|
|
end
|
2017-11-28 11:08:30 -05:00
|
|
|
end
|
|
|
|
|
2020-01-22 07:08:40 -05:00
|
|
|
def workers_for_all_queues_yml
|
|
|
|
workers.partition(&:ee?).reverse.map(&:sort)
|
|
|
|
end
|
|
|
|
|
2020-01-28 10:08:36 -05:00
|
|
|
# YAML.load_file is OK here as we control the file contents
|
2020-01-22 07:08:40 -05:00
|
|
|
def all_queues_yml_outdated?
|
|
|
|
foss_workers, ee_workers = workers_for_all_queues_yml
|
|
|
|
|
2020-01-28 10:08:36 -05:00
|
|
|
return true if foss_workers != YAML.load_file(FOSS_QUEUE_CONFIG_PATH)
|
2020-01-22 07:08:40 -05:00
|
|
|
|
2020-01-28 10:08:36 -05:00
|
|
|
Gitlab.ee? && ee_workers != YAML.load_file(EE_QUEUE_CONFIG_PATH)
|
2020-01-22 07:08:40 -05:00
|
|
|
end
|
|
|
|
|
2020-01-24 13:09:00 -05:00
|
|
|
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) }
|
|
|
|
|
|
|
|
(namespaces_with_equal_weights.map(&:namespace_and_weight) +
|
|
|
|
remaining_queues.map(&:queue_and_weight)).sort
|
|
|
|
end
|
|
|
|
|
2020-01-28 10:08:36 -05:00
|
|
|
# YAML.load_file is OK here as we control the file contents
|
2020-01-24 13:09:00 -05:00
|
|
|
def sidekiq_queues_yml_outdated?
|
2020-01-28 10:08:36 -05:00
|
|
|
config_queues = YAML.load_file(SIDEKIQ_QUEUES_PATH)[:queues]
|
2020-01-24 13:09:00 -05:00
|
|
|
|
|
|
|
queues_for_sidekiq_queues_yml != config_queues
|
|
|
|
end
|
|
|
|
|
2021-06-28 02:07:45 -04:00
|
|
|
# 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-01-15 13:08:34 -05:00
|
|
|
private
|
2017-11-28 11:08:30 -05:00
|
|
|
|
2020-01-22 07:08:40 -05:00
|
|
|
def find_workers(root, ee:)
|
2020-01-15 13:08:34 -05:00
|
|
|
concerns = root.join('concerns').to_s
|
2017-11-28 11:08:30 -05:00
|
|
|
|
2020-01-22 07:08:40 -05:00
|
|
|
Dir[root.join('**', '*.rb')]
|
2020-01-15 13:08:34 -05:00
|
|
|
.reject { |path| path.start_with?(concerns) }
|
2020-01-22 07:08:40 -05:00
|
|
|
.map { |path| worker_from_path(path, root) }
|
|
|
|
.select { |worker| worker < Sidekiq::Worker }
|
|
|
|
.map { |worker| Gitlab::SidekiqConfig::Worker.new(worker, ee: ee) }
|
|
|
|
end
|
2017-11-28 11:08:30 -05:00
|
|
|
|
2020-01-22 07:08:40 -05:00
|
|
|
def worker_from_path(path, root)
|
|
|
|
ns = Pathname.new(path).relative_path_from(root).to_s.gsub('.rb', '')
|
2017-11-28 11:08:30 -05:00
|
|
|
|
2020-01-22 07:08:40 -05:00
|
|
|
ns.camelize.constantize
|
2017-11-28 11:08:30 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|