2018-10-22 03:00:50 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-11-28 11:08:30 -05:00
|
|
|
require 'yaml'
|
2017-11-28 11:16:50 -05:00
|
|
|
require 'set'
|
2017-11-28 11:08:30 -05:00
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module SidekiqConfig
|
2018-11-19 16:29:19 -05:00
|
|
|
QUEUE_CONFIG_PATHS = %w[app/workers/all_queues.yml ee/app/workers/all_queues.yml].freeze
|
|
|
|
|
2017-11-28 11:16:50 -05:00
|
|
|
# This method is called by `bin/sidekiq-cluster` in EE, which runs outside
|
|
|
|
# of bundler/Rails context, so we cannot use any gem or Rails methods.
|
|
|
|
def self.worker_queues(rails_path = Rails.root.to_s)
|
|
|
|
@worker_queues ||= {}
|
2018-11-19 16:29:19 -05:00
|
|
|
|
|
|
|
@worker_queues[rails_path] ||= QUEUE_CONFIG_PATHS.flat_map do |path|
|
|
|
|
full_path = File.join(rails_path, path)
|
|
|
|
|
|
|
|
File.exist?(full_path) ? YAML.load_file(full_path) : []
|
|
|
|
end
|
2017-11-28 11:08:30 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# This method is called by `bin/sidekiq-cluster` in EE, which runs outside
|
|
|
|
# of bundler/Rails context, so we cannot use any gem or Rails methods.
|
2017-11-28 11:16:50 -05:00
|
|
|
def self.expand_queues(queues, all_queues = self.worker_queues)
|
|
|
|
return [] if queues.empty?
|
|
|
|
|
|
|
|
queues_set = all_queues.to_set
|
|
|
|
|
|
|
|
queues.flat_map do |queue|
|
|
|
|
[queue, *queues_set.grep(/\A#{queue}:/)]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.redis_queues
|
|
|
|
# Not memoized, because this can change during the life of the application
|
|
|
|
Sidekiq::Queue.all.map(&:name)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.config_queues
|
2017-11-28 11:08:30 -05:00
|
|
|
@config_queues ||= begin
|
2017-11-28 11:16:50 -05:00
|
|
|
config = YAML.load_file(Rails.root.join('config/sidekiq_queues.yml'))
|
2017-11-28 11:08:30 -05:00
|
|
|
config[:queues].map(&:first)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.cron_workers
|
|
|
|
@cron_workers ||= Settings.cron_jobs.map { |job_name, options| options['job_class'].constantize }
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.workers
|
|
|
|
@workers ||= find_workers(Rails.root.join('app', 'workers'))
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.find_workers(root)
|
|
|
|
concerns = root.join('concerns').to_s
|
|
|
|
|
|
|
|
workers = Dir[root.join('**', '*.rb')]
|
|
|
|
.reject { |path| path.start_with?(concerns) }
|
|
|
|
|
|
|
|
workers.map! do |path|
|
|
|
|
ns = Pathname.new(path).relative_path_from(root).to_s.gsub('.rb', '')
|
|
|
|
|
|
|
|
ns.camelize.constantize
|
|
|
|
end
|
|
|
|
|
2017-11-28 11:16:50 -05:00
|
|
|
# Skip things that aren't workers
|
2017-11-28 11:08:30 -05:00
|
|
|
workers.select { |w| w < Sidekiq::Worker }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|