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 9, 2021
1 parent b2717bd commit 5cfa60e
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
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
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 5cfa60e

Please sign in to comment.