diff --git a/lib/active_job/queue_adapters/amazon_sqs_adapter.rb b/lib/active_job/queue_adapters/amazon_sqs_adapter.rb index 28a9b3a2..9998d18b 100644 --- a/lib/active_job/queue_adapters/amazon_sqs_adapter.rb +++ b/lib/active_job/queue_adapters/amazon_sqs_adapter.rb @@ -11,6 +11,8 @@ def enqueue(job) def enqueue_at(job, timestamp) delay = (timestamp - Time.now.to_f).floor + + delay = 0 if delay.negative? raise ArgumentError, 'Unable to queue a job with a delay great than 15 minutes' if delay > 15.minutes _enqueue(job, nil, delay_seconds: delay) diff --git a/lib/generators/aws_record/base.rb b/lib/generators/aws_record/base.rb index c7e22748..d22174b5 100644 --- a/lib/generators/aws_record/base.rb +++ b/lib/generators/aws_record/base.rb @@ -147,7 +147,7 @@ def parse_table_config! [idx.name, parse_rw_units(idx.name)] end.to_h - options['table_config'].each do |config, _rw_units| + options['table_config'].each_key do |config| next if config == 'primary' gsi = @gsis.select { |idx| idx.name == config } diff --git a/test/active_job/queue_adapters/amazon_sqs_adapter_test.rb b/test/active_job/queue_adapters/amazon_sqs_adapter_test.rb index 459c818e..b72ab5f5 100644 --- a/test/active_job/queue_adapters/amazon_sqs_adapter_test.rb +++ b/test/active_job/queue_adapters/amazon_sqs_adapter_test.rb @@ -144,29 +144,49 @@ module QueueAdapters end end - it 'enqueues delayed jobs' do - t1 = Time.now - allow(Time).to receive(:now).and_return t1 - - expect(client).to receive(:send_message).with( - { - queue_url: 'https://queue-url', - delay_seconds: 60, - message_body: instance_of(String), - message_attributes: instance_of(Hash) - } - ) - - TestJob.set(wait: 1.minute).perform_later('test') - sleep(0.2) - end + describe 'with queue delay' do + it 'enqueues jobs with proper delay' do + t1 = Time.now + allow(Time).to receive(:now).and_return t1 + + expect(client).to receive(:send_message).with( + { + queue_url: 'https://queue-url', + delay_seconds: 60, + message_body: instance_of(String), + message_attributes: instance_of(Hash) + } + ) + + TestJob.set(wait: 1.minute).perform_later('test') + sleep(0.2) + end - it 'raises an error when job delay is great than SQS support' do - t1 = Time.now - allow(Time).to receive(:now).and_return t1 - expect do - TestJob.set(wait: 1.day).perform_later('test') - end.to raise_error ArgumentError + it 'enqueues jobs with zero or negative delay' do + t1 = Time.now + allow(Time).to receive(:now).and_return t1 + + expect(client).to receive(:send_message).with( + { + queue_url: 'https://queue-url', + delay_seconds: 0, + message_body: instance_of(String), + message_attributes: instance_of(Hash) + } + ).twice + + TestJob.set(wait: 0).perform_later('test') + TestJob.set(wait: -1).perform_later('test') + sleep(0.2) + end + + it 'raises an error when job delay is great than SQS support' do + t1 = Time.now + allow(Time).to receive(:now).and_return t1 + expect do + TestJob.set(wait: 1.day).perform_later('test') + end.to raise_error ArgumentError + end end end end