Skip to content

Commit

Permalink
Inline adapter should raise unhandled exceptions during execution
Browse files Browse the repository at this point in the history
  • Loading branch information
bensheldon committed Oct 10, 2021
1 parent 4d5ebef commit b18f987
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 5 deletions.
4 changes: 3 additions & 1 deletion lib/good_job/adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,12 @@ def enqueue_at(active_job, timestamp)

if execute_inline?
begin
execution.perform
result = execution.perform
ensure
execution.advisory_unlock
end

raise result.unhandled_error if result.unhandled_error
else
job_state = { queue_name: execution.queue_name }
job_state[:scheduled_at] = execution.scheduled_at if execution.scheduled_at
Expand Down
10 changes: 8 additions & 2 deletions spec/app/jobs/example_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,14 @@

describe "DEAD_TYPE" do
it 'errors but does not retry' do
active_job = described_class.perform_later(described_class::DEAD_TYPE)
executions = GoodJob::Execution.where(active_job_id: active_job.job_id).order(created_at: :asc)
begin
described_class.perform_later(described_class::DEAD_TYPE)
rescue ExampleJob::DeadError
nil
end
active_job_id = GoodJob::Execution.last.active_job_id

executions = GoodJob::Execution.where(active_job_id: active_job_id).order(created_at: :asc)
expect(executions.size).to eq 3
expect(executions.last.error).to be_present
end
Expand Down
6 changes: 5 additions & 1 deletion spec/engine/filters/good_job/executions_filter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@

ExampleJob.set(queue: 'default').perform_later('success')
ExampleJob.set(queue: 'mice').perform_later('error_once')
ExampleJob.set(queue: 'elephants').perform_later('dead')
begin
ExampleJob.set(queue: 'elephants').perform_later('dead')
rescue ExampleJob::DeadError
nil
end

running_job = ExampleJob.perform_later('success')
running_execution = GoodJob::Execution.find(running_job.provider_job_id)
Expand Down
6 changes: 5 additions & 1 deletion spec/engine/filters/good_job/jobs_filter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@

ExampleJob.set(queue: 'default').perform_later('success')
ExampleJob.set(queue: 'mice').perform_later('error_once')
ExampleJob.set(queue: 'elephants').perform_later('dead')
begin
ExampleJob.set(queue: 'elephants').perform_later('dead')
rescue ExampleJob::DeadError
nil
end

running_job = ExampleJob.perform_later('success')
running_execution = GoodJob::Execution.find(running_job.provider_job_id)
Expand Down
24 changes: 24 additions & 0 deletions spec/integration/adapter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,28 @@ def perform(*_args, **_kwargs)
end
end
end

context 'when inline adapter' do
let(:adapter) { GoodJob::Adapter.new(execution_mode: :inline) }

before do
stub_const 'PERFORMED', []
stub_const 'JobError', Class.new(StandardError)
stub_const 'TestJob', (Class.new(ActiveJob::Base) do
retry_on JobError, attempts: 3

def perform
PERFORMED << Time.current
raise JobError
end
end)
end

it 'raises unhandled exceptions' do
expect do
TestJob.perform_later
end.to raise_error JobError
expect(PERFORMED.size).to eq 3
end
end
end
27 changes: 27 additions & 0 deletions spec/lib/good_job/adapter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,33 @@
)
end

context 'when inline' do
let(:adapter) { described_class.new(execution_mode: :inline) }

before do
stub_const 'PERFORMED', []
stub_const 'JobError', Class.new(StandardError)
stub_const 'TestJob', (Class.new(ActiveJob::Base) do
def perform(succeed: true)
PERFORMED << Time.current

raise JobError unless succeed
end
end)
end

it 'executes the job immediately' do
adapter.enqueue(TestJob.new(succeed: true))
expect(PERFORMED.size).to eq 1
end

it "raises unhandled exceptions" do
expect do
adapter.enqueue(TestJob.new(succeed: false))
end.to raise_error(JobError)
end
end

context 'when async' do
it 'triggers an execution thread and the notifier' do
allow(GoodJob::Execution).to receive(:enqueue).and_return(good_job)
Expand Down

0 comments on commit b18f987

Please sign in to comment.