Skip to content
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

Unhandled ActiveJob errors should trigger GoodJob.on_thread_error #312

Merged
merged 1 commit into from
Aug 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/good_job/scheduler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,9 @@ def create_thread(state = nil)
# @!visibility private
# @return [void]
def task_observer(time, output, thread_error)
GoodJob.on_thread_error.call(thread_error) if thread_error && GoodJob.on_thread_error.respond_to?(:call)
error = thread_error || (output.is_a?(GoodJob::ExecutionResult) ? output.unhandled_error : nil)
GoodJob.on_thread_error.call(error) if error && GoodJob.on_thread_error.respond_to?(:call)

instrument("finished_job_task", { result: output, error: thread_error, time: time })
create_task if output
end
Expand Down
20 changes: 19 additions & 1 deletion spec/lib/good_job/scheduler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
end

context 'when on task thread' do
it 'calls GoodJob.on_thread_error' do
it 'calls GoodJob.on_thread_error for thread errors' do
allow(performer).to receive(:next) do
THREAD_HAS_RUN.make_true
raise "Whoops"
Expand All @@ -37,6 +37,24 @@

scheduler.shutdown
end

it 'calls GoodJob.on_thread_error for unhandled_errors' do
allow(performer).to receive(:next) do
THREAD_HAS_RUN.make_true
GoodJob::ExecutionResult.new(value: nil, unhandled_error: StandardError.new("oopsy"))
end

allow(error_proc).to receive(:call) do
ERROR_TRIGGERED.make_true
end

scheduler = described_class.new(performer)
scheduler.create_thread
sleep_until { THREAD_HAS_RUN.true? }
sleep_until { ERROR_TRIGGERED.true? }

expect(error_proc).to have_received(:call).with(an_instance_of(StandardError).and(having_attributes(message: 'oopsy'))).at_least(:once)
end
end
end

Expand Down