shoryuken 7.0.0.alpha1 → 7.0.0.alpha2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (105) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/push.yml +3 -3
  3. data/.github/workflows/specs.yml +6 -9
  4. data/.github/workflows/verify-action-pins.yml +1 -1
  5. data/.rspec +2 -1
  6. data/.ruby-version +1 -1
  7. data/Appraisals +0 -6
  8. data/CHANGELOG.md +186 -142
  9. data/Gemfile +1 -0
  10. data/README.md +12 -13
  11. data/bin/cli/base.rb +1 -2
  12. data/bin/cli/sqs.rb +5 -4
  13. data/bin/shoryuken +2 -1
  14. data/gemfiles/rails_7_0.gemfile +10 -10
  15. data/gemfiles/rails_7_1.gemfile +10 -9
  16. data/gemfiles/rails_7_2.gemfile +10 -9
  17. data/gemfiles/rails_8_0.gemfile +10 -9
  18. data/lib/shoryuken/body_parser.rb +3 -1
  19. data/lib/shoryuken/client.rb +2 -0
  20. data/lib/shoryuken/default_exception_handler.rb +2 -0
  21. data/lib/shoryuken/default_worker_registry.rb +11 -11
  22. data/lib/shoryuken/environment_loader.rb +6 -6
  23. data/lib/shoryuken/extensions/active_job_adapter.rb +8 -6
  24. data/lib/shoryuken/extensions/active_job_concurrent_send_adapter.rb +5 -5
  25. data/lib/shoryuken/extensions/active_job_extensions.rb +2 -0
  26. data/lib/shoryuken/fetcher.rb +4 -2
  27. data/lib/shoryuken/helpers/atomic_boolean.rb +44 -0
  28. data/lib/shoryuken/helpers/atomic_counter.rb +104 -0
  29. data/lib/shoryuken/helpers/atomic_hash.rb +182 -0
  30. data/lib/shoryuken/helpers/hash_utils.rb +56 -0
  31. data/lib/shoryuken/helpers/string_utils.rb +65 -0
  32. data/lib/shoryuken/inline_message.rb +22 -0
  33. data/lib/shoryuken/launcher.rb +2 -0
  34. data/lib/shoryuken/logging.rb +19 -5
  35. data/lib/shoryuken/manager.rb +6 -4
  36. data/lib/shoryuken/message.rb +2 -0
  37. data/lib/shoryuken/middleware/chain.rb +2 -0
  38. data/lib/shoryuken/middleware/server/active_record.rb +2 -0
  39. data/lib/shoryuken/middleware/server/auto_delete.rb +2 -0
  40. data/lib/shoryuken/middleware/server/auto_extend_visibility.rb +10 -10
  41. data/lib/shoryuken/middleware/server/exponential_backoff_retry.rb +5 -3
  42. data/lib/shoryuken/middleware/server/timing.rb +2 -0
  43. data/lib/shoryuken/options.rb +9 -5
  44. data/lib/shoryuken/polling/base_strategy.rb +126 -0
  45. data/lib/shoryuken/polling/queue_configuration.rb +103 -0
  46. data/lib/shoryuken/polling/strict_priority.rb +2 -0
  47. data/lib/shoryuken/polling/weighted_round_robin.rb +2 -0
  48. data/lib/shoryuken/processor.rb +5 -2
  49. data/lib/shoryuken/queue.rb +6 -4
  50. data/lib/shoryuken/runner.rb +9 -12
  51. data/lib/shoryuken/util.rb +6 -6
  52. data/lib/shoryuken/version.rb +3 -1
  53. data/lib/shoryuken/worker/default_executor.rb +2 -0
  54. data/lib/shoryuken/worker/inline_executor.rb +3 -1
  55. data/lib/shoryuken/worker.rb +2 -0
  56. data/lib/shoryuken/worker_registry.rb +2 -0
  57. data/lib/shoryuken.rb +8 -28
  58. data/shoryuken.gemspec +6 -6
  59. data/spec/integration/launcher_spec.rb +2 -3
  60. data/spec/shared_examples_for_active_job.rb +13 -8
  61. data/spec/shoryuken/body_parser_spec.rb +1 -2
  62. data/spec/shoryuken/client_spec.rb +1 -1
  63. data/spec/shoryuken/default_exception_handler_spec.rb +9 -10
  64. data/spec/shoryuken/default_worker_registry_spec.rb +1 -2
  65. data/spec/shoryuken/environment_loader_spec.rb +9 -8
  66. data/spec/shoryuken/extensions/active_job_adapter_spec.rb +2 -1
  67. data/spec/shoryuken/extensions/active_job_base_spec.rb +2 -1
  68. data/spec/shoryuken/extensions/active_job_concurrent_send_adapter_spec.rb +2 -1
  69. data/spec/shoryuken/extensions/active_job_wrapper_spec.rb +2 -1
  70. data/spec/shoryuken/fetcher_spec.rb +23 -26
  71. data/spec/shoryuken/helpers/atomic_boolean_spec.rb +196 -0
  72. data/spec/shoryuken/helpers/atomic_counter_spec.rb +177 -0
  73. data/spec/shoryuken/helpers/atomic_hash_spec.rb +307 -0
  74. data/spec/shoryuken/helpers/hash_utils_spec.rb +145 -0
  75. data/spec/shoryuken/helpers/string_utils_spec.rb +124 -0
  76. data/spec/shoryuken/helpers_integration_spec.rb +96 -0
  77. data/spec/shoryuken/inline_message_spec.rb +196 -0
  78. data/spec/shoryuken/launcher_spec.rb +1 -2
  79. data/spec/shoryuken/manager_spec.rb +1 -2
  80. data/spec/shoryuken/middleware/chain_spec.rb +1 -1
  81. data/spec/shoryuken/middleware/server/auto_delete_spec.rb +1 -1
  82. data/spec/shoryuken/middleware/server/auto_extend_visibility_spec.rb +1 -1
  83. data/spec/shoryuken/middleware/server/exponential_backoff_retry_spec.rb +1 -1
  84. data/spec/shoryuken/middleware/server/timing_spec.rb +1 -1
  85. data/spec/shoryuken/options_spec.rb +4 -4
  86. data/spec/shoryuken/polling/base_strategy_spec.rb +280 -0
  87. data/spec/shoryuken/polling/queue_configuration_spec.rb +195 -0
  88. data/spec/shoryuken/polling/strict_priority_spec.rb +1 -1
  89. data/spec/shoryuken/polling/weighted_round_robin_spec.rb +1 -1
  90. data/spec/shoryuken/processor_spec.rb +1 -1
  91. data/spec/shoryuken/queue_spec.rb +2 -3
  92. data/spec/shoryuken/runner_spec.rb +1 -3
  93. data/spec/shoryuken/util_spec.rb +1 -1
  94. data/spec/shoryuken/worker/default_executor_spec.rb +1 -1
  95. data/spec/shoryuken/worker/inline_executor_spec.rb +1 -1
  96. data/spec/shoryuken/worker_spec.rb +15 -11
  97. data/spec/shoryuken_spec.rb +1 -1
  98. data/spec/spec_helper.rb +16 -0
  99. metadata +60 -27
  100. data/.github/FUNDING.yml +0 -12
  101. data/gemfiles/rails_6_1.gemfile +0 -18
  102. data/lib/shoryuken/core_ext.rb +0 -69
  103. data/lib/shoryuken/polling/base.rb +0 -67
  104. data/shoryuken.jpg +0 -0
  105. data/spec/shoryuken/core_ext_spec.rb +0 -40
