cron-task
v1.0.6
Published
A simple cron task library for node.js with some useful features. Like assigning a name to task and getting the task by name. or category to task and getting the task by category.
Maintainers
Readme
cron-task
cron-task is a module for scheduling tasks at specific time intervals using cron expressions or milliseconds. You can easily add, remove, start, and stop tasks.
Installation
To install cron-task, run the following command:
npm install cron-taskUsage
const { CronTaskScheduler } = require('cron-task');
// Create scheduler instance
const scheduler = new CronTaskScheduler();
// Add a simple "Hello World" task that runs every 3 seconds
scheduler.addTask({
name: 'helloWorldTask',
interval: 3000, // 3 seconds in milliseconds
onTick: () => {
console.log('Hello World!', new Date().toLocaleTimeString());
}
});
console.log('Scheduler started. Press Ctrl+C to stop.');API
Task class
task: Object describing the task with the following properties:id: unique uuid for task (this not be send on creation)name: Unique name for the task.category(optional): Task category.interval: Interval in milliseconds or cron expression.onTick: Function executed at each interval.
Methods
addTask(task: Omit<Task, 'id'>): TaskgetTasks(): Task[]getTaskById(id:string): voidgetTasksByName(name: string): Task[]getTasksByCategory(category: string): Task[]removeTaskById(id:string): voidremoveTaskByName(name: string): voidremoveTasksByCategory(category: string): voidremoveAllTasks(): voidstartTaskById(id: string): voidstartTaskByName(name: string): voidstopTaskByName(name: string): voidstopAllTasksByCategory(category: string): voidupdateTaskById(id:string): void
Cron Expression
* * * * *
- - - - -
| | | | |
| | | | +---- Day of the week (0 - 7) (Sunday=0 or 7)
| | | +------ Month (1 - 12)
| | +-------- Day of the month (1 - 31)
| +---------- Hour (0 - 23)
+------------ Minute (0 - 59)Contribution
If you encounter any issues or have suggestions, feel free to open an issue or submit a pull request.
Thanks for contributing!
