Skip to content

Commit

Permalink
refactor: Move a logic, which calculate delay seconds to Params class.
Browse files Browse the repository at this point in the history
  • Loading branch information
kakubin committed Jan 17, 2024
1 parent 842493e commit 4b1606a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
6 changes: 1 addition & 5 deletions lib/active_job/queue_adapters/amazon_sqs_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@ def enqueue(job)
end

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

delay = Params.assured_delay_seconds(timestamp)
_enqueue(job, nil, delay_seconds: delay)
end

Expand Down
10 changes: 10 additions & 0 deletions lib/active_job/queue_adapters/amazon_sqs_adapter/params.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@ module QueueAdapters
class AmazonSqsAdapter
# == build request parameter of Aws::SQS::Client
class Params
class << self
def assured_delay_seconds(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

delay
end
end

def initialize(job, body)
@job = job
@body = body || job.serialize
Expand Down
22 changes: 21 additions & 1 deletion test/active_job/queue_adapters/amazon_sqs_adapter/params_test.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,32 @@
# frozen_string_literal: true

require 'test_helper'
require 'timecop'

module ActiveJob
module QueueAdapters
class AmazonSqsAdapter
describe Params do
describe '.assured_delay_seconds' do
let(:now) { Time.now }

before { allow(Time).to receive(:now).and_return(now) }

it 'returns seconds from present' do
unix_time = (now + 15.minutes).to_f
expect(Params.assured_delay_seconds(unix_time)).to eq 900
end

it 'rounds up to zero' do
unix_time = (now - 1.second).to_f
expect(Params.assured_delay_seconds(unix_time)).to eq 0
end

it 'raise error when 15 minutes after present' do
unix_time = (now + 15.minutes + 1.second).to_f
expect { Params.assured_delay_seconds(unix_time) }.to raise_error ArgumentError
end
end

describe '#queue_url' do
let(:params) { Params.new(job, nil) }
let(:job) { TestJob.new('a1', 'a2') }
Expand Down

0 comments on commit 4b1606a

Please sign in to comment.