@@ -0,0 +1,182 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Shoryuken
4
+ module Helpers
5
+ # A thread-safe hash implementation using Ruby's Mutex for all operations.
6
+ #
7
+ # This class provides a hash-like interface with thread-safe operations, serving as a
8
+ # drop-in replacement for Concurrent::Hash without requiring external dependencies.
9
+ # The implementation uses a single mutex to protect both read and write operations,
10
+ # ensuring complete thread safety across all Ruby implementations including JRuby.
11
+ #
12
+ # Since hash operations (lookup, assignment) are very fast, the mutex overhead is
13
+ # minimal while providing guaranteed safety and simplicity. This approach avoids
14
+ # the complexity of copy-on-write while maintaining excellent performance for
15
+ # typical usage patterns.
16
+ #
17
+ # @note This implementation uses mutex synchronization for all operations,
18
+ # ensuring complete thread safety with minimal performance impact.
19
+ #
20
+ # @note All operations are atomic and will never see partial effects from
21
+ # concurrent operations.
22
+ #
23
+ # @example Basic hash operations
24
+ # hash = Shoryuken::Helpers::AtomicHash.new
25
+ # hash['key'] = 'value'
26
+ # hash['key'] # => 'value'
27
+ # hash.keys # => ['key']
28
+ # hash.clear
29
+ # hash['key'] # => nil
30
+ #
31
+ # @example Worker registry usage
32
+ # @workers = Shoryuken::Helpers::AtomicHash.new
33
+ #
34
+ # # Registration (infrequent writes)
35
+ # @workers['queue_name'] = WorkerClass
36
+ #
37
+ # # Lookups (frequent reads)
38
+ # worker_class = @workers['queue_name']
39
+ # available_queues = @workers.keys
40
+ # worker_class = @workers.fetch('queue_name', DefaultWorker)
41
+ #
42
+ # @example Thread-safe concurrent access
43
+ # hash = Shoryuken::Helpers::AtomicHash.new
44
+ #
45
+ # # Multiple threads can safely write
46
+ # Thread.new { hash['key1'] = 'value1' }
47
+ # Thread.new { hash['key2'] = 'value2' }
48
+ #
49
+ # # Multiple threads can safely read concurrently
50
+ # Thread.new { puts hash['key1'] }
51
+ # Thread.new { puts hash.keys.size }
52
+ class AtomicHash
53
+ # Creates a new empty atomic hash.
54
+ #
55
+ # The hash starts empty and ready to accept key-value pairs through
56
+ # thread-safe operations.
57
+ #
58
+ # @return [AtomicHash] A new empty atomic hash instance
59
+ #
60
+ # @example Creating an empty hash
61
+ # hash = Shoryuken::Helpers::AtomicHash.new
62
+ # hash.keys # => []
63
+ def initialize
64
+ @mutex = Mutex.new
65
+ @hash = {}
66
+ end
67
+
68
+ # Returns the value associated with the given key.
69
+ #
70
+ # This operation is thread-safe and will return a consistent value
71
+ # even when called concurrently with write operations.
72
+ #
73
+ # @param key [Object] The key to look up
74
+ # @return [Object, nil] The value associated with the key, or nil if not found
75
+ #
76
+ # @example Reading values
77
+ # hash = Shoryuken::Helpers::AtomicHash.new
78
+ # hash['existing'] = 'value'
79
+ # hash['existing'] # => 'value'
80
+ # hash['missing'] # => nil
81
+ #
82
+ # @example Works with any key type
83
+ # hash = Shoryuken::Helpers::AtomicHash.new
84
+ # hash[:symbol] = 'symbol_value'
85
+ # hash[42] = 'number_value'
86
+ # hash[:symbol] # => 'symbol_value'
87
+ # hash[42] # => 'number_value'
88
+ def [](key)
89
+ @mutex.synchronize { @hash[key] }
90
+ end
91
+
92
+ # Sets the value for the given key.
93
+ #
94
+ # This is a thread-safe write operation that ensures data integrity
95
+ # when called concurrently with other read or write operations.
96
+ #
97
+ # @param key [Object] The key to set
98
+ # @param value [Object] The value to associate with the key
99
+ # @return [Object] The assigned value
100
+ #
101
+ # @example Setting values
102
+ # hash = Shoryuken::Helpers::AtomicHash.new
103
+ # hash['queue1'] = 'Worker1'
104
+ # hash['queue2'] = 'Worker2'
105
+ # hash['queue1'] # => 'Worker1'
106
+ #
107
+ # @example Overwriting values
108
+ # hash = Shoryuken::Helpers::AtomicHash.new
109
+ # hash['key'] = 'old_value'
110
+ # hash['key'] = 'new_value'
111
+ # hash['key'] # => 'new_value'
112
+ def []=(key, value)
113
+ @mutex.synchronize { @hash[key] = value }
114
+ end
115
+
116
+ # Removes all key-value pairs from the hash.
117
+ #
118
+ # This is a thread-safe write operation that ensures atomicity
119
+ # when called concurrently with other operations.
120
+ #
121
+ # @return [Hash] An empty hash (for compatibility with standard Hash#clear)
122
+ #
123
+ # @example Clearing all entries
124
+ # hash = Shoryuken::Helpers::AtomicHash.new
125
+ # hash['key1'] = 'value1'
126
+ # hash['key2'] = 'value2'
127
+ # hash.keys.size # => 2
128
+ # hash.clear
129
+ # hash.keys.size # => 0
130
+ # hash['key1'] # => nil
131
+ def clear
132
+ @mutex.synchronize { @hash.clear }
133
+ end
134
+
135
+ # Returns an array of all keys in the hash.
136
+ #
137
+ # This operation is thread-safe and will return a consistent snapshot
138
+ # of keys even when called concurrently with write operations.
139
+ #
140
+ # @return [Array] An array containing all keys in the hash
141
+ #
142
+ # @example Getting all keys
143
+ # hash = Shoryuken::Helpers::AtomicHash.new
144
+ # hash['queue1'] = 'Worker1'
145
+ # hash['queue2'] = 'Worker2'
146
+ # hash.keys # => ['queue1', 'queue2'] (order not guaranteed)
147
+ #
148
+ # @example Empty hash returns empty array
149
+ # hash = Shoryuken::Helpers::AtomicHash.new
150
+ # hash.keys # => []
151
+ def keys
152
+ @mutex.synchronize { @hash.keys }
153
+ end
154
+
155
+ # Returns the value for the given key, or a default value if the key is not found.
156
+ #
157
+ # This operation is thread-safe and will return a consistent value
158
+ # even when called concurrently with write operations.
159
+ #
160
+ # @param key [Object] The key to look up
161
+ # @param default [Object] The value to return if the key is not found
162
+ # @return [Object] The value associated with the key, or the default value
163
+ #
164
+ # @example Fetching with defaults
165
+ # hash = Shoryuken::Helpers::AtomicHash.new
166
+ # hash['existing'] = 'found'
167
+ # hash.fetch('existing', 'default') # => 'found'
168
+ # hash.fetch('missing', 'default') # => 'default'
169
+ #
170
+ # @example Default parameter is optional
171
+ # hash = Shoryuken::Helpers::AtomicHash.new
172
+ # hash.fetch('missing') # => nil
173
+ #
174
+ # @example Useful for providing fallback collections
175
+ # hash = Shoryuken::Helpers::AtomicHash.new
176
+ # workers = hash.fetch('queue_name', []) # => [] if not found
177
+ def fetch(key, default = nil)
178
+ @mutex.synchronize { @hash.fetch(key, default) }
179
+ end
180
+ end
181
+ end
182
+ end
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Shoryuken
4
+ module Helpers
5
+ # Utility methods for hash manipulation.
6
+ #
7
+ # This module provides helper methods for common hash operations that were
8
+ # previously implemented as core class extensions. By using a dedicated
9
+ # helper module, we avoid polluting the global namespace while maintaining
10
+ # the same functionality.
11
+ #
12
+ # @example Basic usage
13
+ # hash = { 'key1' => 'value1', 'key2' => { 'nested' => 'value2' } }
14
+ # symbolized = Shoryuken::Helpers::HashUtils.deep_symbolize_keys(hash)
15
+ # # => { key1: 'value1', key2: { nested: 'value2' } }
16
+ module HashUtils
17
+ class << self
18
+ # Recursively converts hash keys to symbols.
19
+ #
20
+ # This method traverses a hash structure and converts all string keys
21
+ # to symbols, including nested hashes. Non-hash values are left unchanged.
22
+ # This is useful for normalizing configuration data loaded from YAML files.
23
+ #
24
+ # @param hash [Hash, Object] The hash to convert, or any other object
25
+ # @return [Hash, Object] Hash with symbolized keys, or the original object if not a hash
26
+ #
27
+ # @example Converting a simple hash
28
+ # hash = { 'key1' => 'value1', 'key2' => 'value2' }
29
+ # HashUtils.deep_symbolize_keys(hash)
30
+ # # => { key1: 'value1', key2: 'value2' }
31
+ #
32
+ # @example Converting a nested hash
33
+ # hash = { 'config' => { 'timeout' => 30, 'retries' => 3 } }
34
+ # HashUtils.deep_symbolize_keys(hash)
35
+ # # => { config: { timeout: 30, retries: 3 } }
36
+ #
37
+ # @example Handling non-hash input gracefully
38
+ # HashUtils.deep_symbolize_keys('not a hash')
39
+ # # => 'not a hash'
40
+ #
41
+ # @example Mixed value types
42
+ # hash = { 'string' => 'value', 'number' => 42, 'nested' => { 'bool' => true } }
43
+ # HashUtils.deep_symbolize_keys(hash)
44
+ # # => { string: 'value', number: 42, nested: { bool: true } }
45
+ def deep_symbolize_keys(hash)
46
+ return hash unless hash.is_a?(Hash)
47
+
48
+ hash.each_with_object({}) do |(key, value), result|
49
+ symbol_key = key.is_a?(String) ? key.to_sym : key
50
+ result[symbol_key] = value.is_a?(Hash) ? deep_symbolize_keys(value) : value
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Shoryuken
4
+ module Helpers
5
+ # Utility methods for string manipulation.
6
+ #
7
+ # This module provides helper methods for common string operations that were
8
+ # previously implemented as core class extensions. By using a dedicated
9
+ # helper module, we avoid polluting the global namespace while maintaining
10
+ # the same functionality.
11
+ #
12
+ # @example Basic usage
13
+ # klass = Shoryuken::Helpers::StringUtils.constantize('MyWorker')
14
+ # # => MyWorker
15
+ module StringUtils
16
+ class << self
17
+ # Converts a string to a constant.
18
+ #
19
+ # This method takes a string representation of a constant name and returns
20
+ # the actual constant. It handles nested constants (e.g., 'Foo::Bar') and
21
+ # leading double colons (e.g., '::Object'). This is commonly used for
22
+ # dynamically loading worker classes from configuration.
23
+ #
24
+ # @param string [String] The string to convert to a constant
25
+ # @return [Class, Module] The constant represented by the string
26
+ # @raise [NameError] if the constant is not found or not defined
27
+ #
28
+ # @example Converting a simple class name
29
+ # StringUtils.constantize('String')
30
+ # # => String
31
+ #
32
+ # @example Converting a nested constant
33
+ # StringUtils.constantize('Shoryuken::Worker')
34
+ # # => Shoryuken::Worker
35
+ #
36
+ # @example Handling leading double colon
37
+ # StringUtils.constantize('::Object')
38
+ # # => Object
39
+ #
40
+ # @example Worker class loading
41
+ # worker_class = StringUtils.constantize('MyApp::EmailWorker')
42
+ # worker_instance = worker_class.new
43
+ #
44
+ # @example Error handling
45
+ # begin
46
+ # StringUtils.constantize('NonExistentClass')
47
+ # rescue NameError => e
48
+ # puts "Class not found: #{e.message}"
49
+ # end
50
+ def constantize(string)
51
+ names = string.split('::')
52
+ names.shift if names.empty? || names.first.empty?
53
+
54
+ constant = Object
55
+
56
+ names.each do |name|
57
+ constant = constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name)
58
+ end
59
+
60
+ constant
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Shoryuken
4
+ # A high-performance alternative to OpenStruct for representing SQS messages.
5
+ #
6
+ # InlineMessage is a Struct-based implementation that provides the same interface
7
+ # as the previous OpenStruct-based message representation but with significantly
8
+ # better performance characteristics. It contains all the essential attributes
9
+ # needed to represent an Amazon SQS message within the Shoryuken framework.
10
+ InlineMessage = Struct.new(
11
+ :body,
12
+ :attributes,
13
+ :md5_of_body,
14
+ :md5_of_message_attributes,
15
+ :message_attributes,
16
+ :message_id,
17
+ :receipt_handle,
18
+ :delete,
19
+ :queue_name,
20
+ keyword_init: true
21
+ )
22
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Shoryuken
2
4
  class Launcher
