Skip to content

Commit 8898ebd

Browse files
committed
fix: Capture and log exceptions from job threads.
1 parent 62b0ff5 commit 8898ebd

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

app/jobs/application_job.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,21 @@ class ApplicationJob < ActiveJob::Base
44

55
# Most jobs are safe to ignore if the underlying records are no longer available
66
# discard_on ActiveJob::DeserializationError
7+
8+
# Make sure to capture and record any exceptions that occur in jobs.
9+
rescue_from(Exception) do |error|
10+
span = OpenTelemetry::Trace.current_span
11+
span.status = OpenTelemetry::Trace::Status.error(error.message)
12+
span.record_exception(
13+
error,
14+
attributes: { "job.class" => self.class.name, "job.id" => job_id }
15+
)
16+
17+
Rails.logger.error("Job failed", job_class: self.class.name, job_id: job_id,
18+
error: error.message, error_class: error.class.name,
19+
backtrace: error.backtrace&.first(5))
20+
21+
# Re-raise the exception to be handled by the job framework (SolidQueue).
22+
raise error
23+
end
724
end

config/initializers/solid_queue.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# frozen_string_literal: true
2+
3+
# Surface SolidQueue thread-level errors (worker/dispatcher crashes) to logs and
4+
# traces. Job-level errors are handled in ApplicationJob#rescue_from.
5+
SolidQueue.on_thread_error = lambda do |error|
6+
span = OpenTelemetry::Trace.current_span
7+
span.status = OpenTelemetry::Trace::Status.error(error.message)
8+
span.record_exception(error)
9+
10+
Rails.logger.error("SolidQueue thread error", error: error.message,
11+
error_class: error.class.name,
12+
backtrace: error.backtrace&.first(5))
13+
end

0 commit comments

Comments
 (0)