Cron Jobs
Sidetrack supports scheduling recurring jobs using cron expressions. This allows you to run jobs on a schedule, like sending weekly reports or cleaning up old data.
Scheduling a Cron Job
Use the scheduleCron
method to create a recurring job. You’ll need to specify:
- The queue name
- A cron expression
- The job payload
import { } from "sidetrack";
const = async (: string) => {
.(`Generating report for user ${}`);
};
const = new <{
: { : string };
}>({
: {
: .["DATABASE_URL"]!,
},
: {
: {
: async (, ) => {
await (.);
},
},
},
});
// Run every Monday at 9am
await .("weeklyReport", "0 9 * * 1", { : "123" });
The cron expression can use either 5 (standard cron format) or 6 (non-standard) parts. The 6-part format includes seconds:
┌─────────────── second (0 - 59) (optional)
│ ┌───────────── minute (0 - 59)
│ │ ┌───────────── hour (0 - 23)
│ │ │ ┌───────────── day of month (1 - 31)
│ │ │ │ ┌───────────── month (1 - 12)
│ │ │ │ │ ┌───────────── day of week (0 - 6) (Sunday to Saturday)
│ │ │ │ │ │
│ │ │ │ │ │
* * * * * * (6-part format with seconds)
* * * * * (5-part format without seconds)
You can optionally specify a timezone when scheduling the cron job:
await .(
"weeklyReport",
"0 9 * * 1",
{ : "123" },
{ : "America/New_York" },
);
Managing Cron Schedules
Deactivating a Schedule
To temporarily stop a cron schedule from creating new jobs, use deactivateCronSchedule
:
await .("weeklyReport", "0 9 * * 1");
This will prevent new jobs from being created, but won’t affect any existing jobs that were already scheduled.
Deleting a Schedule
To permanently remove a cron schedule, use deleteCronSchedule
:
await .("weeklyReport", "0 9 * * 1");
This removes the cron schedule from the database entirely. Any existing jobs that were already scheduled will still run.
Common Cron Patterns
Here are some common cron patterns you might find useful:
# Every 30 seconds (6-part format)
30 * * * * *
# Every minute
* * * * *
# Every hour
0 * * * *
# Every day at midnight
0 0 * * *
# Every Monday at 9am
0 9 * * 1
# Every weekday at 9am
0 9 * * 1-5
# First day of every month at midnight
0 0 1 * *