Gathering detailed insights and metrics for node-schedule
Gathering detailed insights and metrics for node-schedule
Gathering detailed insights and metrics for node-schedule
Gathering detailed insights and metrics for node-schedule
@types/node-schedule
TypeScript definitions for node-schedule
node-schedule-bin
Using node-schedule as a global binary to schedule jobs without additional code.
node-schedule-tz
A cron-like and not-cron-like job scheduler with timezone support for Node.
goldwasher-schedule
Scheduled goldwasher requests, using goldwasher-needle and node-schedule.
A cron-like and not-cron-like job scheduler for Node.
npm install node-schedule
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
9,131 Stars
380 Commits
515 Forks
111 Watching
9 Branches
49 Contributors
Updated on 27 Nov 2024
JavaScript (100%)
Cumulative downloads
Total Downloads
Last day
-2.6%
353,216
Compared to previous day
Last week
1.6%
1,816,326
Compared to previous week
Last month
10.7%
7,584,694
Compared to previous month
Last year
26.7%
63,865,998
Compared to previous year
3
Node Schedule is a flexible cron-like and not-cron-like job scheduler for Node.js. It allows you to schedule jobs (arbitrary functions) for execution at specific dates, with optional recurrence rules. It only uses a single timer at any given time (rather than reevaluating upcoming jobs every second/minute).
Node 6+ is supported.
Node Schedule is for time-based scheduling, not interval-based scheduling.
While you can easily bend it to your will, if you only want to do something like
"run this function every 5 minutes", toad-scheduler would be a better choice. But if you want to, say, "run this function at the :20
and :50 of every hour on the third Tuesday of every month," you'll find that
Node Schedule suits your needs better. Additionally, Node Schedule has Windows
support, unlike true cron
, since the node runtime is now fully supported.
Note that Node Schedule is designed for in-process scheduling, i.e. scheduled jobs will only fire as long as your script is running, and the schedule will disappear when execution completes. If you need to schedule jobs that will persist even when your script isn't running, consider using actual cron.
In case you need durable jobs that persist across restarts and lock system compatible with multi-node deployments, try agenda or bree.
You can install using npm.
npm install node-schedule
Every scheduled job in Node Schedule is represented by a Job
object. You can
create jobs manually, then execute the schedule()
method to apply a schedule,
or use the convenience function scheduleJob()
as demonstrated below.
Job
objects are EventEmitter
s, and emit the following events:
run
event after each execution.scheduled
event each time they're scheduled to run.canceled
event when an invocation is canceled before it's executed.canceled
is the single-L American spelling.error
event when a job invocation triggered by a schedule throws or returns
a rejected Promise
.success
event when a job invocation triggered by a schedule returns successfully or
returns a resolved Promise
. In any case, the success
event receives the value returned
by the callback or in case of a promise, the resolved value.(Both the scheduled
and canceled
events receive a JavaScript date object as
a parameter).
Note that jobs are scheduled the first time immediately, so if you create a job
using the scheduleJob()
convenience method, you'll miss the first scheduled
event, but you can query the invocation manually (see below).
The cron format consists of:
* * * * * *
┬ ┬ ┬ ┬ ┬ ┬
│ │ │ │ │ │
│ │ │ │ │ └ day of week (0 - 7) (0 or 7 is Sun)
│ │ │ │ └───── month (1 - 12)
│ │ │ └────────── day of month (1 - 31)
│ │ └─────────────── hour (0 - 23)
│ └──────────────────── minute (0 - 59)
└───────────────────────── second (0 - 59, OPTIONAL)
Examples with the cron format:
1const schedule = require('node-schedule'); 2 3const job = schedule.scheduleJob('42 * * * *', function(){ 4 console.log('The answer to life, the universe, and everything!'); 5});
Execute a cron job when the minute is 42 (e.g. 19:42, 20:42, etc.).
And:
1const job = schedule.scheduleJob('0 17 ? * 0,4-6', function(){ 2 console.log('Today is recognized by Rebecca Black!'); 3});
Execute a cron job every 5 Minutes = */5 * * * *
You can also get when it is scheduled to run for every invocation of the job:
1const job = schedule.scheduleJob('0 1 * * *', function(fireDate){ 2 console.log('This job was supposed to run at ' + fireDate + ', but actually ran at ' + new Date()); 3});
This is useful when you need to check if there is a delay of the job invocation when the system is busy, or save a record of all invocations of a job for audit purpose.
Currently, W
(nearest weekday) and L
(last day of month/week) are not supported.
Most other features supported by popular cron implementations should work just fine,
including #
(nth weekday of the month).
cron-parser is used to parse crontab instructions.
Say you very specifically want a function to execute at 5:30am on December 21, 2012. Remember - in JavaScript - 0 - January, 11 - December.
1const schedule = require('node-schedule'); 2const date = new Date(2012, 11, 21, 5, 30, 0); 3 4const job = schedule.scheduleJob(date, function(){ 5 console.log('The world is going to end today.'); 6});
To use current data in the future you can use binding:
1const schedule = require('node-schedule'); 2const date = new Date(2012, 11, 21, 5, 30, 0); 3const x = 'Tada!'; 4const job = schedule.scheduleJob(date, function(y){ 5 console.log(y); 6}.bind(null,x)); 7x = 'Changing Data';
This will log 'Tada!' when the scheduled Job runs, rather than 'Changing Data', which x changes to immediately after scheduling.
You can build recurrence rules to specify when a job should recur. For instance, consider this rule, which executes the function every hour at 42 minutes after the hour:
1const schedule = require('node-schedule'); 2 3const rule = new schedule.RecurrenceRule(); 4rule.minute = 42; 5 6const job = schedule.scheduleJob(rule, function(){ 7 console.log('The answer to life, the universe, and everything!'); 8});
You can also use arrays to specify a list of acceptable values, and the Range
object to specify a range of start and end values, with an optional step parameter.
For instance, this will print a message on Thursday, Friday, Saturday, and Sunday at 5pm:
1const rule = new schedule.RecurrenceRule(); 2rule.dayOfWeek = [0, new schedule.Range(4, 6)]; 3rule.hour = 17; 4rule.minute = 0; 5 6const job = schedule.scheduleJob(rule, function(){ 7 console.log('Today is recognized by Rebecca Black!'); 8});
Timezones are also supported. Here is an example of executing at the start of every day in the UTC timezone.
1const rule = new schedule.RecurrenceRule(); 2rule.hour = 0; 3rule.minute = 0; 4rule.tz = 'Etc/UTC'; 5 6const job = schedule.scheduleJob(rule, function(){ 7 console.log('A new day has begun in the UTC timezone!'); 8});
A list of acceptable tz (timezone) values can be found at https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
second (0-59)
minute (0-59)
hour (0-23)
date (1-31)
month (0-11)
year
dayOfWeek (0-6) Starting with Sunday
tz
Note: It's worth noting that the default value of a component of a recurrence rule is
null
(except for second, which is 0 for familiarity with cron). If we did not explicitly setminute
to 0 above, the message would have instead been logged at 5:00pm, 5:01pm, 5:02pm, ..., 5:59pm. Probably not what you want.
To make things a little easier, an object literal syntax is also supported, like in this example which will log a message every Sunday at 2:30pm:
1const job = schedule.scheduleJob({hour: 14, minute: 30, dayOfWeek: 0}, function(){
2 console.log('Time for tea!');
3});
It will run after 5 seconds and stop after 10 seconds in this example. The ruledat supports the above.
1const startTime = new Date(Date.now() + 5000);
2const endTime = new Date(startTime.getTime() + 5000);
3const job = schedule.scheduleJob({ start: startTime, end: endTime, rule: '*/1 * * * * *' }, function(){
4 console.log('Time for tea!');
5});
You can shutdown jobs gracefully.
gracefulShutdown()
will cancel all jobs and return Promise.
It will wait until all jobs are terminated.
1schedule.gracefulShutdown();
You can also gracefully shutdown jobs when a system interrupt occurs.
process.on('SIGINT', function () {
schedule.gracefulShutdown()
.then(() => process.exit(0))
}
There are some function to get information for a Job and to handle the Job and Invocations.
You can invalidate any job with the cancel()
method:
1j.cancel();
All planned invocations will be canceled. When you set the parameter reschedule to true then the Job is newly scheduled afterwards.
This method invalidates the next planned invocation or the job. When you set the parameter reschedule to true then the Job is newly scheduled afterwards.
This method cancels all pending invocation and registers the Job completely new again using the given specification. Return true/false on success/failure.
This method returns a Date object for the planned next Invocation for this Job. If no invocation is planned the method returns null.
This module was originally developed by Matt Patenaude who eventually passed over maintainer's mantle over to Tejas Manohar.
Currently it is being maintained by Igor Savin and our amazing community.
We'd love to get your contributions. Individuals making significant and valuable contributions are given commit-access to the project to contribute as they see fit.
Before jumping in, check out our Contributing page guide!
Copyright 2015 Matt Patenaude.
Licensed under the MIT License.
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
Found 5/20 approved changesets -- score normalized to 2
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More