Module: Shoryuken::Worker::ClassMethods
- Defined in:
- lib/shoryuken/worker.rb
Overview
Class methods added to classes that include Shoryuken::Worker. Provides methods for configuring the worker, enqueueing jobs, and managing middleware.
Instance Method Summary collapse
-
#auto_delete? ⇒ Boolean
Checks if automatic message deletion is enabled for this worker.
-
#auto_visibility_timeout? ⇒ Boolean
Checks if automatic visibility timeout extension is enabled for this worker.
-
#exponential_backoff? ⇒ Boolean
Checks if exponential backoff retry is configured for this worker.
-
#get_shoryuken_options ⇒ Hash
Returns the shoryuken options for this worker class.
-
#perform_async(body, options = {}) ⇒ String
Enqueues a job to be processed asynchronously by a Shoryuken worker.
-
#perform_in(interval, body, options = {}) ⇒ String
(also: #perform_at)
Enqueues a job to be processed after a specified time interval.
-
#server_middleware {|Shoryuken::Middleware::Chain| ... } ⇒ Shoryuken::Middleware::Chain
Configures server-side middleware chain for this worker class.
-
#shoryuken_class_attribute(*attrs) ⇒ void
Defines inheritable class attributes for workers.
-
#shoryuken_options(opts = {}) ⇒ Object
Configures worker options including queue assignment, processing behavior, and SQS-specific settings.
-
#stringify_keys(hash) ⇒ Hash
Converts hash keys to strings.
Instance Method Details
#auto_delete? ⇒ Boolean
Checks if automatic message deletion is enabled for this worker. When enabled, successfully processed messages are automatically deleted from the SQS queue. When disabled, you must manually delete messages or they will become visible again after the visibility timeout.
249 250 251 |
# File 'lib/shoryuken/worker.rb', line 249 def auto_delete? !!(['delete'] || ['auto_delete']) end |
#auto_visibility_timeout? ⇒ Boolean
Checks if automatic visibility timeout extension is enabled for this worker. When enabled, Shoryuken automatically extends the message visibility timeout during processing to prevent the message from becoming visible to other consumers.
215 216 217 |
# File 'lib/shoryuken/worker.rb', line 215 def auto_visibility_timeout? !!['auto_visibility_timeout'] end |
#exponential_backoff? ⇒ Boolean
Checks if exponential backoff retry is configured for this worker. When retry intervals are specified, failed jobs will be retried with increasing delays between attempts.
230 231 232 |
# File 'lib/shoryuken/worker.rb', line 230 def exponential_backoff? !!['retry_intervals'] end |
#get_shoryuken_options ⇒ Hash
Returns the shoryuken options for this worker class
255 256 257 |
# File 'lib/shoryuken/worker.rb', line 255 def # :nodoc: || Shoryuken. end |
#perform_async(body, options = {}) ⇒ String
Enqueues a job to be processed asynchronously by a Shoryuken worker.
71 72 73 |
# File 'lib/shoryuken/worker.rb', line 71 def perform_async(body, = {}) Shoryuken.worker_executor.perform_async(self, body, ) end |
#perform_in(interval, body, options = {}) ⇒ String Also known as: perform_at
Enqueues a job to be processed after a specified time interval.
90 91 92 |
# File 'lib/shoryuken/worker.rb', line 90 def perform_in(interval, body, = {}) Shoryuken.worker_executor.perform_in(self, interval, body, ) end |
#server_middleware {|Shoryuken::Middleware::Chain| ... } ⇒ Shoryuken::Middleware::Chain
Configures server-side middleware chain for this worker class. Middleware runs before and after job processing, similar to Rack middleware.
111 112 113 114 115 |
# File 'lib/shoryuken/worker.rb', line 111 def server_middleware @_server_chain ||= Shoryuken.server_middleware.dup yield @_server_chain if block_given? @_server_chain end |
#shoryuken_class_attribute(*attrs) ⇒ void
This method returns an undefined value.
Defines inheritable class attributes for workers
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 |
# File 'lib/shoryuken/worker.rb', line 271 def shoryuken_class_attribute(*attrs) # :nodoc: attrs.each do |name| singleton_class.instance_eval do undef_method(name) if method_defined?(name) || private_method_defined?(name) end define_singleton_method(name) { nil } ivar = "@#{name}" singleton_class.instance_eval do m = "#{name}=" undef_method(m) if method_defined?(m) || private_method_defined?(m) end define_singleton_method("#{name}=") do |val| singleton_class.class_eval do undef_method(name) if method_defined?(name) || private_method_defined?(name) define_method(name) { val } end # singleton? backwards compatibility for ruby < 2.1 singleton_klass = respond_to?(:singleton?) ? singleton? : self != ancestors.first if singleton_klass class_eval do undef_method(name) if method_defined?(name) || private_method_defined?(name) define_method(name) do if instance_variable_defined? ivar instance_variable_get ivar else singleton_class.send name end end end end val end # instance reader undef_method(name) if method_defined?(name) || private_method_defined?(name) define_method(name) do if instance_variable_defined?(ivar) instance_variable_get ivar else self.class.public_send name end end # instance writer m = "#{name}=" undef_method(m) if method_defined?(m) || private_method_defined?(m) attr_writer name end end |
#shoryuken_options(opts = {}) ⇒ Object
Configures worker options including queue assignment, processing behavior, and SQS-specific settings. This is the main configuration method for workers.
203 204 205 206 |
# File 'lib/shoryuken/worker.rb', line 203 def (opts = {}) self. = .merge(stringify_keys(opts || {})) normalize_worker_queue! end |
#stringify_keys(hash) ⇒ Hash
Converts hash keys to strings
262 263 264 265 266 |
# File 'lib/shoryuken/worker.rb', line 262 def stringify_keys(hash) # :nodoc: new_hash = {} hash.each { |key, value| new_hash[key.to_s] = value } new_hash end |