Skip to content

Commit 852a9f6

Browse files
authored
Merge branch 'main' into instrumentation-system
2 parents ca60c39 + 24bb7fb commit 852a9f6

File tree

10 files changed

+65
-22
lines changed

10 files changed

+65
-22
lines changed

CHANGELOG.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,17 @@
11
## [7.0.0] - Unreleased
2+
- Breaking: Add `Shoryuken::Errors` module with domain-specific error classes
3+
- Introduces `Shoryuken::Errors::BaseError` as base class for all Shoryuken errors
4+
- `Shoryuken::Errors::QueueNotFoundError` for non-existent or inaccessible SQS queues
5+
- `Shoryuken::Errors::InvalidConfigurationError` for configuration validation failures
6+
- `Shoryuken::Errors::InvalidWorkerRegistrationError` for worker registration conflicts
7+
- `Shoryuken::Errors::InvalidPollingStrategyError` for invalid polling strategy configuration
8+
- `Shoryuken::Errors::InvalidEventError` for invalid lifecycle event names
9+
- `Shoryuken::Errors::InvalidDelayError` for delays exceeding SQS 15-minute maximum
10+
- `Shoryuken::Errors::InvalidArnError` for invalid ARN format
11+
- `Shoryuken::Errors::Shutdown` for graceful shutdown (replaces `Shoryuken::Shutdown`)
12+
- Replaces generic Ruby exceptions (ArgumentError, RuntimeError) with specific error types
13+
- Code rescuing `Shoryuken::Shutdown` must change to `Shoryuken::Errors::Shutdown`
14+
215
- Fix: Raise ArgumentError when using delay with FIFO queues
316
- FIFO queues do not support per-message DelaySeconds
417
- Previously caused confusing AWS errors with ActiveJob's `retry_on` (Rails 6.1+ defaults to `wait: 3.seconds`)

lib/shoryuken/default_worker_registry.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@ def queues
5656
# @param queue [String] the queue name
5757
# @param clazz [Class] the worker class to register
5858
# @return [Class] the registered worker class
59-
# @raise [ArgumentError] if a batchable worker is already registered for the queue
59+
# @raise [Errors::InvalidWorkerRegistrationError] if a batchable worker is already registered for the queue
6060
def register_worker(queue, clazz)
6161
if (worker_class = @workers[queue]) && (worker_class.get_shoryuken_options['batch'] == true || clazz.get_shoryuken_options['batch'] == true)
62-
fail ArgumentError, "Could not register #{clazz} for #{queue}, "\
62+
raise Errors::InvalidWorkerRegistrationError, "Could not register #{clazz} for #{queue}, "\
6363
"because #{worker_class} is already registered for this queue, "\
6464
"and Shoryuken doesn't support a batchable worker for a queue with multiple workers"
6565
end

lib/shoryuken/environment_loader.rb

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def initialize_options
8080
def config_file_options
8181
return {} unless (path = options[:config_file])
8282

83-
fail ArgumentError, "The supplied config file #{path} does not exist" unless File.exist?(path)
83+
raise Errors::InvalidConfigurationError, "The supplied config file #{path} does not exist" unless File.exist?(path)
8484

8585
if (result = YAML.load(ERB.new(IO.read(path)).result))
8686
Shoryuken::Helpers::HashUtils.deep_symbolize_keys(result)
@@ -248,10 +248,7 @@ def validate_queues
248248
It's also possible that you don't have permission to access the specified queues.
249249
MSG
250250

251-
fail(
252-
ArgumentError,
253-
error_msg
254-
)
251+
raise Errors::QueueNotFoundError, error_msg
255252
end
256253

257254
# Validates that all queues have registered workers

lib/shoryuken/errors.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# frozen_string_literal: true
2+
3+
module Shoryuken
4+
# Namespace for all Shoryuken-specific errors.
5+
# These provide more meaningful error types than generic Ruby exceptions,
6+
# making it easier to rescue and handle specific failure cases.
7+
module Errors
8+
# Base class for all Shoryuken errors
9+
BaseError = Class.new(StandardError)
10+
11+
# Raised when there is a configuration validation failure
12+
InvalidConfigurationError = Class.new(BaseError)
13+
14+
# Raised when a specified SQS queue does not exist or cannot be accessed
15+
QueueNotFoundError = Class.new(BaseError)
16+
17+
# Raised when worker registration fails due to conflicts
18+
# (e.g., registering multiple workers for a batch queue)
19+
InvalidWorkerRegistrationError = Class.new(BaseError)
20+
21+
# Raised when an invalid polling strategy is specified
22+
InvalidPollingStrategyError = Class.new(BaseError)
23+
24+
# Raised when an invalid lifecycle event name is used
25+
InvalidEventError = Class.new(BaseError)
26+
27+
# Raised when a delay exceeds the maximum allowed by SQS (15 minutes)
28+
InvalidDelayError = Class.new(BaseError)
29+
30+
# Raised when an ARN format is invalid
31+
InvalidArnError = Class.new(BaseError)
32+
33+
# Exception raised to trigger graceful shutdown of the server
34+
# @see https://github.com/mperham/sidekiq/blob/33f5d6b2b6c0dfaab11e5d39688cab7ebadc83ae/lib/sidekiq/cli.rb#L20
35+
Shutdown = Class.new(Interrupt)
36+
end
37+
end

