class Shoryuken::Options

Constants

DEFAULTS

Attributes

active_job_queue_name_prefixing[RW]
cache_visibility_timeout[RW]
default_worker_options[W]
exception_handlers[RW]
groups[RW]
launcher_executor[RW]
sqs_client[W]
sqs_client_receive_message_opts[R]
start_callback[RW]
stop_callback[RW]
worker_executor[RW]
worker_registry[RW]

Public Class Methods

new() click to toggle source
# File lib/shoryuken/options.rb, line 25
def initialize
  self.groups = {}
  self.worker_registry = DefaultWorkerRegistry.new
  self.exception_handlers = [DefaultExceptionHandler]
  self.active_job_queue_name_prefixing = false
  self.worker_executor = Worker::DefaultExecutor
  self.cache_visibility_timeout = false
  # this is needed for keeping backward compatibility
  @sqs_client_receive_message_opts ||= {}
end

Public Instance Methods

active_job?() click to toggle source
# File lib/shoryuken/options.rb, line 36
def active_job?
  defined?(::ActiveJob)
end
active_job_queue_name_prefixing?() click to toggle source
# File lib/shoryuken/options.rb, line 161
def active_job_queue_name_prefixing?
  @active_job_queue_name_prefixing
end
add_group(group, concurrency = nil, delay: nil) click to toggle source
# File lib/shoryuken/options.rb, line 40
def add_group(group, concurrency = nil, delay: nil)
  concurrency ||= options[:concurrency]
  delay ||= options[:delay]

  groups[group] ||= {
    concurrency: concurrency,
    delay: delay,
    queues: []
  }
end
add_queue(queue, weight, group) click to toggle source
# File lib/shoryuken/options.rb, line 51
def add_queue(queue, weight, group)
  weight.times do
    groups[group][:queues] << queue
  end
end
cache_visibility_timeout?() click to toggle source
# File lib/shoryuken/options.rb, line 157
def cache_visibility_timeout?
  @cache_visibility_timeout
end
client_middleware() { |_client_chain| ... } click to toggle source
# File lib/shoryuken/options.rb, line 113
def client_middleware
  @_client_chain ||= default_client_middleware
  yield @_client_chain if block_given?
  @_client_chain
end
configure_client() { |self| ... } click to toggle source
# File lib/shoryuken/options.rb, line 109
def configure_client
  yield self unless server?
end
configure_server() { |self| ... } click to toggle source
# File lib/shoryuken/options.rb, line 99
def configure_server
  yield self if server?
end
default_worker_options() click to toggle source
# File lib/shoryuken/options.rb, line 119
def default_worker_options
  @default_worker_options ||= {
    'queue' => 'default',
    'delete' => false,
    'auto_delete' => false,
    'auto_visibility_timeout' => false,
    'retry_intervals' => nil,
    'batch' => false
  }
end
delay(group) click to toggle source
# File lib/shoryuken/options.rb, line 75
def delay(group)
  groups[group].to_h.fetch(:delay, options[:delay]).to_f
end
logger() click to toggle source
# File lib/shoryuken/options.rb, line 91
def logger
  Shoryuken::Logging.logger
end
on(event, &block) click to toggle source

Register a block to run at a point in the Shoryuken lifecycle. :startup, :quiet, :shutdown or :stopped are valid events.

Shoryuken.configure_server do |config|
  config.on(:shutdown) do
    puts "Goodbye cruel world!"
  end
end
# File lib/shoryuken/options.rb, line 146
def on(event, &block)
  fail ArgumentError, "Symbols only please: #{event}" unless event.is_a?(Symbol)
  fail ArgumentError, "Invalid event name: #{event}" unless options[:lifecycle_events].key?(event)

  options[:lifecycle_events][event] << block
end
on_start(&block) click to toggle source
# File lib/shoryuken/options.rb, line 130
def on_start(&block)
  self.start_callback = block
end
on_stop(&block) click to toggle source
# File lib/shoryuken/options.rb, line 134
def on_stop(&block)
  self.stop_callback = block
end
options() click to toggle source
# File lib/shoryuken/options.rb, line 87
def options
  @options ||= DEFAULTS.dup
end
polling_strategy(group) click to toggle source
# File lib/shoryuken/options.rb, line 61
def polling_strategy(group)
  strategy = (group == 'default' ? options : options[:groups].to_h[group]).to_h[:polling_strategy]
  case strategy
  when 'WeightedRoundRobin', nil # Default case
    Polling::WeightedRoundRobin
  when 'StrictPriority'
    Polling::StrictPriority
  when Class
    strategy
  else
    raise ArgumentError, "#{strategy} is not a valid polling_strategy"
  end
end
register_worker(*args) click to toggle source
# File lib/shoryuken/options.rb, line 95
def register_worker(*args)
  worker_registry.register_worker(*args)
end
server?() click to toggle source
# File lib/shoryuken/options.rb, line 153
def server?
  defined?(Shoryuken::CLI)
end
server_middleware() { |_server_chain| ... } click to toggle source
# File lib/shoryuken/options.rb, line 103
def server_middleware
  @_server_chain ||= default_server_middleware
  yield @_server_chain if block_given?
  @_server_chain
end
sqs_client() click to toggle source
# File lib/shoryuken/options.rb, line 79
def sqs_client
  @sqs_client ||= Aws::SQS::Client.new
end
sqs_client_receive_message_opts=(sqs_client_receive_message_opts) click to toggle source
# File lib/shoryuken/options.rb, line 83
def sqs_client_receive_message_opts=(sqs_client_receive_message_opts)
  @sqs_client_receive_message_opts['default'] = sqs_client_receive_message_opts
end
ungrouped_queues() click to toggle source
# File lib/shoryuken/options.rb, line 57
def ungrouped_queues
  groups.values.flat_map { |options| options[:queues] }
end

Private Instance Methods

default_client_middleware() click to toggle source
# File lib/shoryuken/options.rb, line 180
def default_client_middleware
  Middleware::Chain.new
end
default_server_middleware() click to toggle source
# File lib/shoryuken/options.rb, line 167
def default_server_middleware
  Middleware::Chain.new do |m|
    m.add Middleware::Server::Timing
    m.add Middleware::Server::ExponentialBackoffRetry
    m.add Middleware::Server::AutoDelete
    m.add Middleware::Server::AutoExtendVisibility
    if defined?(::ActiveRecord::Base)
      require 'shoryuken/middleware/server/active_record'
      m.add Middleware::Server::ActiveRecord
    end
  end
end