Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

ruby
ror

How to retry or discard failed jobs in ROR

Behzad Ahmad

In ROR, a job fails if we do not cater to an exception that is raised. Once a job fails, it cannot be retried. However, if we change the configuration of a job, we are able to retry a failed job.

We can retry or discard failed jobs with the help of retry_on and discard_on.

Code

The code below shows how to retry or discard jobs.

class Vehicle < ApplicationJob
  retry_on CustomAppException 

  discard_on ActiveJob::DeserializationError

  def perform(*args)
  
  end
end

The default wait time of CustomAppException is 3 seconds. In the case of a CustomAppException in the code, the job is retried.

The DeserializationError occurs when we pass the record, which is deleted after the job is enqueued. When such an error occurs in the code, the job is discarded.

RELATED TAGS

ruby
ror

CONTRIBUTOR

Behzad Ahmad
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring