Skip to content
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## [Unreleased]

### Fixed

- Retrying a failed job could leave it with no execution at all, so it never ran again and was shown as pending forever. The monitor moved the job back to the ready queue and deleted its failed execution itself, inside an `ActiveRecord::Base` transaction. That is the app's primary database: when Solid Queue has its own database (`config.solid_queue.connects_to`, the Rails 8 default), the two steps committed separately. A worker could pick the job up in between and fail it again, Solid Queue recorded that failure on the same failed execution, and the monitor then deleted it. Retry, per row and bulk, now calls Solid Queue's own `FailedExecution#retry`, which does both in one transaction on Solid Queue's database. Retried jobs now also get their execution counters reset (so `retry_on ... attempts:` starts over) and respect `limits_concurrency`, as a retry from Solid Queue itself does. Retrying a failed job that someone else retried or discarded after it was loaded no longer raises an error, and a bulk retry now counts a job whose retry raises (e.g. a lock timeout) as failed and carries on with the rest instead of stopping halfway with an error page. The service's success message now reads "Job queued for retry", since a job with a concurrency limit can go to blocked executions instead of the ready queue.

## [2.4.0] - 2026-11-17

### Added
Expand Down
33 changes: 10 additions & 23 deletions app/services/solid_queue_monitor/failed_job_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,16 @@ class FailedJobService
def retry_job(failed_execution_id)
failed_execution = SolidQueue::FailedExecution.find_by(id: failed_execution_id)
return { success: false, message: 'Failed job not found' } unless failed_execution
return { success: false, message: 'Associated job not found' } unless failed_execution.job

job = failed_execution.job
return { success: false, message: 'Associated job not found' } unless job

ActiveRecord::Base.transaction do
SolidQueue::ReadyExecution.create!(
job_id: job.id,
queue_name: get_queue_name(failed_execution, job),
priority: job.priority
)

failed_execution.destroy!
end
# Solid Queue's retry runs in one transaction on its own database; doing it
# here with ActiveRecord::Base.transaction left orphaned jobs when the queue
# has a separate database.
failed_execution.retry

{ success: true, message: 'Job moved to ready queue for retry' }
{ success: true, message: 'Job queued for retry' }
rescue ActiveRecord::RecordNotFound
{ success: false, message: 'Failed job not found' }
end

def discard_job(failed_execution_id)
Expand Down Expand Up @@ -51,6 +46,8 @@ def retry_all(job_ids)
else
failed_count += 1
end
rescue StandardError
failed_count += 1
end

if success_count.positive? && failed_count.zero?
Expand Down Expand Up @@ -85,15 +82,5 @@ def discard_all(job_ids)
{ success: false, message: 'Failed to discard jobs' }
end
end

private

def get_queue_name(failed_execution, job)
if failed_execution.respond_to?(:queue_name) && failed_execution.queue_name.present?
failed_execution.queue_name
else
job.queue_name
end
end
end
end
81 changes: 81 additions & 0 deletions spec/services/solid_queue_monitor/failed_job_service_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe SolidQueueMonitor::FailedJobService do
subject(:service) { described_class.new }

describe '#retry_job' do
let(:job) { create(:solid_queue_job, queue_name: 'mailers', priority: 3) }
let!(:failed_execution) { create(:solid_queue_failed_execution, job: job) }

it 'moves the job to the ready queue and removes the failed execution' do
result = service.retry_job(failed_execution.id)

expect(result).to eq(success: true, message: 'Job queued for retry')
expect(SolidQueue::FailedExecution.exists?(failed_execution.id)).to be(false)
expect(SolidQueue::ReadyExecution.find_by(job_id: job.id))
.to have_attributes(queue_name: 'mailers', priority: 3)
end

it "delegates to Solid Queue's FailedExecution#retry instead of creating the ready execution itself" do
allow(SolidQueue::ReadyExecution).to receive(:create!)
retried = []
allow_any_instance_of(SolidQueue::FailedExecution).to receive(:retry) { |execution| retried << execution.id }

service.retry_job(failed_execution.id)

expect(retried).to eq([failed_execution.id])
expect(SolidQueue::ReadyExecution).not_to have_received(:create!)
end

it 'reports a failed execution that no longer exists' do
expect(service.retry_job(0)).to eq(success: false, message: 'Failed job not found')
end

it 'reports a failed execution removed after it was loaded' do
allow(SolidQueue::FailedExecution).to receive(:find_by).and_wrap_original do |original, *args|
original.call(*args).tap { |execution| SolidQueue::FailedExecution.where(id: execution.id).delete_all }
end

expect(service.retry_job(failed_execution.id)).to eq(success: false, message: 'Failed job not found')
expect(SolidQueue::ReadyExecution.where(job_id: job.id)).to be_empty
end

it 'reports a failed execution whose job is gone' do
SolidQueue::Job.where(id: job.id).delete_all

expect(service.retry_job(failed_execution.id)).to eq(success: false, message: 'Associated job not found')
end
end

describe '#retry_all' do
let!(:first) { create(:solid_queue_failed_execution) }
let!(:second) { create(:solid_queue_failed_execution) }

it 'retries every selected job' do
result = service.retry_all([first.id, second.id])

expect(result).to eq(success: true, message: 'All selected jobs have been queued for retry')
expect(SolidQueue::FailedExecution.count).to eq(0)
expect(SolidQueue::ReadyExecution.pluck(:job_id)).to contain_exactly(first.job_id, second.job_id)
end

it 'counts jobs that could not be retried' do
result = service.retry_all([first.id, 0])

expect(result).to eq(success: true, message: '1 jobs queued for retry, 1 failed')
end

it 'counts a job whose retry raises as failed and still retries the rest' do
allow(SolidQueue::FailedExecution).to receive(:find_by).and_call_original
allow(SolidQueue::FailedExecution).to receive(:find_by).with(id: first.id)
.and_raise(ActiveRecord::LockWaitTimeout)

result = service.retry_all([first.id, second.id])

expect(result).to eq(success: true, message: '1 jobs queued for retry, 1 failed')
expect(SolidQueue::ReadyExecution.pluck(:job_id)).to eq([second.job_id])
end
end
end
9 changes: 5 additions & 4 deletions spec/support/solid_queue_stubs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,12 @@ class FailedExecution < ApplicationRecord
self.table_name = 'solid_queue_failed_executions'
belongs_to :job

# Mirrors SolidQueue::FailedExecution#retry: one locked transaction.
def retry
# Stub implementation for testing
job&.update(finished_at: nil)
destroy
true
with_lock do
ReadyExecution.create!(job_id: job_id, queue_name: job.queue_name, priority: job.priority)
destroy!
end
end

def discard
Expand Down
Loading