Skip to content

Reset Active Job's execution and exception_executions counters when retrying #239

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

Merged
merged 1 commit into from
Jun 11, 2024
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
1 change: 1 addition & 0 deletions app/models/solid_queue/failed_execution.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def self.retry_all(jobs)
def retry
SolidQueue.instrument(:retry, job_id: job.id) do
with_lock do
job.reset_execution_counters
job.prepare_for_execution
destroy!
end
Expand Down
12 changes: 1 addition & 11 deletions app/models/solid_queue/job/executable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@ module Executable
extend ActiveSupport::Concern

included do
include ConcurrencyControls, Schedulable
include ConcurrencyControls, Schedulable, Retryable

has_one :ready_execution
has_one :claimed_execution
has_one :failed_execution

after_create :prepare_for_execution

scope :finished, -> { where.not(finished_at: nil) }
scope :failed, -> { includes(:failed_execution).where.not(failed_execution: { id: nil }) }
end

class_methods do
Expand Down Expand Up @@ -97,18 +95,10 @@ def status
end
end

def retry
failed_execution&.retry
end

def discard
execution&.discard
end

def failed_with(exception)
FailedExecution.create_or_find_by!(job_id: id, exception: exception)
end

private
def ready
ReadyExecution.create_or_find_by!(job_id: id)
Expand Down
29 changes: 29 additions & 0 deletions app/models/solid_queue/job/retryable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

module SolidQueue
class Job
module Retryable
extend ActiveSupport::Concern

included do
has_one :failed_execution

scope :failed, -> { includes(:failed_execution).where.not(failed_execution: { id: nil }) }
end

def retry
failed_execution&.retry
end

def failed_with(exception)
FailedExecution.create_or_find_by!(job_id: id, exception: exception)
end

def reset_execution_counters
arguments["executions"] = 0
arguments["exception_executions"] = {}
save!
end
end
end
end
4 changes: 2 additions & 2 deletions test/dummy/app/jobs/raising_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ class DiscardableError < StandardError; end
retry_on DefaultError, attempts: 3, wait: 0.1.seconds
discard_on DiscardableError

def perform(raising, identifier, attempts = 1)
def perform(raising, identifier, fail_count = 1)
raising = raising.shift if raising.is_a?(Array)

if raising && executions <= attempts
if raising && executions <= fail_count
JobBuffer.add("#{identifier}: raised #{raising} for the #{executions.ordinalize} time")
raise raising, "This is a #{raising} exception"
else
Expand Down
20 changes: 20 additions & 0 deletions test/integration/jobs_lifecycle_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,26 @@ class JobsLifecycleTest < ActiveSupport::TestCase
assert_equal 1, SolidQueue::FailedExecution.count
end

test "retry job that failed after being automatically retried" do
RaisingJob.perform_later(RaisingJob::DefaultError, "A", 5)

@dispatcher.start
@worker.start

wait_for_jobs_to_finish_for(3.seconds)

assert_equal 2, SolidQueue::Job.finished.count # 2 retries of A
assert_equal 1, SolidQueue::FailedExecution.count

failed_execution = SolidQueue::FailedExecution.last
failed_execution.job.retry

wait_for_jobs_to_finish_for(3.seconds)

assert_equal 4, SolidQueue::Job.finished.count # Add other 2 retries of A
assert_equal 1, SolidQueue::FailedExecution.count
end

test "enqueue and run jobs that fail and it's discarded" do
RaisingJob.perform_later(RaisingJob::DiscardableError, "A")

Expand Down
Loading