lib/shoryuken/options.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ def polling_strategy(group)
119119
begin
120120
Object.const_get(strategy)
121121
rescue NameError
122-
raise ArgumentError, "#{strategy} is not a valid polling_strategy"
122+
raise Errors::InvalidPollingStrategyError, "#{strategy} is not a valid polling_strategy"
123123
end
124124
when Class
125125
strategy
@@ -267,8 +267,8 @@ def on_stop(&block)
267267
# end
268268
# end
269269
def on(event, &block)
270-
fail ArgumentError, "Symbols only please: #{event}" unless event.is_a?(Symbol)
271-
fail ArgumentError, "Invalid event name: #{event}" unless options[:lifecycle_events].key?(event)
270+
raise Errors::InvalidEventError, "Symbols only please: #{event}" unless event.is_a?(Symbol)
271+
raise Errors::InvalidEventError, "Invalid event name: #{event}" unless options[:lifecycle_events].key?(event)
272272

273273
options[:lifecycle_events][event] << block
274274
end

lib/shoryuken/queue.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ def arn_to_url(arn_str)
149149
required = [region, account_id, resource].map(&:to_s)
150150
valid = required.none?(&:empty?)
151151

152-
abort "Invalid ARN: #{arn_str}. A valid ARN must include: region, account_id and resource." unless valid
152+
raise Errors::InvalidArnError, "Invalid ARN: #{arn_str}. A valid ARN must include: region, account_id and resource." unless valid
153153

154154
"https://sqs.#{region}.amazonaws.com/#{account_id}/#{resource}"
155155
end
@@ -177,8 +177,8 @@ def set_name_and_url(name_or_url_or_arn) # rubocop:disable Naming/AccessorMethod
177177
end
178178

179179
set_by_name(name_or_url_or_arn)
180-
rescue Aws::Errors::NoSuchEndpointError, Aws::SQS::Errors::NonExistentQueue => e
181-
raise e, "The specified queue #{name_or_url_or_arn} does not exist."
180+
rescue Aws::Errors::NoSuchEndpointError, Aws::SQS::Errors::NonExistentQueue
181+
raise Errors::QueueNotFoundError, "The specified queue #{name_or_url_or_arn} does not exist."
182182
end
183183

184184
# Returns the queue attributes from SQS

lib/shoryuken/runner.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@
99
require 'shoryuken'
1010

1111
module Shoryuken
12-
# Exception raised to trigger shutdown
13-
# @see https://github.com/mperham/sidekiq/blob/33f5d6b2b6c0dfaab11e5d39688cab7ebadc83ae/lib/sidekiq/cli.rb#L20
14-
class Shutdown < Interrupt; end
15-
1612
# Runs the Shoryuken server process.
1713
# Handles signal trapping, daemonization, and lifecycle management.
1814
class Runner

lib/shoryuken/worker/default_executor.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,15 @@ def perform_async(worker_class, body, options = {})
3737
# @option options [Hash] :message_attributes custom message attributes
3838
# @option options [String] :queue override the default queue
3939
# @return [Aws::SQS::Types::SendMessageResult] the send result
40-
# @raise [RuntimeError] if delay exceeds 15 minutes
40+
# @raise [Errors::InvalidDelayError] if delay exceeds 15 minutes
4141
def perform_in(worker_class, interval, body, options = {})
4242
interval = interval.to_f
4343
now = Time.now.to_f
4444
ts = (interval < 1_000_000_000 ? (now + interval).to_f : interval)
4545

4646
delay = (ts - now).ceil
4747

48-
raise 'The maximum allowed delay is 15 minutes' if delay > 15 * 60
48+
raise Errors::InvalidDelayError, 'The maximum allowed delay is 15 minutes' if delay > 15 * 60
4949

5050
worker_class.perform_async(body, options.merge(delay_seconds: delay))
5151
end

spec/lib/shoryuken/environment_loader_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
context "when given queues don't exist" do
2525
specify do
2626
expect { subject.load }.to raise_error(
27-
ArgumentError,
27+
Shoryuken::Errors::QueueNotFoundError,
2828
<<-MSG.gsub(/^\s+/, '')
2929
The specified queue(s) stubbed_queue do not exist.
3030
Try 'shoryuken sqs create QUEUE-NAME' for creating a queue with default settings.

spec/lib/shoryuken/options_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,8 @@ def perform(sqs_msg, body); end
188188
end
189189

190190
specify do
191-
expect { Shoryuken.polling_strategy('default') }.to raise_error(ArgumentError)
192-
expect { Shoryuken.polling_strategy('group1') }.to raise_error(ArgumentError)
191+
expect { Shoryuken.polling_strategy('default') }.to raise_error(Shoryuken::Errors::InvalidPollingStrategyError)
192+
expect { Shoryuken.polling_strategy('group1') }.to raise_error(Shoryuken::Errors::InvalidPollingStrategyError)
193193
end
194194
end
195195

0 commit comments

Comments
 (0)