Retries

In a normal queue operation it is inevitable that jobs will eventually fail. Failed jobs will by default en on the "failed" set (unless removeOnFailed is enabled). However, it is often useful to perform a number of retries before the job is finally considered failed.

In order to enable retries we need to specify a max number of attempts to retry a job, and which backoff strategy to use. These are options passed inside the "opts" field of our job payload following this interface (for simplicity we will hide the rest of the available options for now):

interface JobOpts {
  attempts: number;
  backoff: {
    type: "exponential" | "linear",
    delay: number // Time in milliseconds
  }
  // ... More opts
}

We can therefore enable retries by sending these options when adding a job, so for instance if we want to retry up to 5 times with an exponential backoff that starts with 1 second we would send the job like this:

curl --location 'http://mydomain.dev/queues/my-queue/jobs' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer my-secret-token' \
--data '[{
	"name": "paint-red",
        "data": {},
        "opts": {
        "attempts": 5,
        "backoff": {
          "type": "exponential",
          "delay": 1000
        }
}]'

Now, if this job would fail when processed, it will retry up to 5 times following an exponential backoff (retrying after 1 second, 2 seconds, 4, seconds and so on).

Last updated