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

Store error backtraces on discrete executions #1325

Merged
merged 4 commits into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,8 @@ GEM
racc (~> 1.4)
nokogiri (1.16.2-java)
racc (~> 1.4)
nokogiri (1.16.2-x86_64-darwin)
racc (~> 1.4)
nokogiri (1.16.2-x86_64-linux)
racc (~> 1.4)
octokit (4.25.1)
Expand Down Expand Up @@ -487,6 +489,7 @@ PLATFORMS
arm64-darwin-23
universal-java-11
universal-java-20
x86_64-darwin-23
x86_64-linux

DEPENDENCIES
Expand Down
7 changes: 7 additions & 0 deletions app/models/good_job/discrete_execution.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ def self.error_event_migrated?
false
end

def self.backtrace_migrated?
return true if columns_hash["error_backtrace"].present?

migration_pending_warning!
false
end

def number
serialized_params.fetch('executions', 0) + 1
end
Expand Down
6 changes: 6 additions & 0 deletions app/models/good_job/execution.rb
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,10 @@ def self.format_error(error)
[error.class.to_s, ERROR_MESSAGE_SEPARATOR, error.message].join
end

def self.format_backtrace(backtrace)
Rails.backtrace_cleaner.clean(backtrace || [])
end

# Execute the ActiveJob job this {Execution} represents.
# @return [ExecutionResult]
# An array of the return value of the job's +#perform+ method and the
Expand Down Expand Up @@ -455,12 +459,14 @@ def perform
job_error = result.handled_error || result.unhandled_error
if job_error
error_string = self.class.format_error(job_error)
error_backtrace = self.class.format_backtrace(job_error.backtrace)

job_attributes[:error] = error_string
job_attributes[:error_event] = result.error_event if self.class.error_event_migrated?
if discrete_execution
discrete_execution.error = error_string
discrete_execution.error_event = result.error_event
discrete_execution.error_backtrace = error_backtrace if discrete_execution.class.backtrace_migrated?
Intrepidd marked this conversation as resolved.
Show resolved Hide resolved
end
else
job_attributes[:error] = nil
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

class CreateGoodJobExecutionErrorBacktrace < ActiveRecord::Migration[7.1]
def change
reversible do |dir|
dir.up do
# Ensure this incremental update migration is idempotent
# with monolithic install migration.
return if connection.column_exists?(:good_job_executions, :error_backtrace)
end
end

add_column :good_job_executions, :error_backtrace, :text, array: true
end
end
3 changes: 2 additions & 1 deletion demo/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2024_01_14_221613) do
ActiveRecord::Schema.define(version: 2024_04_17_093204) do
# These are extensions that must be enabled in order to support this database
enable_extension "pgcrypto"
enable_extension "plpgsql"
Expand Down Expand Up @@ -41,6 +41,7 @@
t.datetime "finished_at"
t.text "error"
t.integer "error_event", limit: 2
t.text "error_backtrace", array: true
t.index ["active_job_id", "created_at"], name: "index_good_job_executions_on_active_job_id_and_created_at"
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class CreateGoodJobs < ActiveRecord::Migration<%= migration_version %>
t.datetime :finished_at
t.text :error
t.integer :error_event, limit: 2
t.text :error_backtrace, array: true
end

create_table :good_job_processes, id: :uuid do |t|
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

class CreateGoodJobExecutionErrorBacktrace < ActiveRecord::Migration<%= migration_version %>
def change
reversible do |dir|
dir.up do
# Ensure this incremental update migration is idempotent
# with monolithic install migration.
return if connection.column_exists?(:good_job_executions, :error_backtrace)
end
end

add_column :good_job_executions, :error_backtrace, :text, array: true
end
end
1 change: 1 addition & 0 deletions spec/app/jobs/example_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
good_job = GoodJob::Job.find_by(active_job_id: active_job.job_id)
expect(good_job.discrete_executions.count).to eq 3
expect(good_job.discrete_executions.last.error).to be_present
expect(good_job.discrete_executions.last.error_backtrace).to eq(["app/jobs/example_job.rb:41:in `perform'"])
end
end

Expand Down
Loading