Class: Shoryuken::Options

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

Constant Summary collapse

DEFAULTS =
{
  concurrency: 25,
  queues: [],
  aws: {},
  delay: 0,
  timeout: 8,
  lifecycle_events: {
    startup: [],
    dispatch: [],
    quiet: [],
    shutdown: []
  }
}.freeze
@@groups =
{}
@@worker_registry =
DefaultWorkerRegistry.new
@@active_job_queue_name_prefixing =
false
@@sqs_client =
nil
@@sqs_client_receive_message_opts =
{}
@@start_callback =
nil
@@stop_callback =
nil
@@worker_executor =
Worker::DefaultExecutor

Class Method Summary collapse

Class Method Details

.active_job?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/shoryuken/options.rb', line 27

def active_job?
  defined?(::ActiveJob)
end

.active_job_queue_name_prefixingObject



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

def active_job_queue_name_prefixing
  @@active_job_queue_name_prefixing
end

.active_job_queue_name_prefixing=(active_job_queue_name_prefixing) ⇒ Object



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

def active_job_queue_name_prefixing=(active_job_queue_name_prefixing)
  @@active_job_queue_name_prefixing = active_job_queue_name_prefixing
end

.add_group(group, concurrency) ⇒ Object



31
32
33
34
35
36
# File 'lib/shoryuken/options.rb', line 31

def add_group(group, concurrency)
  groups[group] ||= {
    concurrency: concurrency,
    queues: []
  }
end

.add_queue(queue, weight, group) ⇒ Object



42
43
44
45
46
# File 'lib/shoryuken/options.rb', line 42

def add_queue(queue, weight, group)
  weight.times do
    groups[group][:queues] << queue
  end
end

.client_middleware {|@@client_chain| ... } ⇒ Object

Yields:

  • (@@client_chain)


149
150
151
152
153
# File 'lib/shoryuken/options.rb', line 149

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:



145
146
147
# File 'lib/shoryuken/options.rb', line 145

def configure_client
  yield self unless server?
end

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

Yields:

  • (_self)

Yield Parameters:



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

def configure_server
  yield self if server?
end

.default_worker_optionsObject



155
156
157
158
159
160
161
162
163
164
# File 'lib/shoryuken/options.rb', line 155

def default_worker_options
  @@default_worker_options ||= {
    'queue'                   => 'default',
    'delete'                  => false,
    'auto_delete'             => false,
    'auto_visibility_timeout' => false,
    'retry_intervals'         => nil,
    'batch'                   => false
  }
end

.default_worker_options=(default_worker_options) ⇒ Object



166
167
168
# File 'lib/shoryuken/options.rb', line 166

def default_worker_options=(default_worker_options)
  @@default_worker_options = default_worker_options
end

.groupsObject



38
39
40
# File 'lib/shoryuken/options.rb', line 38

def groups
  @@groups
end

.loggerObject



127
128
129
# File 'lib/shoryuken/options.rb', line 127

def logger
  Shoryuken::Logging.logger
end

.on(event, &block) ⇒ Object

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

Shoryuken.configure_server do |config|
  config.on(:shutdown) do
    puts "Goodbye cruel world!"
  end
end


186
187
188
189
190
# File 'lib/shoryuken/options.rb', line 186

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



170
171
172
# File 'lib/shoryuken/options.rb', line 170

def on_start(&block)
  @@start_callback = block
end

.on_stop(&block) ⇒ Object



174
175
176
# File 'lib/shoryuken/options.rb', line 174

def on_stop(&block)
  @@stop_callback = block
end

.optionsObject



123
124
125
# File 'lib/shoryuken/options.rb', line 123

def options
  @@options ||= DEFAULTS.dup
end

.polling_strategy(group) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/shoryuken/options.rb', line 68

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



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

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

.server_middleware {|@@server_chain| ... } ⇒ Object

Yields:

  • (@@server_chain)


139
140
141
142
143
# File 'lib/shoryuken/options.rb', line 139

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

.sqs_clientObject



107
108
109
# File 'lib/shoryuken/options.rb', line 107

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

.sqs_client=(sqs_client) ⇒ Object



111
112
113
# File 'lib/shoryuken/options.rb', line 111

def sqs_client=(sqs_client)
  @@sqs_client = sqs_client
end

.sqs_client_receive_message_optsObject



115
116
117
# File 'lib/shoryuken/options.rb', line 115

def sqs_client_receive_message_opts
  @@sqs_client_receive_message_opts
end

.sqs_client_receive_message_opts=(sqs_client_receive_message_opts) ⇒ Object



119
120
121
# File 'lib/shoryuken/options.rb', line 119

def sqs_client_receive_message_opts=(sqs_client_receive_message_opts)
  @@sqs_client_receive_message_opts['default'] = sqs_client_receive_message_opts
end

.start_callbackObject



83
84
85
# File 'lib/shoryuken/options.rb', line 83

def start_callback
  @@start_callback
end

.start_callback=(start_callback) ⇒ Object



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

def start_callback=(start_callback)
  @@start_callback = start_callback
end

.stop_callbackObject



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

def stop_callback
  @@stop_callback
end

.stop_callback=(stop_callback) ⇒ Object



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

def stop_callback=(stop_callback)
  @@stop_callback = stop_callback
end

.ungrouped_queuesObject



48
49
50
# File 'lib/shoryuken/options.rb', line 48

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

.worker_executorObject



60
61
62
# File 'lib/shoryuken/options.rb', line 60

def worker_executor
  @@worker_executor
end

.worker_executor=(worker_executor) ⇒ Object



64
65
66
# File 'lib/shoryuken/options.rb', line 64

def worker_executor=(worker_executor)
  @@worker_executor = worker_executor
end

.worker_registryObject



52
53
54
# File 'lib/shoryuken/options.rb', line 52

def worker_registry
  @@worker_registry
end

.worker_registry=(worker_registry) ⇒ Object



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

def worker_registry=(worker_registry)
  @@worker_registry = worker_registry
end