Retrying / re-enqueuing active jobs and change parameters

147 views
Skip to first unread message

Cyril Duchon-Doris

unread,
May 3, 2017, 6:27:41 AM5/3/17
to Ruby on Rails: Core
Feature suggestion :

Currently ActiveJob allows to retry jobs with `retry_job queue` allowing to set the `:queue`, `wait` or `wait_until`

It would be nice to allow changing/adding parameters during the retry.

Example :
I want to retry a job up to 5 times, each time with a bigger interval between the retries.
I would pass an optional/keyword parameter `retry_count` to the job signature, and depending on the value I would schedule the retry further in time

```ruby
class MyJob < ApplicationJob
  rescue_from(JobError) do
    reschedule_time.try do |wait_time|
      retry_job(
        retry_count: (@retry_count + 1), # Change retry_count
        wait: wait_time
      )
    end
  end

  def perform(*args, retry_count: 0)
    @retry_count = retry_count
    raise JobError
  end

  def reschedule_time
    # This can eventually be configured in a Settings class / per user config
    case retry_count
    when 0..2
      1.day
    when 3..4
      5.days
    when 5
      2.weeks
    else
      nil
    end
  end
end
```

Right now what I am doing is just calling the Job name again

```ruby
rescue_from(JobError) do
  reschedule_time.try do |wait_time|
      MyJob
      .set(wait_until: wait_duration.from_now)
      .perform_later(
        retry_count: (@retry_count + 1)
      )
  end
end
```

A typical use for this would be automation flows where we want to keep sending emails until an action is performed : reminders, etc.

Linked to https://github.com/rails/rails/issues/28347
Reply all
Reply to author
Forward
0 new messages