-
Notifications
You must be signed in to change notification settings - Fork 289
support AJ continuation in rails 8.1 #899
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'securerandom' | ||
| require 'active_job' | ||
| require 'shoryuken/extensions/active_job_adapter' | ||
| require 'shoryuken/extensions/active_job_extensions' | ||
|
|
||
| RSpec.describe 'ActiveJob Continuations Integration' do | ||
| # Skip all tests in this suite if ActiveJob::Continuable is not available (Rails < 8.0) | ||
| before(:all) do | ||
| skip 'ActiveJob::Continuable not available (Rails < 8.0)' unless defined?(ActiveJob::Continuable) | ||
| end | ||
|
|
||
| # Test job that uses ActiveJob Continuations | ||
| class ContinuableTestJob < ActiveJob::Base | ||
| include ActiveJob::Continuable if defined?(ActiveJob::Continuable) | ||
|
|
||
| queue_as :default | ||
|
|
||
| class_attribute :executions_log, default: [] | ||
| class_attribute :checkpoints_reached, default: [] | ||
|
|
||
| def perform(max_iterations: 10) | ||
| self.class.executions_log << { execution: executions, started_at: Time.current } | ||
|
|
||
| step :initialize_work do | ||
| self.class.checkpoints_reached << "initialize_work_#{executions}" | ||
| end | ||
|
|
||
| step :process_items, start: cursor || 0 do | ||
| (cursor..max_iterations).each do |i| | ||
| self.class.checkpoints_reached << "processing_item_#{i}" | ||
|
|
||
| # Check if we should stop (checkpoint) | ||
| checkpoint | ||
|
|
||
| # Simulate some work | ||
| sleep 0.01 | ||
|
|
||
| # Advance cursor | ||
| cursor.advance! | ||
| end | ||
| end | ||
|
|
||
| step :finalize_work do | ||
| self.class.checkpoints_reached << 'finalize_work' | ||
| end | ||
|
|
||
| self.class.executions_log.last[:completed] = true | ||
| end | ||
| end | ||
|
|
||
| describe 'stopping? method (unit tests)' do | ||
| it 'returns false when launcher is not initialized' do | ||
| adapter = ActiveJob::QueueAdapters::ShoryukenAdapter.new | ||
| expect(adapter.stopping?).to be false | ||
| end | ||
|
|
||
| it 'returns true when launcher is stopping' do | ||
| launcher = Shoryuken::Launcher.new | ||
| runner = Shoryuken::Runner.instance | ||
| runner.instance_variable_set(:@launcher, launcher) | ||
|
|
||
| adapter = ActiveJob::QueueAdapters::ShoryukenAdapter.new | ||
| expect(adapter.stopping?).to be false | ||
|
|
||
| launcher.instance_variable_set(:@stopping, true) | ||
| expect(adapter.stopping?).to be true | ||
| end | ||
| end | ||
|
|
||
| describe 'timestamp handling for continuation retries' do | ||
| it 'handles past timestamps for continuation retries' do | ||
| adapter = ActiveJob::QueueAdapters::ShoryukenAdapter.new | ||
| job = ContinuableTestJob.new | ||
| job.sqs_send_message_parameters = {} | ||
|
|
||
| # Mock the queue | ||
| queue = instance_double(Shoryuken::Queue, fifo?: false) | ||
| allow(Shoryuken::Client).to receive(:queues).and_return(queue) | ||
| allow(Shoryuken).to receive(:register_worker) | ||
| allow(queue).to receive(:send_message) do |params| | ||
| # Verify past timestamp results in immediate delivery (delay_seconds <= 0) | ||
| expect(params[:delay_seconds]).to be <= 0 | ||
| end | ||
|
|
||
| # Enqueue with past timestamp (simulating continuation retry) | ||
| past_timestamp = Time.current.to_f - 60 | ||
| adapter.enqueue_at(job, past_timestamp) | ||
| end | ||
| end | ||
|
|
||
| describe 'enqueue_at with continuation timestamps (unit tests)' do | ||
| let(:adapter) { ActiveJob::QueueAdapters::ShoryukenAdapter.new } | ||
| let(:job) do | ||
| job = ContinuableTestJob.new | ||
| job.sqs_send_message_parameters = {} | ||
| job | ||
| end | ||
| let(:queue) { instance_double(Shoryuken::Queue, fifo?: false) } | ||
|
|
||
| before do | ||
| allow(Shoryuken::Client).to receive(:queues).and_return(queue) | ||
| allow(Shoryuken).to receive(:register_worker) | ||
| @sent_messages = [] | ||
| allow(queue).to receive(:send_message) do |params| | ||
| @sent_messages << params | ||
| end | ||
| end | ||
|
|
||
| it 'accepts past timestamps without error' do | ||
| past_timestamp = Time.current.to_f - 30 | ||
|
|
||
| expect { | ||
| adapter.enqueue_at(job, past_timestamp) | ||
| }.not_to raise_error | ||
|
|
||
| expect(@sent_messages.size).to eq(1) | ||
| expect(@sent_messages.first[:delay_seconds]).to be <= 0 | ||
| end | ||
|
|
||
| it 'accepts current timestamp' do | ||
| current_timestamp = Time.current.to_f | ||
|
|
||
| expect { | ||
| adapter.enqueue_at(job, current_timestamp) | ||
| }.not_to raise_error | ||
|
|
||
| expect(@sent_messages.size).to eq(1) | ||
| expect(@sent_messages.first[:delay_seconds]).to be_between(-1, 1) | ||
| end | ||
|
|
||
| it 'accepts future timestamp' do | ||
| future_timestamp = Time.current.to_f + 30 | ||
|
|
||
| expect { | ||
| adapter.enqueue_at(job, future_timestamp) | ||
| }.not_to raise_error | ||
|
|
||
| expect(@sent_messages.size).to eq(1) | ||
| expect(@sent_messages.first[:delay_seconds]).to be > 0 | ||
| expect(@sent_messages.first[:delay_seconds]).to be <= 30 | ||
| end | ||
| end | ||
| end |
110 changes: 110 additions & 0 deletions
110
spec/shoryuken/extensions/active_job_continuation_spec.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'active_job' | ||
| require 'shared_examples_for_active_job' | ||
| require 'shoryuken/extensions/active_job_adapter' | ||
| require 'shoryuken/extensions/active_job_extensions' | ||
|
|
||
| RSpec.describe 'ActiveJob Continuation support' do | ||
| let(:adapter) { ActiveJob::QueueAdapters::ShoryukenAdapter.new } | ||
| let(:job) do | ||
| job = TestJob.new | ||
| job.sqs_send_message_parameters = {} | ||
| job | ||
| end | ||
| let(:queue) { double('Queue', fifo?: false) } | ||
|
|
||
| before do | ||
| allow(Shoryuken::Client).to receive(:queues).with(job.queue_name).and_return(queue) | ||
| allow(Shoryuken).to receive(:register_worker) | ||
| end | ||
mensfeld marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| describe '#stopping?' do | ||
| context 'when Launcher is not initialized' do | ||
| it 'returns false' do | ||
| runner = instance_double(Shoryuken::Runner, launcher: nil) | ||
| allow(Shoryuken::Runner).to receive(:instance).and_return(runner) | ||
|
|
||
| expect(adapter.stopping?).to be false | ||
| end | ||
| end | ||
|
|
||
| context 'when Launcher is initialized' do | ||
| let(:runner) { instance_double(Shoryuken::Runner) } | ||
| let(:launcher) { instance_double(Shoryuken::Launcher) } | ||
|
|
||
| before do | ||
| allow(Shoryuken::Runner).to receive(:instance).and_return(runner) | ||
| allow(runner).to receive(:launcher).and_return(launcher) | ||
| end | ||
|
|
||
| it 'returns false when not stopping' do | ||
| allow(launcher).to receive(:stopping?).and_return(false) | ||
| expect(adapter.stopping?).to be false | ||
| end | ||
|
|
||
| it 'returns true when stopping' do | ||
| allow(launcher).to receive(:stopping?).and_return(true) | ||
| expect(adapter.stopping?).to be true | ||
| end | ||
| end | ||
| end | ||
|
|
||
| describe '#enqueue_at with past timestamps' do | ||
| let(:past_timestamp) { Time.current.to_f - 60 } # 60 seconds ago | ||
|
|
||
| it 'enqueues with negative delay_seconds when timestamp is in the past' do | ||
| expect(queue).to receive(:send_message) do |hash| | ||
| expect(hash[:delay_seconds]).to be <= 0 | ||
| expect(hash[:delay_seconds]).to be >= -61 # Allow for rounding and timing | ||
| end | ||
|
|
||
| adapter.enqueue_at(job, past_timestamp) | ||
| end | ||
|
|
||
| it 'does not raise an error for past timestamps' do | ||
| allow(queue).to receive(:send_message) | ||
|
|
||
| expect { adapter.enqueue_at(job, past_timestamp) }.not_to raise_error | ||
| end | ||
| end | ||
|
|
||
| describe '#enqueue_at with future timestamps' do | ||
| let(:future_timestamp) { Time.current.to_f + 60 } # 60 seconds from now | ||
|
|
||
| it 'enqueues with delay_seconds when timestamp is in the future' do | ||
| expect(queue).to receive(:send_message) do |hash| | ||
| expect(hash[:delay_seconds]).to be > 0 | ||
| expect(hash[:delay_seconds]).to be <= 60 | ||
| end | ||
|
|
||
| adapter.enqueue_at(job, future_timestamp) | ||
| end | ||
| end | ||
|
|
||
| describe '#enqueue_at with current timestamp' do | ||
| let(:current_timestamp) { Time.current.to_f } | ||
|
|
||
| it 'enqueues with delay_seconds close to 0' do | ||
| expect(queue).to receive(:send_message) do |hash| | ||
| expect(hash[:delay_seconds]).to be_between(-1, 1) # Allow for timing/rounding | ||
| end | ||
|
|
||
| adapter.enqueue_at(job, current_timestamp) | ||
| end | ||
| end | ||
|
|
||
| describe 'retry_on with zero wait' do | ||
| it 'allows immediate retries through continuation mechanism' do | ||
| # Simulate a job with retry_on configuration that uses zero wait | ||
| past_timestamp = Time.current.to_f - 1 | ||
|
|
||
| expect(queue).to receive(:send_message) do |hash| | ||
| # Negative delay for past timestamp - SQS will handle immediate delivery | ||
| expect(hash[:delay_seconds]).to be <= 0 | ||
| end | ||
|
|
||
| adapter.enqueue_at(job, past_timestamp) | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.