From 668e8bf7c1d960b706c7a69ce22e34e053b26beb Mon Sep 17 00:00:00 2001 From: Daniel Dao Date: Thu, 6 Jan 2022 11:43:31 -0800 Subject: [PATCH 1/4] =?UTF-8?q?=F0=9F=94=8A=20Add=20better=20logging?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/shoryuken/environment_loader.rb | 8 +++++++- spec/shoryuken/environment_loader_spec.rb | 2 +- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/shoryuken/environment_loader.rb b/lib/shoryuken/environment_loader.rb index 35d226f7..4bd3bbdd 100644 --- a/lib/shoryuken/environment_loader.rb +++ b/lib/shoryuken/environment_loader.rb @@ -159,9 +159,15 @@ def validate_queues return if non_existent_queues.none? + error_msg = <<~MSG + "The specified queue(s) #{non_existent_queues.join(', ')} do not exist. + Try 'shoryuken sqs create QUEUE-NAME' for creating a queue with default settings. + It's also possible that you don't have permission to access the specified queues." + MSG + fail( ArgumentError, - "The specified queue(s) #{non_existent_queues.join(', ')} do not exist.\nTry 'shoryuken sqs create QUEUE-NAME' for creating a queue with default settings" + error_msg ) end diff --git a/spec/shoryuken/environment_loader_spec.rb b/spec/shoryuken/environment_loader_spec.rb index 0e667024..44a3ce1b 100644 --- a/spec/shoryuken/environment_loader_spec.rb +++ b/spec/shoryuken/environment_loader_spec.rb @@ -44,7 +44,6 @@ end end - describe '#prefix_active_job_queue_names' do before do allow(subject).to receive(:load_rails) @@ -94,6 +93,7 @@ expect(Shoryuken.groups['group1'][:queues]).to(eq(['arn:aws:sqs:fake-region-1:1234:test_group1_queue1'])) end end + describe "#setup_options" do let (:cli_queues) { { "queue1"=> 10, "queue2" => 20 } } let (:config_queues) { [["queue1", 8], ["queue2", 4]] } From 349c4d5a31ce15270141cf85063e14560eaf53e6 Mon Sep 17 00:00:00 2001 From: Daniel Dao Date: Fri, 7 Jan 2022 10:35:56 -0800 Subject: [PATCH 2/4] Fix Rails 4.2 HEREDOC --- lib/shoryuken/environment_loader.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/shoryuken/environment_loader.rb b/lib/shoryuken/environment_loader.rb index 4bd3bbdd..69184da4 100644 --- a/lib/shoryuken/environment_loader.rb +++ b/lib/shoryuken/environment_loader.rb @@ -159,7 +159,7 @@ def validate_queues return if non_existent_queues.none? - error_msg = <<~MSG + error_msg = <<-MSG "The specified queue(s) #{non_existent_queues.join(', ')} do not exist. Try 'shoryuken sqs create QUEUE-NAME' for creating a queue with default settings. It's also possible that you don't have permission to access the specified queues." From cb202444f732f9455be281694e5542d85e712def Mon Sep 17 00:00:00 2001 From: Daniel Dao Date: Fri, 7 Jan 2022 15:14:53 -0800 Subject: [PATCH 3/4] =?UTF-8?q?=F0=9F=94=8A=20add=20more=20logging=20+=20d?= =?UTF-8?q?ocumentation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/shoryuken/environment_loader.rb | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/shoryuken/environment_loader.rb b/lib/shoryuken/environment_loader.rb index 69184da4..8999752d 100644 --- a/lib/shoryuken/environment_loader.rb +++ b/lib/shoryuken/environment_loader.rb @@ -159,10 +159,12 @@ def validate_queues return if non_existent_queues.none? - error_msg = <<-MSG - "The specified queue(s) #{non_existent_queues.join(', ')} do not exist. - Try 'shoryuken sqs create QUEUE-NAME' for creating a queue with default settings. - It's also possible that you don't have permission to access the specified queues." + # NOTE: HEREDOC's ~ operator removes indents, but is only available Ruby 2.3+ + # See github PR: https://github.com/ruby-shoryuken/shoryuken/pull/691#issuecomment-1007653595 + error_msg = <<-MSG.gsub(/^\s+/, '') + The specified queue(s) #{non_existent_queues.join(', ')} do not exist. + Try 'shoryuken sqs create QUEUE-NAME' for creating a queue with default settings. + It's also possible that you don't have permission to access the specified queues. MSG fail( From 47ccb6bcdb201a5ec9163bbd6e82f3a8a2b44774 Mon Sep 17 00:00:00 2001 From: Daniel Dao Date: Fri, 7 Jan 2022 15:24:06 -0800 Subject: [PATCH 4/4] =?UTF-8?q?=E2=9C=85=20=20Add=20tests?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- spec/shoryuken/environment_loader_spec.rb | 43 ++++++++++++++++++++--- 1 file changed, 38 insertions(+), 5 deletions(-) diff --git a/spec/shoryuken/environment_loader_spec.rb b/spec/shoryuken/environment_loader_spec.rb index 44a3ce1b..d1fb4040 100644 --- a/spec/shoryuken/environment_loader_spec.rb +++ b/spec/shoryuken/environment_loader_spec.rb @@ -4,6 +4,36 @@ RSpec.describe Shoryuken::EnvironmentLoader do subject { described_class.new({}) } + describe '#load' do + before do + Shoryuken.groups.clear + # See issue: https://stackoverflow.com/a/63699568 for stubbing AWS errors + allow(Shoryuken::Client) + .to receive(:queues) + .with('stubbed_queue') + .and_raise(Aws::SQS::Errors::NonExistentQueue.new(nil, nil)) + allow(subject).to receive(:load_rails) + allow(subject).to receive(:prefix_active_job_queue_names) + allow(subject).to receive(:require_workers) + allow(subject).to receive(:validate_workers) + allow(subject).to receive(:patch_deprecated_workers) + Shoryuken.options[:groups] = [['custom', { queues: ['stubbed_queue'] }]] + end + + context "when given queues don't exist" do + specify do + expect { subject.load }.to raise_error( + ArgumentError, + <<-MSG.gsub(/^\s+/, '') + The specified queue(s) stubbed_queue do not exist. + Try 'shoryuken sqs create QUEUE-NAME' for creating a queue with default settings. + It's also possible that you don't have permission to access the specified queues. + MSG + ) + end + end + end + describe '#parse_queues loads default queues' do before do allow(subject).to receive(:load_rails) @@ -34,7 +64,7 @@ specify do Shoryuken.options[:queues] = ['queue1', 'queue2'] # default queues - Shoryuken.options[:groups] = [[ 'custom', { queues: ['queue3'], delay: 25 }]] + Shoryuken.options[:groups] = [['custom', { queues: ['queue3'], delay: 25 }]] subject.load expect(Shoryuken.groups['default'][:queues]).to eq(%w[queue1 queue2]) @@ -75,7 +105,7 @@ it 'does not prefix url-based queues' do Shoryuken.options[:queues] = ['https://example.com/test_queue1'] - Shoryuken.options[:groups] = {'group1' => {queues: ['https://example.com/test_group1_queue1']}} + Shoryuken.options[:groups] = { 'group1' => { queues: ['https://example.com/test_group1_queue1'] } } subject.load @@ -85,7 +115,7 @@ it 'does not prefix arn-based queues' do Shoryuken.options[:queues] = ['arn:aws:sqs:fake-region-1:1234:test_queue1'] - Shoryuken.options[:groups] = {'group1' => {queues: ['arn:aws:sqs:fake-region-1:1234:test_group1_queue1']}} + Shoryuken.options[:groups] = { 'group1' => { queues: ['arn:aws:sqs:fake-region-1:1234:test_group1_queue1'] } } subject.load @@ -95,8 +125,9 @@ end describe "#setup_options" do - let (:cli_queues) { { "queue1"=> 10, "queue2" => 20 } } - let (:config_queues) { [["queue1", 8], ["queue2", 4]] } + let(:cli_queues) { { "queue1" => 10, "queue2" => 20 } } + let(:config_queues) { [["queue1", 8], ["queue2", 4]] } + context "when given queues through config and CLI" do specify do allow_any_instance_of(Shoryuken::EnvironmentLoader).to receive(:config_file_options).and_return({ queues: config_queues }) @@ -104,6 +135,7 @@ expect(Shoryuken.options[:queues]).to eq(cli_queues) end end + context "when given queues through config only" do specify do allow_any_instance_of(Shoryuken::EnvironmentLoader).to receive(:config_file_options).and_return({ queues: config_queues }) @@ -111,6 +143,7 @@ expect(Shoryuken.options[:queues]).to eq(config_queues) end end + context "when given queues through CLI only" do specify do Shoryuken::EnvironmentLoader.setup_options(queues: cli_queues)