A lightweight, flexible library for managing scheduled tasks with Vercel Cron Jobs. This library allows you to define tasks with various scheduling options directly in your code.
- π Flexible Scheduling: Schedule tasks by day of week, date of month, month, or week number
- π Simple API: Register tasks with a clean, intuitive API
- π Zero Dependencies: No external dependencies required
- β‘ Vercel Integration: Works seamlessly with Vercel Cron Jobs
- π§© Composable: Combine different scheduling options for complex patterns
-
Add the library files to your Next.js project:
lib/cron-scheduler.ts- Core libraryapp/api/cron/route.ts- Vercel cron job endpointvercel.json- Vercel configuration
-
Configure your cron schedule in
vercel.json:
{
"crons": [
{
"path": "/api/cron",
"schedule": "0 0 * * *"
}
]
}Create a file to define your tasks (e.g., lib/tasks.ts):
import { registerTask } from "./cron-scheduler";
// Daily task (runs every day)
registerTask("daily-task", {
name: "Daily Task",
handler: async () => {
// Your task implementation
console.log("Running daily task...");
return { status: "completed" };
},
schedule: {}, // Empty schedule means run every time
});Import this file early in your application lifecycle to register the tasks:
// In app/layout.tsx or similar
import "../lib/tasks";The library supports various scheduling options that can be combined:
registerTask("weekly-task", {
name: "Weekly Task",
handler: async () => {
// Your task implementation
return { status: "completed" };
},
schedule: {
days: ["monday", "wednesday", "friday"],
},
});registerTask("monthly-task", {
name: "Monthly Task",
handler: async () => {
// Your task implementation
return { status: "completed" };
},
schedule: {
dates: [1, 15], // Runs on the 1st and 15th of each month
},
});registerTask("quarterly-task", {
name: "Quarterly Task",
handler: async () => {
// Your task implementation
return { status: "completed" };
},
schedule: {
months: ["january", "april", "july", "october"],
},
});registerTask("biweekly-task", {
name: "Biweekly Task",
handler: async () => {
// Your task implementation
return { status: "completed" };
},
schedule: {
weekNumbers: [1, 3], // Runs on the 1st and 3rd week of each month
},
});registerTask("complex-task", {
name: "Complex Task",
handler: async () => {
// Your task implementation
return { status: "completed" };
},
schedule: {
days: ["monday"],
months: ["january", "july"],
weekNumbers: [2, 4], // 2nd and 4th Monday in January and July
},
});Registers a new task with the specified ID and definition.
Parameters:
taskId: A unique identifier for the tasktask: The task definition object
Removes a task from the registry.
Parameters:
taskId: The ID of the task to remove
Returns all registered tasks.
interface TaskDefinition {
name: string; // Human-readable name
handler: () => Promise<any>; // Function to execute
schedule: {
// When to run the task
days?: string[]; // Days of week (e.g., "monday")
dates?: number[]; // Dates of month (e.g., 1, 15)
months?: string[]; // Months (e.g., "january")
weekNumbers?: number[]; // Week numbers in month (e.g., 1, 3)
};
}Deploy your project to Vercel to activate the cron job:
vercel deploy --prodImportant notes:
- Cron jobs only run in production deployments, not in preview deployments
- Vercel cron jobs are triggered by making an HTTP GET request to the specified path
- The cron job will run according to the schedule specified in
vercel.json
registerTask("db-backup", {
name: "Database Backup",
handler: async () => {
// Connect to your database
// Create a backup
// Store the backup
return { status: "Backup completed", timestamp: new Date().toISOString() };
},
schedule: {
days: ["monday", "wednesday", "friday"],
},
});registerTask("newsletter", {
name: "Weekly Newsletter",
handler: async () => {
// Fetch newsletter content
// Get subscriber list
// Send emails
return { status: "Newsletter sent", recipientCount: 1250 };
},
schedule: {
days: ["friday"],
// Only send at 9am - this is handled by the Vercel cron schedule
// in vercel.json, not by this library
},
});registerTask("monthly-report", {
name: "Monthly Report",
handler: async () => {
// Generate reports
// Store in database
// Notify stakeholders
return { status: "Reports generated" };
},
schedule: {
dates: [1], // First day of each month
},
});MIT