This is a fork of the original node-cron package. It adds a remove function to a task, to delete it fully.
IMPORTANT: When working with this repository, always set it up to push to github and gitlab at the same time. This is to ensure that the changes are kept in sync.
git remote set-url --add --push origin <github>
git remote set-url --add --push origin <gitlab>The node-cron module is tiny task scheduler in pure JavaScript for node.js based on GNU crontab. This module allows you to schedule task in node.js using full crontab syntax.
Need a job scheduler with support for worker threads and cron syntax? Try out the Bree job scheduler!
Install using npm:
npm install --save @idot-digital/node-cronImport node-cron and schedule a task:
- commonjs
const cron = require('@idot-digital/node-cron');
cron.schedule('* * * * *', () => {
console.log('running a task every minute');
});- es6 (module)
import cron from '@idot-digital/node-cron';
cron.schedule('* * * * *', () => {
console.log('running a task every minute');
});This is a quick reference to cron syntax and also shows the options supported by node-cron.
# ┌────────────── second (optional)
# │ ┌──────────── minute
# │ │ ┌────────── hour
# │ │ │ ┌──────── day of month
# │ │ │ │ ┌────── month
# │ │ │ │ │ ┌──── day of week
# │ │ │ │ │ │
# │ │ │ │ │ │
# * * * * * *
| field | value |
|---|---|
| second | 0-59 |
| minute | 0-59 |
| hour | 0-23 |
| day of month | 1-31 |
| month | 1-12 (or names) |
| day of week | 0-7 (or names, 0 or 7 are sunday) |
You may use multiples values separated by comma:
import cron from '@idot-digital/node-cron';
cron.schedule('1,2,4,5 * * * *', () => {
console.log('running every minute 1, 2, 4 and 5');
});You may also define a range of values:
import cron from '@idot-digital/node-cron';
cron.schedule('1-5 * * * *', () => {
console.log('running every minute to 1 from 5');
});Step values can be used in conjunction with ranges, following a range with '/' and a number. e.g: 1-10/2 that is the same as 2,4,6,8,10. Steps are also permitted after an asterisk, so if you want to say “every two minutes”, just use */2.
import cron from '@idot-digital/node-cron';
cron.schedule('*/2 * * * *', () => {
console.log('running a task every two minutes');
});For month and week day you also may use names or short names. e.g:
import cron from '@idot-digital/node-cron';
cron.schedule('* * * January,September Sunday', () => {
console.log('running on Sundays of January and September');
});Or with short names:
import cron from '@idot-digital/node-cron';
cron.schedule('* * * Jan,Sep Sun', () => {
console.log('running on Sundays of January and September');
});Schedules given task to be executed whenever the cron expression ticks.
Arguments:
- expression
string: Cron expression - function
Function: Task to be executed - options
Object: Optional configuration for job scheduling.
- scheduled: A
booleanto set if the created task is scheduled. Defaulttrue; - recoverMissedExecutions: A
booleanto set if the created task should be able to recover missed executions. Defaultfalse; - timezone: The timezone that is used for job scheduling. See IANA time zone database for valid values, such as
Asia/Shanghai,Asia/Kolkata,America/Sao_Paulo.
Example:
import cron from '@idot-digital/node-cron';
cron.schedule(
'0 1 * * *',
() => {
console.log('Running a job at 01:00 at America/Sao_Paulo timezone');
},
{
scheduled: true,
timezone: 'America/Sao_Paulo',
}
);Starts the scheduled task.
import cron from '@idot-digital/node-cron';
const task = cron.schedule(
'* * * * *',
() => {
console.log('stopped task');
},
{
scheduled: false,
}
);
task.start();The task won't be executed unless re-started.
import cron from '@idot-digital/node-cron';
const task = cron.schedule('* * * * *', () => {
console.log('will execute every minute until stopped');
});
task.stop();Validate that the given string is a valid cron expression.
import cron from '@idot-digital/node-cron';
const valid = cron.validate('59 * * * *');
const invalid = cron.validate('60 * * * *');You can name your tasks to make it easier to identify them in the logs.
import cron from '@idot-digital/node-cron';
const task = cron.schedule(
'* * * * *',
() => {
console.log('will execute every minute until stopped');
},
{
name: 'my-task',
}
);You can list all the tasks that are currently running.
import cron from '@idot-digital/node-cron';
const tasks = cron.getTasks();
for (let [key, value] of tasks.entries()) {
console.log('key', key);
console.log('value', value);
}value is an object with the following properties:
- _events
- _eventsCount
- _maxListeners
- options
- _task
- etc...
This is an interal fork, so please report any issues with the original package to the original repository. Otherwise the fork is not maintained.
Contributions to this fork are not accepted. Please contribute to the original repository.
This project exists thanks to all the people who contribute.
Thank you to all our backers! 🙏 [Become a backer]
Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [Become a sponsor]
This fork is licensed under ISC same as the original repository. See the LICENSE file for details.