Retrying / re-enqueuing active jobs and change parameters

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Retrying / re-enqueuing active jobs and change parameters

Cyril Duchon-Doris
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

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.