Skip to content

Commit 4c9a81b

Browse files
committed
Report errors to the error reporter
1 parent a57660c commit 4c9a81b

File tree

7 files changed

+37
-14
lines changed

7 files changed

+37
-14
lines changed

app/models/solid_queue/claimed_execution.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ def release_all
3636
end
3737
end
3838

39-
def fail_all_with(error)
39+
def fail_all_with(error, reraise:)
4040
SolidQueue.instrument(:fail_many_claimed) do |payload|
4141
includes(:job).tap do |executions|
42-
executions.each { |execution| execution.failed_with(error) }
42+
executions.each { |execution| execution.failed_with(error, reraise: reraise) }
4343

4444
payload[:process_ids] = executions.map(&:process_id).uniq
4545
payload[:job_ids] = executions.map(&:job_id).uniq
@@ -63,7 +63,7 @@ def perform
6363
if result.success?
6464
finished
6565
else
66-
failed_with(result.error)
66+
failed_with(result.error, reraise: true)
6767
end
6868
ensure
6969
job.unblock_next_blocked_job
@@ -82,11 +82,12 @@ def discard
8282
raise UndiscardableError, "Can't discard a job in progress"
8383
end
8484

85-
def failed_with(error)
85+
def failed_with(error, reraise:)
8686
transaction do
8787
job.failed_with(error)
8888
destroy!
8989
end
90+
raise error if reraise
9091
end
9192

9293
private

app/models/solid_queue/process/executor.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ module Executor
1111
after_destroy :release_all_claimed_executions
1212
end
1313

14-
def fail_all_claimed_executions_with(error)
14+
def fail_all_claimed_executions_with(error, reraise:)
1515
if claims_executions?
16-
claimed_executions.fail_all_with(error)
16+
claimed_executions.fail_all_with(error, reraise: reraise)
1717
end
1818
end
1919

app/models/solid_queue/process/prunable.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def prune(excluding: nil)
2323

2424
def prune
2525
error = Processes::ProcessPrunedError.new(last_heartbeat_at)
26-
fail_all_claimed_executions_with(error)
26+
fail_all_claimed_executions_with(error, reraise: false)
2727

2828
deregister(pruned: true)
2929
end

lib/solid_queue/supervisor.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ def replace_fork(pid, status)
173173
def handle_claimed_jobs_by(terminated_fork, status)
174174
if registered_process = process.supervisees.find_by(name: terminated_fork.name)
175175
error = Processes::ProcessExitError.new(status)
176-
registered_process.fail_all_claimed_executions_with(error)
176+
registered_process.fail_all_claimed_executions_with(error, reraise: false)
177177
end
178178
end
179179

lib/solid_queue/supervisor/maintenance.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ def prune_dead_processes
2929

3030
def fail_orphaned_executions
3131
wrap_in_app_executor do
32-
ClaimedExecution.orphaned.fail_all_with(Processes::ProcessMissingError.new)
32+
ClaimedExecution.orphaned.
33+
fail_all_with(Processes::ProcessMissingError.new, reraise: false)
3334
end
3435
end
3536
end

test/models/solid_queue/claimed_execution_test.rb

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ class SolidQueue::ClaimedExecutionTest < ActiveSupport::TestCase
2222
job = claimed_execution.job
2323

2424
assert_difference -> { SolidQueue::ClaimedExecution.count } => -1, -> { SolidQueue::FailedExecution.count } => 1 do
25-
claimed_execution.perform
25+
assert_raises RuntimeError do
26+
claimed_execution.perform
27+
end
2628
end
2729

2830
assert_not job.reload.finished?
@@ -37,10 +39,12 @@ class SolidQueue::ClaimedExecutionTest < ActiveSupport::TestCase
3739
test "job failures are reported via Rails error subscriber" do
3840
subscriber = ErrorBuffer.new
3941

40-
with_error_subscriber(subscriber) do
41-
claimed_execution = prepare_and_claim_job RaisingJob.perform_later(RuntimeError, "B")
42+
assert_raises RuntimeError do
43+
with_error_subscriber(subscriber) do
44+
claimed_execution = prepare_and_claim_job RaisingJob.perform_later(RuntimeError, "B")
4245

43-
claimed_execution.perform
46+
claimed_execution.perform
47+
end
4448
end
4549

4650
assert_equal 1, subscriber.errors.count
@@ -63,7 +67,7 @@ class SolidQueue::ClaimedExecutionTest < ActiveSupport::TestCase
6367
job = claimed_execution.job
6468

6569
assert_difference -> { SolidQueue::ClaimedExecution.count } => -1, -> { SolidQueue::FailedExecution.count } => 1 do
66-
claimed_execution.failed_with(RuntimeError.new)
70+
claimed_execution.failed_with(RuntimeError.new, reraise: false)
6771
end
6872

6973
assert job.reload.failed?

test/unit/worker_test.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,23 @@ class WorkerTest < ActiveSupport::TestCase
6767
SolidQueue.on_thread_error = original_on_thread_error
6868
end
6969

70+
test "errors on claimed executions are reported via Rails error subscriber" do
71+
subscriber = ErrorBuffer.new
72+
Rails.error.subscribe(subscriber)
73+
74+
RaisingJob.perform_later(RuntimeError, "B")
75+
76+
@worker.start
77+
78+
wait_for_jobs_to_finish_for(1.second)
79+
@worker.wake_up
80+
81+
assert_equal 1, subscriber.errors.count
82+
assert_equal "This is a RuntimeError exception", subscriber.messages.first
83+
ensure
84+
Rails.error.unsubscribe(subscriber) if Rails.error.respond_to?(:unsubscribe)
85+
end
86+
7087
test "claim and process more enqueued jobs than the pool size allows to process at once" do
7188
5.times do |i|
7289
StoreResultJob.perform_later(:paused, pause: 0.1.second)

0 commit comments

Comments
 (0)