3
5
  include Util
@@ -1,12 +1,13 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'time'
2
4
  require 'logger'
3
5
 
4
6
  module Shoryuken
5
7
  module Logging
6
- class Pretty < Logger::Formatter
7
- # Provide a call() method that returns the formatted message.
8
- def call(severity, time, _program_name, message)
9
- "#{time.utc.iso8601} #{Process.pid} TID-#{Thread.current.object_id.to_s(36)}#{context} #{severity}: #{message}\n"
8
+ class Base < ::Logger::Formatter
9
+ def tid
10
+ Thread.current['shoryuken_tid'] ||= (Thread.current.object_id ^ ::Process.pid).to_s(36)
10
11
  end
11
12
 
12
13
  def context
@@ -15,6 +16,19 @@ module Shoryuken
15
16
  end
16
17
  end
17
18
 
19
+ class Pretty < Base
20
+ # Provide a call() method that returns the formatted message.
21
+ def call(severity, time, _program_name, message)
22
+ "#{time.utc.iso8601} #{Process.pid} TID-#{tid}#{context} #{severity}: #{message}\n"
23
+ end
24
+ end
25
+
26
+ class WithoutTimestamp < Base
27
+ def call(severity, _time, _program_name, message)
28
+ "pid=#{Process.pid} tid=#{tid}#{context} #{severity}: #{message}\n"
29
+ end
30
+ end
31
+
18
32
  def self.with_context(msg)
