Class: Shoryuken::Options

Inherits:
Object
  • Object
show all
Defined in:
lib/shoryuken/options.rb

Constant Summary collapse

DEFAULTS =
{
  concurrency: 25,
  queues: [],
  aws: {},
  delay: 0.0,
  timeout: 8,
  lifecycle_events: {
    startup: [],
    dispatch: [],
    utilization_update: [],
    quiet: [],
    shutdown: [],
    stopped: []
  }
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeOptions

Returns a new instance of Options.



25
26
27
28
29
30
31
32
33
34
# 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

Instance Attribute Details

#active_job_queue_name_prefixingObject

Returns the value of attribute active_job_queue_name_prefixing.



19
20
21
# File 'lib/shoryuken/options.rb', line 19

def active_job_queue_name_prefixing
  @active_job_queue_name_prefixing
end

#cache_visibility_timeoutObject

Returns the value of attribute cache_visibility_timeout.



19
20
21
# File 'lib/shoryuken/options.rb', line 19

def cache_visibility_timeout
  @cache_visibility_timeout
end

#default_worker_optionsObject



119
120
121
122
123
124
125
126
127
128
# 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

#exception_handlersObject

Returns the value of attribute exception_handlers.



19
20
21
# File 'lib/shoryuken/options.rb', line 19

def exception_handlers
  @exception_handlers
end

#groupsObject

Returns the value of attribute groups.



19
20
21
# File 'lib/shoryuken/options.rb', line 19

def groups
  @groups
end

#launcher_executorObject

Returns the value of attribute launcher_executor.



19
20
21
# File 'lib/shoryuken/options.rb', line 19

def launcher_executor
  @launcher_executor
end

#sqs_clientObject



79
80
81
# File 'lib/shoryuken/options.rb', line 79

def sqs_client
  @sqs_client ||= Aws::SQS::Client.new
end

#sqs_client_receive_message_optsObject

Returns the value of attribute sqs_client_receive_message_opts.



23
24
25
# File 'lib/shoryuken/options.rb', line 23

def sqs_client_receive_message_opts
  @sqs_client_receive_message_opts
end

#start_callbackObject

Returns the value of attribute start_callback.



19
20
21
# File 'lib/shoryuken/options.rb', line 19

def start_callback
  @start_callback
end

#stop_callbackObject

Returns the value of attribute stop_callback.



19
20
21
# File 'lib/shoryuken/options.rb', line 19

def stop_callback
  @stop_callback
end

#worker_executorObject

Returns the value of attribute worker_executor.



19
20
21
# File 'lib/shoryuken/options.rb', line 19

def worker_executor
  @worker_executor
end

#worker_registryObject

Returns the value of attribute worker_registry.



19
20
21
# File 'lib/shoryuken/options.rb', line 19

def worker_registry
  @worker_registry
end

Instance Method Details

#active_job?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/shoryuken/options.rb', line 36

def active_job?
  defined?(::ActiveJob)
end

#active_job_queue_name_prefixing?Boolean

Returns:

  • (Boolean)


161
162
163
# 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) ⇒ Object



40
41
42
43
44
45
46
47
48
49
# 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) ⇒ Object



51
52
53
54
55
# 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?Boolean

Returns:

  • (Boolean)


157
158
159
# File 'lib/shoryuken/options.rb', line 157

def cache_visibility_timeout?
  @cache_visibility_timeout
end

#client_middleware {|@_client_chain| ... } ⇒ Object

Yields:

  • (@_client_chain)


113
114
115
116
117
# 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| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



109
110
111
# File 'lib/shoryuken/options.rb', line 109

def configure_client
  yield self unless server?
end

#configure_server {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



99
100
101
# File 'lib/shoryuken/options.rb', line 99

def configure_server
  yield self if server?
end

#delay(group) ⇒ Object



75
76
77
# File 'lib/shoryuken/options.rb', line 75

def delay(group)
  groups[group].to_h.fetch(:delay, options[:delay]).to_f
end

#loggerObject



91
92
93
# File 'lib/shoryuken/options.rb', line 91

def logger
  Shoryuken::Logging.logger
end

#on(event, &block) ⇒ Object

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


146
147
148
149
150
151
# 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) ⇒ Object



130
131
132
# File 'lib/shoryuken/options.rb', line 130

def on_start(&block)
  self.start_callback = block
end

#on_stop(&block) ⇒ Object



134
135
136
# File 'lib/shoryuken/options.rb', line 134

def on_stop(&block)
  self.stop_callback = block
end

#optionsObject



87
88
89
# File 'lib/shoryuken/options.rb', line 87

def options
  @options ||= DEFAULTS.dup
end

#polling_strategy(group) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
# 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) ⇒ Object



95
96
97
# File 'lib/shoryuken/options.rb', line 95

def register_worker(*args)
  worker_registry.register_worker(*args)
end

#server?Boolean

Returns:

  • (Boolean)


153
154
155
# File 'lib/shoryuken/options.rb', line 153

def server?
  defined?(Shoryuken::CLI)
end

#server_middleware {|@_server_chain| ... } ⇒ Object

Yields:

  • (@_server_chain)


103
104
105
106
107
# File 'lib/shoryuken/options.rb', line 103

def server_middleware
  @_server_chain ||= default_server_middleware
  yield @_server_chain if block_given?
  @_server_chain
end

#ungrouped_queuesObject



57
58
59
# File 'lib/shoryuken/options.rb', line 57

def ungrouped_queues
  groups.values.flat_map { |options| options[:queues] }
end