Retry a subset of failed jobs in Resque 1.x

Retrying failed jobs via Resque’s web interface is pretty tiresome, especially when there are more than a handful of them. So I’ve quickly written down a small script that’ll do it for me.

Copy the snippet below and paste it in a rails console

def retry_if(&should_retry)
  redis = Resque.redis

  (0...Resque::Failure.count).each do |i|
    serialized_job = redis.lindex(:failed, i)
    job = Resque.decode(serialized_job)

    next unless should_retry.(job)
    Resque::Failure.requeue(i)
  end
end

To retry a subset of failed jobs, say email notifications that have failed because of smtp errors

retry_if do |job|
  job['payload']['class'] == 'SendEmailNotifications' && job['exception'] == 'Net::SMTPServerBusy'
end

If you want to skip jobs that have already been retried

retry_if do |job|
  if job['payload']['class'] == 'SendEmailNotifications' && job['exception'] == 'Net::SMTPServerBusy'
    if !job['retried_at']
      next true
    end
  end

  false
end

I might eventually move this to a rake task, but that’s a blog post for the future.



blog comments powered by Disqus
Nisanth Chunduru
Nisanth Chunduru