Configuring Queues
When specifying a queue, you can optionally pass in some options along with the queue object to customize the queue’s behavior.
There’s currently 3 options you can customize:
maxAttempts
- Default is 1. Increasing this means your job will be retried untilcurrentAttempt <= maxAttempts
.payloadTransformer
- Transform the payload before it’s inserted into the database and when it’s retrieved from the database.pollingInterval
- Default is 2 seconds. This is the interval at which Sidetrack will poll your database for new jobs.
maxAttempts
By increasing the maxAttempts
for a job, you are allowing the job to be retried in the future. All jobs have a default maxAttempts
of 1, so if you want a job to run at most 5 times before being marked as failed, you can set maxAttempts to 5.
import { } from "sidetrack";
const = new <{
: { : string };
}>({
: {
: .["DATABASE_URL"]!,
},
: {
: {
: async (, ) => {
throw new ("intentionally failing job");
},
maxAttempts?: number | undefined
maxAttempts: 5,
},
},
});
.();
.("userOnboarding", { : "a@example.com" });
This job will fail on the first run then move to retrying
status since its currentAttempt
(1) is less than 5. After 5 attempts, the job will transition to a status of failed
.
payloadTransformer
The payloadTransformer
option allows you to transform the payload before it’s inserted into the database and when it’s retrieved from the database. It can be set on the queue or the sidetrack instance. When set on the sidetrack instance, the same transformer will be used for all queues. The queue transformer will take precedence over the sidetrack instance transformer.
import { } from "sidetrack";
import from "superjson";
const = new <{
: { : Date };
}>({
: {
: .["DATABASE_URL"]!,
},
// Transform Date objects when saving to and loading from the database
: {
: () => .stringify(),
: () => .deserialize(),
},
: {
: {
: async () => {
// payload.registeredAt is a Date object, not a string
.(`User registered at ${..()}`);
},
: {
: () => .stringify(),
: () => .deserialize(),
},
},
},
});
.();
// Date objects are automatically handled by the transformer
.("userOnboarding", { : new () });
By default, when storing job payloads in the database, they are serialized to JSON. This works fine for simple objects, but can cause issues with special types like Date
objects which get converted to strings. Using a payloadTransformer
, you can customize how the payload is serialized and deserialized.
In this example, we use superjson to handle the transformation. This allows us to work with Date
objects directly in our job handlers, while ensuring they are properly stored in the database.
pollingInterval
The pollingInterval
option allows you to customize the interval at which Sidetrack will poll your database for new jobs. It can be set on the queue or the sidetrack instance. If set on the sidetrack instance, the same polling interval will be used for all queues. The queue polling interval will take precedence over the sidetrack instance polling interval.
import { } from "sidetrack";
const = new <{
: { : string };
}>({
: {
: .["DATABASE_URL"]!,
},
: {
: {
: async (, ) => {
throw new ("intentionally failing job");
},
pollingInterval?: PollingInterval | undefined
pollingInterval: 3000, // 3 seconds
},
},
SidetrackOptions<{ userOnboarding: { email: string; }; }>.pollingInterval?: PollingInterval | undefined
pollingInterval: 5000, // 5 seconds
});
.();
.("userOnboarding", { : "a@example.com" });