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.ts2import { scheduler } from "canxjs";34// Schedule a closure to run every minute5scheduler.call(() => {6console.log("Running every minute");7}).everyMinute();89// Schedule a command10scheduler.command("bun run cleanup").dailyAt("02:00");1112// Advanced chaining13scheduler.call(async () => {14await 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