Skip to content

Instantly share code, notes, and snippets.

@latortuga
Forked from nisanthchunduru/delete_failed_jobs.rb
Created September 15, 2022 18:37
Show Gist options
  • Save latortuga/9608ef2f25a8d49aa04304086adf062a to your computer and use it in GitHub Desktop.
Save latortuga/9608ef2f25a8d49aa04304086adf062a to your computer and use it in GitHub Desktop.
Selectively remove/retry failed jobs in Resque 1.x
def delete_failed_job_if
redis = Resque.redis
(0...Resque::Failure.count).each do |i|
string = redis.lindex(:failed, i)
break if string.nil?
job = Resque.decode(string)
should_delete_job = yield job
next unless should_delete_job
redis.lrem(:failed, 1, string)
redo
end
end
delete_failed_job_if do |job|
job['payload']['class'] == 'SendPushNotification' &&
job['exception'] == 'Pusher::HTTPError'
end
def retry_failed_job_if
redis = Resque.redis
(0...Resque::Failure.count).each do |i|
string = redis.lindex(:failed, i)
break if string.nil?
job = Resque.decode(string)
should_retry_job = yield job
next unless should_retry_job
puts "Retrying job with index #{i}"
Resque::Failure.requeue(i)
end
end
retry_failed_job_if do |job|
job['payload']['class'] == 'MailImport' &&
job['exception'] == 'NoMethodError'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment