Skip to Content
GuidesInserting Jobs

Inserting Jobs

When inserting jobs with Sidetrack, you can use several options to control how and when jobs are executed. Let’s look at the different ways to insert jobs.

import { } from "sidetrack"; const = new <{ : { : string }; : { : string }; : { : string }; }>({ : { : .["DATABASE_URL"]!, }, : { : { : async () => { .(`Onboarding ${.}`); }, }, : { : async () => { .(`Sending reminder to ${.}`); }, }, : { : async () => { .(`Sending newsletter template ${.}`); }, }, }, });

Basic Job Insertion

The simplest way to insert a job is to specify the queue name and payload:

.("userOnboarding", { : "user@example.com" });

Scheduled Jobs

You can schedule jobs to run at a specific time in the future using the scheduledAt option:

// Schedule a job to run 1 hour from now const = new (.() + 60 * 60 * 1000); .( "sendReminder", { : "user@example.com" }, { : }, );

Unique Jobs

To prevent duplicate jobs from being created, you can use the uniqueKey option. This is useful for operations that should only happen once:

// This will only create one job, even if called multiple times .( "userOnboarding", { : "user@example.com" }, { : "onboard-user-123" }, );

By default, attempting to insert a job with a duplicate uniqueKey will throw an error. You can suppress this error using suppressDuplicateUniqueKeyErrors:

// This won't throw an error if a job with the same uniqueKey exists .( "userOnboarding", { : "user@example.com" }, { : "onboard-user-123", : true, }, );

Combining Options

You can combine these options as needed:

// Schedule a unique job for the future .( "sendNewsletter", { : "weekly-digest" }, { : new ("2024-12-31T00:00:00Z"), : "weekly-newsletter-2024-12-31", : true, }, );

Type Safety

Sidetrack ensures type safety for your job payloads. The payload must match the type you defined when initializing Sidetrack:

// This will type check ✅ .("userOnboarding", { : "user@example.com" }); // This will cause a type error ❌ .("userOnboarding", { name: "John" });
Object literal may only specify known properties, and 'name' does not exist in type '{ email: string; }'.
Last updated on