19
33
  Thread.current[:shoryuken_context] = msg
20
34
  yield
@@ -34,7 +48,7 @@ module Shoryuken
34
48
  end
35
49
 
36
50
  def self.logger=(log)
37
- @logger = (log || Logger.new('/dev/null'))
51
+ @logger = log || Logger.new('/dev/null')
38
52
  end
39
53
  end
40
54
  end
@@ -1,9 +1,11 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Shoryuken
2
4
  class Manager
3
5
  include Util
4
6
 
5
7
  BATCH_LIMIT = 10
6
- # See https://github.com/phstc/shoryuken/issues/348#issuecomment-292847028
8
+ # See https://github.com/ruby-shoryuken/shoryuken/issues/348#issuecomment-292847028
7
9
  MIN_DISPATCH_INTERVAL = 0.1
8
10
 
9
11
  attr_reader :group
@@ -13,10 +15,10 @@ module Shoryuken
13
15
  @fetcher = fetcher
14
16
  @polling_strategy = polling_strategy
15
17
  @max_processors = concurrency
16
- @busy_processors = Concurrent::AtomicFixnum.new(0)
18
+ @busy_processors = Shoryuken::Helpers::AtomicCounter.new(0)
17
19
  @executor = executor
18
- @running = Concurrent::AtomicBoolean.new(true)
19
- @stop_new_dispatching = Concurrent::AtomicBoolean.new(false)
20
+ @running = Shoryuken::Helpers::AtomicBoolean.new(true)
21
+ @stop_new_dispatching = Shoryuken::Helpers::AtomicBoolean.new(false)
20
22
  @dispatching_release_signal = ::Queue.new
