Advanced

Task Scheduling

Define your scheduled tasks directly in code using a fluent API, instead of managing raw crontab entries.

Defining Schedules

You can define tasks using scheduler.call() or scheduler.command().

src/schedule.ts
1// src/app.ts or src/schedule.ts
2import { scheduler } from "canxjs";
3
4// Schedule a closure to run every minute
5scheduler.call(() => {
6 console.log("Running every minute");
7}).everyMinute();
8
9// Schedule a command
10scheduler.command("bun run cleanup").dailyAt("02:00");
11
12// Advanced chaining
13scheduler.call(async () => {
14 await db.users.deleteExpired();
15})
16.name("cleanup-users")
17.withoutOverlapping()
18.every("5m");

Running the Scheduler

On your server, you only need to add a single cron entry that runs the `canx schedule:run` command every minute.

Terminal/Crontab
# Run the scheduler (manual)
bun run canx schedule:run
# Setup Cron (Linux/Mac)
* * * * * cd /path/to/project && bun run canx schedule:run >> /dev/null 2>&1

Next Steps

Optimize your application performance with caching.