-
-
Notifications
You must be signed in to change notification settings - Fork 204
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create
InterruptErrors
extension to raise an exception when an inte…
…rrupted job is retried (#830) * Create `InterruptErrors` extension to raise an exception when an interrupted job is retried * Update README.md
- Loading branch information
1 parent
8ddda78
commit a44a464
Showing
8 changed files
with
116 additions
and
25 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,16 @@ | ||
# frozen_string_literal: true | ||
module GoodJob | ||
module ActiveJobExtensions | ||
module InterruptErrors | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
around_perform do |_job, block| | ||
raise InterruptError, "Interrupted after starting perform at '#{CurrentThread.execution_interrupted}'" if CurrentThread.execution_interrupted.present? | ||
|
||
block.call | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains 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 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,6 @@ | ||
# frozen_string_literal: true | ||
module GoodJob | ||
# Exception raised when a job is interrupted by a SIGKILL or power failure. | ||
class InterruptError < StandardError | ||
end | ||
end |
46 changes: 46 additions & 0 deletions
46
spec/lib/good_job/active_job_extensions/interrupt_errors_spec.rb
This file contains 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,46 @@ | ||
# frozen_string_literal: true | ||
require 'rails_helper' | ||
|
||
RSpec.describe GoodJob::ActiveJobExtensions::InterruptErrors do | ||
before do | ||
ActiveJob::Base.queue_adapter = GoodJob::Adapter.new(execution_mode: :external) | ||
|
||
stub_const 'TestJob', (Class.new(ActiveJob::Base) do | ||
include GoodJob::ActiveJobExtensions::InterruptErrors | ||
|
||
def perform | ||
end | ||
end) | ||
end | ||
|
||
context 'when a dequeued job has a performed_at but no finished_at' do | ||
before do | ||
active_job = TestJob.perform_later | ||
GoodJob::Execution.find_by(active_job_id: active_job.job_id).update!(performed_at: Time.current) | ||
end | ||
|
||
it 'raises a GoodJob::InterruptError' do | ||
expect { GoodJob.perform_inline }.to raise_error(GoodJob::InterruptError) | ||
end | ||
|
||
it 'is rescuable' do | ||
TestJob.retry_on GoodJob::InterruptError | ||
|
||
expect { GoodJob.perform_inline }.not_to raise_error | ||
expect(GoodJob::Execution.count).to eq(2) | ||
|
||
job = GoodJob::Job.first | ||
expect(job.executions.first.error).to start_with 'GoodJob::InterruptError: Interrupted after starting perform at' | ||
end | ||
end | ||
|
||
context 'when dequeued job does ot have performed at' do | ||
before do | ||
TestJob.perform_later | ||
end | ||
|
||
it 'does not raise' do | ||
expect { GoodJob.perform_inline }.not_to raise_error | ||
end | ||
end | ||
end |
This file contains 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