21
23
  end
22
24
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Shoryuken
2
4
  class Message
3
5
  extend Forwardable
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Shoryuken
2
4
  # Middleware is code configured to run before/after
3
5
  # a message is processed. It is patterned after Rack
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Shoryuken
2
4
  module Middleware
3
5
  module Server
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Shoryuken
2
4
  module Middleware
3
5
  module Server
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Shoryuken
2
4
  module Middleware
3
5
  module Server
@@ -29,16 +31,14 @@ module Shoryuken
29
31
  queue_visibility_timeout = Shoryuken::Client.queues(queue).visibility_timeout
30
32
 
31
33
  Concurrent::TimerTask.new(execution_interval: queue_visibility_timeout - EXTEND_UPFRONT_SECONDS) do
32
- begin
33
- logger.debug do
34
- "Extending message #{queue}/#{sqs_msg.message_id} visibility timeout by #{queue_visibility_timeout}s"
35
- end
36
-
37
- sqs_msg.change_visibility(visibility_timeout: queue_visibility_timeout)
38
- rescue => ex
39
- logger.error do
40
- "Could not auto extend the message #{queue}/#{sqs_msg.message_id} visibility timeout. Error: #{ex.message}"
41
- end
34
+ logger.debug do
35
+ "Extending message #{queue}/#{sqs_msg.message_id} visibility timeout by #{queue_visibility_timeout}s"
36
+ end
37
+
38
+ sqs_msg.change_visibility(visibility_timeout: queue_visibility_timeout)
39
+ rescue => e
40
+ logger.error do
41
+ "Could not auto extend the message #{queue}/#{sqs_msg.message_id} visibility timeout. Error: #{e.message}"
42
42
  end
