class Shoryuken::EnvironmentLoader

Attributes

options[R]

Public Class Methods

load_for_rails_console() click to toggle source
# File lib/shoryuken/environment_loader.rb, line 11
def self.load_for_rails_console
  instance = setup_options(config_file: (Rails.root + 'config' + 'shoryuken.yml'))
  instance.load
end
new(options) click to toggle source
# File lib/shoryuken/environment_loader.rb, line 16
def initialize(options)
  @options = options
end
setup_options(options) click to toggle source
# File lib/shoryuken/environment_loader.rb, line 5
def self.setup_options(options)
  instance = new(options)
  instance.setup_options
  instance
end

Public Instance Methods

load() click to toggle source
# File lib/shoryuken/environment_loader.rb, line 26
def load
  prefix_active_job_queue_names
  parse_queues
  require_workers
  validate_queues
  validate_workers
end
setup_options() click to toggle source
# File lib/shoryuken/environment_loader.rb, line 20
def setup_options
  initialize_rails if load_rails?
  initialize_options
  initialize_logger
end

Private Instance Methods

config_file_options() click to toggle source
# File lib/shoryuken/environment_loader.rb, line 41
def config_file_options
  return {} unless (path = options[:config_file])

  fail ArgumentError, "The supplied config file #{path} does not exist" unless File.exist?(path)

  if (result = YAML.load(ERB.new(IO.read(path)).result))
    result.deep_symbolize_keys
  else
    {}
  end
end
initialize_logger() click to toggle source
# File lib/shoryuken/environment_loader.rb, line 53
def initialize_logger
  Shoryuken::Logging.initialize_logger(Shoryuken.options[:logfile]) if Shoryuken.options[:logfile]
  Shoryuken.logger.level = Logger::DEBUG if Shoryuken.options[:verbose]
end
initialize_options() click to toggle source
# File lib/shoryuken/environment_loader.rb, line 36
def initialize_options
  Shoryuken.options.merge!(config_file_options)
  Shoryuken.options.merge!(options)
end
initialize_rails() click to toggle source
# File lib/shoryuken/environment_loader.rb, line 58
def initialize_rails
  # Adapted from: https://github.com/mperham/sidekiq/blob/master/lib/sidekiq/cli.rb

  require 'rails'
  if ::Rails::VERSION::MAJOR < 4
    require File.expand_path('config/environment.rb')
    ::Rails.application.eager_load!
  else
    # Painful contortions, see 1791 for discussion
    require File.expand_path('config/application.rb')
    if ::Rails::VERSION::MAJOR == 4
      ::Rails::Application.initializer 'shoryuken.eager_load' do
        ::Rails.application.config.eager_load = true
      end
    end
    if Shoryuken.active_job?
      require 'shoryuken/extensions/active_job_extensions'
      require 'shoryuken/extensions/active_job_adapter'
      require 'shoryuken/extensions/active_job_concurrent_send_adapter'
    end
    require File.expand_path('config/environment.rb')
  end
end
load_rails?() click to toggle source
# File lib/shoryuken/environment_loader.rb, line 82
def load_rails?
  options[:rails]
end
parse_queue(queue, weight, group) click to toggle source
# File lib/shoryuken/environment_loader.rb, line 117
def parse_queue(queue, weight, group)
  Shoryuken.add_queue(queue, [weight.to_i, 1].max, group)
end
parse_queues() click to toggle source
# File lib/shoryuken/environment_loader.rb, line 121
def parse_queues
  if Shoryuken.options[:queues].to_a.any?
    Shoryuken.add_group('default', Shoryuken.options[:concurrency])

    Shoryuken.options[:queues].to_a.each do |queue, weight|
      parse_queue(queue, weight, 'default')
    end
  end

  Shoryuken.options[:groups].to_a.each do |group, options|
    Shoryuken.add_group(group, options[:concurrency], delay: options[:delay])

    options[:queues].to_a.each do |queue, weight|
      parse_queue(queue, weight, group)
    end
  end
end
prefix_active_job_queue_name(queue_name, weight) click to toggle source
# File lib/shoryuken/environment_loader.rb, line 86
def prefix_active_job_queue_name(queue_name, weight)
  return [queue_name, weight] if queue_name.start_with?('https://', 'arn:')

  queue_name_prefix = ::ActiveJob::Base.queue_name_prefix
  queue_name_delimiter = ::ActiveJob::Base.queue_name_delimiter

  # See https://github.com/rails/rails/blob/master/activejob/lib/active_job/queue_name.rb#L27
  name_parts = [queue_name_prefix.presence, queue_name]
  prefixed_queue_name = name_parts.compact.join(queue_name_delimiter)
  [prefixed_queue_name, weight]
end
prefix_active_job_queue_names() click to toggle source
# File lib/shoryuken/environment_loader.rb, line 98
def prefix_active_job_queue_names
  return unless Shoryuken.active_job?
  return unless Shoryuken.active_job_queue_name_prefixing?

  Shoryuken.options[:queues].to_a.map! do |queue_name, weight|
    prefix_active_job_queue_name(queue_name, weight)
  end

  Shoryuken.options[:groups].to_a.map! do |group, options|
    if options[:queues]
      options[:queues].map! do |queue_name, weight|
        prefix_active_job_queue_name(queue_name, weight)
      end
    end

    [group, options]
  end
end
require_workers() click to toggle source
# File lib/shoryuken/environment_loader.rb, line 139
def require_workers
  required = Shoryuken.options[:require]

  return unless required

  if File.directory?(required)
    Dir[File.join(required, '**', '*.rb')].each(&method(:require))
  else
    require required
  end
end
validate_queues() click to toggle source
# File lib/shoryuken/environment_loader.rb, line 151
    def validate_queues
      return Shoryuken.logger.warn { 'No queues supplied' } if Shoryuken.ungrouped_queues.empty?

      non_existent_queues = []

      Shoryuken.ungrouped_queues.uniq.each do |queue|
        begin
          Shoryuken::Client.queues(queue)
        rescue Aws::Errors::NoSuchEndpointError, Aws::SQS::Errors::NonExistentQueue
          non_existent_queues << queue
        end
      end

      return if non_existent_queues.none?

      # NOTE: HEREDOC's ~ operator removes indents, but is only available Ruby 2.3+
      # See github PR: https://github.com/ruby-shoryuken/shoryuken/pull/691#issuecomment-1007653595
      error_msg = <<-MSG.gsub(/^\s+/, '')
        The specified queue(s) #{non_existent_queues.join(', ')} do not exist.
        Try 'shoryuken sqs create QUEUE-NAME' for creating a queue with default settings.
        It's also possible that you don't have permission to access the specified queues.
      MSG

      fail(
        ArgumentError,
        error_msg
      )
    end
validate_workers() click to toggle source
# File lib/shoryuken/environment_loader.rb, line 180
def validate_workers
  return if Shoryuken.active_job?

  all_queues = Shoryuken.ungrouped_queues
  queues_with_workers = Shoryuken.worker_registry.queues

  (all_queues - queues_with_workers).each do |queue|
    Shoryuken.logger.warn { "No worker supplied for #{queue}" }
  end
end