cronz-scheduler
Version:
A lightweight cron and interval scheduler for Node.js written in TypeScript.
187 lines (147 loc) โข 4.86 kB
Markdown
A lightweight, zero-dependency TypeScript/JavaScript cron and interval scheduler for Node.js.
> ๐ ๏ธ Schedule cron-based and interval-based jobs โ with optional auto-expiration.
---
```bash
npm install cronz-scheduler
```
- โ
Supports standard cron expressions (like * * * * *)
- โ
Schedule jobs using seconds based intervals (5, 10, etc.)
- โ
Optional endAt to auto stop jobs after a specific time
- โ
Manually stop any job
- โ
Lightweight and fast no external dependencies
- โ
Fully written in TypeScript
# ๐ง Cron Expression Format
```bash
* * * * *
โ โ โ โ โโโ Day of week (0 - 6) (Sunday = 0)
โ โ โ โโโโโโโ Month (1 - 12)
โ โ โโโโโโโโโโโโ Day of month (1 - 31)
โ โโโโโโโโโโโโโโโโโ Hour (0 - 23)
โโโโโโโโโโโโโโโโโโโโโโ Minute (0 - 59)
```
# โจ Usage
```ts
const { CronScheduler } = require("cronz-scheduler");
const scheduler = new CronScheduler();
/**
* 1. Cron job: Every minute
*/
scheduler.addJob("every-minute", "* * * * *", () => {
console.log(
"[every-minute] Running every minute:",
new Date().toLocaleTimeString()
);
});
/**
* 2. Cron job: Every day at 3:30 AM
*/
scheduler.addJob("daily-3-30am", "30 3 * * *", () => {
console.log("[daily-3-30am] It's 3:30 AM!");
});
/**
* 3. Cron job: Every Monday at 12 PM
*/
scheduler.addJob("weekly-monday-noon", "0 12 * * 1", () => {
console.log("[weekly-monday-noon] It's Monday Noon!");
});
/**
* 4. Cron job: Every 5 minutes
*/
scheduler.addJob("every-5-min", "*/5 * * * *", () => {
console.log("[every-5-min] Every 5 minutes");
});
/**
* 5. Cron job: On 1st of every month at midnight
*/
scheduler.addJob("monthly-midnight", "0 0 1 * *", () => {
console.log("[monthly-midnight] First day of the month at midnight!");
});
/**
* 6. Cron job with `endAt`: Stop after 2 minutes
*/
scheduler.addJob(
"limited-cron",
"* * * * *",
() => {
console.log("[limited-cron] This will run only for 2 minutes");
},
{
endAt: new Date(Date.now() + 2 * 60_000),
}
);
/**
* 7. Interval job: Every 10 seconds
*/
scheduler.addJob("interval-10s", 10, () => {
console.log("[interval-10s] Runs every 10 seconds");
});
/**
* 8. Interval job with `endAt`: Runs every 3 seconds, stops after 15 seconds
*/
scheduler.addJob(
"temp-interval",
3,
() => {
console.log("[temp-interval] Runs every 3s until endAt");
},
{
endAt: new Date(Date.now() + 15_000),
}
);
/**
* 9. Manual stop after 20 seconds
*/
setTimeout(() => {
scheduler.stopJob("interval-10s");
console.log("[manual-stop] Stopped interval-10s manually");
}, 20_000);
// Start the scheduler
scheduler.start();
```
| Description | Expression |
| ------------------------- | ------------- |
| Every minute | `* * * * *` |
| Daily at 9:00 AM | `0 9 * * *` |
| Every Monday at 12:00 PM | `0 12 * * 1` |
| Every 5 minutes | `*/5 * * * *` |
| On the 1st of every month | `0 0 1 * *` |
| Every weekday at 9 AM | `0 9 * * 1-5` |
Adds a new job to the scheduler.
| Parameter | Type | Description |
|------------------------|---------------------|---------------------------------------------------------------------------|
| `name` | `string` | Unique name for the job. |
| `expressionOrSeconds` | `string \| number` | Cron expression (`* * * * *`) or number of seconds for interval jobs. |
| `callback` | `() => void` | Function to execute when the job triggers. |
| `options.endAt` | `Date` _(optional)_ | If provided, the job will auto-stop after this time. |
```ts
scheduler.addJob("every-5-seconds", 5, () => {
console.log("Runs every 5 seconds");
});
scheduler.addJob(
"daily-job",
"0 9 * * *",
() => console.log("Runs every day at 9 AM"),
{ endAt: new Date(Date.now() + 60_000) } // optional
);
```
Begins evaluating and running scheduled jobs.
```ts
scheduler.start();
```
Stops a job by its name.
```ts
scheduler.stopJob("interval");
```
MIT ยฉ Ahmad Hassan Khan