sidekiq_config.rb 2.08 KB
Newer Older
1 2
# frozen_string_literal: true

3
require 'yaml'
4
require 'set'
5 6 7

module Gitlab
  module SidekiqConfig
8 9
    QUEUE_CONFIG_PATHS = %w[app/workers/all_queues.yml ee/app/workers/all_queues.yml].freeze

10
    # This method is called by `ee/bin/sidekiq-cluster` in EE, which runs outside
11 12 13
    # 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 ||= {}
14 15 16 17 18 19

      @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
20 21
    end

22
    # This method is called by `ee/bin/sidekiq-cluster` in EE, which runs outside
23
    # of bundler/Rails context, so we cannot use any gem or Rails methods.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
    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
40
      @config_queues ||= begin
41
        config = YAML.load_file(Rails.root.join('config/sidekiq_queues.yml'))
42 43 44 45 46 47 48 49 50
        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
51 52 53
      @workers ||=
        find_workers(Rails.root.join('app', 'workers')) +
        find_workers(Rails.root.join('ee', 'app', 'workers'))
54 55 56 57 58 59 60 61 62 63 64 65 66 67
    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

68
      # Skip things that aren't workers
69 70 71 72
      workers.select { |w| w < Sidekiq::Worker }
    end
  end
end