43
43
  end
44
44
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Shoryuken
2
4
  module Middleware
3
5
  module Server
@@ -14,7 +16,7 @@ module Shoryuken
14
16
 
15
17
  started_at = Time.now
16
18
  yield
17
- rescue => ex
19
+ rescue => e
18
20
  retry_intervals = worker.class.get_shoryuken_options['retry_intervals']
19
21
 
20
22
  if retry_intervals.nil? || !handle_failure(sqs_msg, started_at, retry_intervals)
@@ -23,9 +25,9 @@ module Shoryuken
23
25
  raise
24
26
  end
25
27
 
26
- logger.warn { "Message #{sqs_msg.message_id} will attempt retry due to error: #{ex.message}" }
28
+ logger.warn { "Message #{sqs_msg.message_id} will attempt retry due to error: #{e.message}" }
27
29
  # since we didn't raise, lets log the backtrace for debugging purposes.
28
- logger.debug { ex.backtrace.join("\n") } unless ex.backtrace.nil?
30
+ logger.debug { e.backtrace.join("\n") } unless e.backtrace.nil?
29
31
  end
30
32
 
31
33
  private
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Shoryuken
2
4
  module Middleware
3
5
  module Server
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Shoryuken
2
4
  class Options
3
5
  DEFAULTS = {
@@ -17,10 +19,12 @@ module Shoryuken
17
19
  }
18
20
  }.freeze
19
21
 
20
- attr_accessor :active_job_queue_name_prefixing, :cache_visibility_timeout, :groups,
21
- :launcher_executor, :reloader, :enable_reloading,
22
- :start_callback, :stop_callback, :worker_executor, :worker_registry, :exception_handlers
23
- attr_writer :default_worker_options, :sqs_client
22
+ attr_accessor :active_job_queue_name_prefixing, :cache_visibility_timeout,
23
+ :groups, :launcher_executor, :reloader, :enable_reloading,
24
+ :start_callback, :stop_callback, :worker_executor, :worker_registry,
25
+ :exception_handlers
26
+
27
+ attr_writer :default_worker_options, :sqs_client, :logger
24
28
  attr_reader :sqs_client_receive_message_opts
25
29
 
26
30
  def initialize
@@ -96,7 +100,7 @@ module Shoryuken
96
100
  end
97
101
 
98
102
  def logger
99
- Shoryuken::Logging.logger
103
+ @logger ||= Shoryuken::Logging.logger
100
104
  end
101
105
 
102
106
  def thread_priority
@@ -0,0 +1,126 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Shoryuken
4
+ module Polling
5
+ # Abstract base class for queue polling strategies.
6
+ #
7
+ # This class defines the interface that all polling strategies must implement
8
+ # to manage queue selection and message flow control in Shoryuken workers.
9
+ # Polling strategies determine which queue to fetch messages from next and
10
+ # how to handle scenarios where queues have no messages available.
11
+ #
12
+ # @abstract Subclass and override {#next_queue}, {#messages_found}, and {#active_queues}
13
+ # to implement a custom polling strategy.
14
+ #
15
+ # @example Implementing a custom polling strategy
16
+ # class CustomStrategy < BaseStrategy
17
+ # def initialize(queues)
18
+ # @queues = queues
19
+ # end
20
+ #
21
+ # def next_queue
22
+ # # Return next queue to poll
23
+ # @queues.sample
24
+ # end
25
+ #
26
+ # def messages_found(queue, count)
27
+ # # Handle result of polling
28
+ # logger.info "Found #{count} messages in #{queue}"
29
+ # end
30
+ #
31
+ # def active_queues
32
+ # # Return list of active queues
33
+ # @queues
34
+ # end
35
+ # end
36
+ #
37
+ # @see WeightedRoundRobin
38
+ # @see StrictPriority
39
+ class BaseStrategy
40
+ include Util
41
+
42
+ # Returns the next queue to poll for messages.
43
+ #
44
+ # This method should return a QueueConfiguration object representing
45
+ # the next queue that should be polled for messages, or nil if no
46
+ # queues are currently available for polling.
47
+ #
48
+ # @abstract Subclasses must implement this method
49
+ # @return [QueueConfiguration, nil] Next queue to poll, or nil if none available
50
+ # @raise [NotImplementedError] if not implemented by subclass
51
+ def next_queue
52
+ fail NotImplementedError
53
+ end
54
+
55
+ # Called when messages are found (or not found) in a queue.
56
+ #
57
+ # This method is invoked after polling a queue to inform the strategy
58
+ # about the number of messages that were retrieved. Strategies can use
59
+ # this information to make decisions about future polling behavior,
60
+ # such as pausing empty queues or adjusting queue weights.
61
+ #
62
+ # @abstract Subclasses must implement this method
63
+ # @param _queue [String] The name of the queue that was polled
64
+ # @param _messages_found [Integer] The number of messages retrieved
65
+ # @raise [NotImplementedError] if not implemented by subclass
66
+ def messages_found(_queue, _messages_found)
67
+ fail NotImplementedError
68
+ end
69
+
70
+ # Called when a message from a queue has been processed.
71
+ #
72
+ # This optional callback is invoked after a message has been successfully
73
+ # processed by a worker. Strategies can use this information for cleanup
74
+ # or to adjust their polling behavior.
75
+ #
76
+ # @param _queue [String] The name of the queue whose message was processed
77
+ # @return [void]
78
+ def message_processed(_queue); end
79
+
80
+ # Returns the list of currently active queues.
81
+ #
82
+ # This method should return an array representing the queues that are
83
+ # currently active and available for polling. The format may vary by
84
+ # strategy implementation.
85
+ #
86
+ # @abstract Subclasses must implement this method
87
+ # @return [Array] List of active queues
88
+ # @raise [NotImplementedError] if not implemented by subclass
89
+ def active_queues
90
+ fail NotImplementedError
91
+ end
92
+
93
+ # Compares this strategy with another object for equality.
94
+ #
95
+ # Two strategies are considered equal if they have the same active queues.
96
+ # This method also supports comparison with Array objects for backward
97
+ # compatibility.
98
+ #
99
+ # @param other [Object] Object to compare with
100
+ # @return [Boolean] true if strategies are equivalent
101
+ def ==(other)
102
+ case other
103
+ when Array
104
+ @queues == other
105
+ else
106
+ if other.respond_to?(:active_queues)
107
+ active_queues == other.active_queues
108
+ else
109
+ false
110
+ end
111
+ end
112
+ end
113
+
114
+ # Returns the delay time for pausing empty queues.
115
+ #
116
+ # This method returns the amount of time (in seconds) that empty queues
117
+ # should be paused before being polled again. The delay can be set at
118
+ # the strategy level or falls back to the global Shoryuken delay setting.
119
+ #
120
+ # @return [Float] Delay time in seconds
121
+ def delay
122
+ @delay || Shoryuken.options[:delay].to_f
123
+ end
124
+ end
125
+ end
126
+ end