Skip to content

Instantly share code, notes, and snippets.

@mStirner
Forked from shimondoodkin/cron.js
Created May 5, 2023 15:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mStirner/6b165a8099619d9ad2d37615ef57f3b9 to your computer and use it in GitHub Desktop.
Save mStirner/6b165a8099619d9ad2d37615ef57f3b9 to your computer and use it in GitHub Desktop.
javascript cron can be used in node.js
/*
Added complex matches by Shimon Doodkin 2012
Developed by Elijah Rutschman 2009 - http://elijahr.blogspot.com/2009/03/javascript-cron.html
*/
/*
a typical cron entry has either wildcards (*) or an integer:
.---------------- minute (0 - 59)
| .-------------- hour (0 - 23)
| | .------------ day of month (1 - 31)
| | | .---------- month (1 - 12)
| | | | .-------- day of week (0 - 6) (Sunday=0)
| | | | |
* * * * *
*/
// m h d M w
//cron.add("* * * * *", function(){alert('cron job that runs every minute')})
var cron = {
jobs : [],
parse: function(a)
{
var active_at=a.split(',').map(function(i)
{
var z=i.split('/');
var x=z[0].split('-');
if(x[0] == "*") x[0]=-1;
if(x.length==1)
x.push(x[0]);
x[2]=z.length===1?-1:z[1];
// parsed array structure:
x[0]=parseInt(x[0]);// 0 - from
x[1]=parseInt(x[1]);// 1 - to
x[2]=parseInt(x[2]);// 2 modulus
return x;
});
var modulo=a.length>1?a[1]:-1;
return active_at;
},
match: function(a,b)
{
for(var c,b0,i=0;i<a.length;i++)
{
c=a[i];
if( c[0]===-1 || ( b>=c[0] && b<=c[1] ) )
{
b0=b-c[0]; // make the modulo start from 1st miniture of from matched time, not first minute of from 0 minutes
if( c[2]===-1 || b0===0 || b0%c[2]===0 )
return true;
}
}
return false;
},
process : function() {
var now = new Date();
for (var i=0; i<cron.jobs.length; i++) {
if ( cron.match(cron.jobs[i].minute, now.getMinutes()) )
if ( cron.match(cron.jobs[i].hour, now.getHours()) )
if ( cron.match(cron.jobs[i].date, now.getDate()) )
if ( cron.match(cron.jobs[i].month, now.getMonth()) )
if ( cron.match(cron.jobs[i].day, now.getDay()) )
cron.jobs[i].run();
}
now = null;
},
"id" : 0,
"start" : function() {
cron.stop();
cron.id = setInterval(function(){cron.process()},60000);
},
"stop" : function() {
clearInterval(cron.id);
},
"add" : function(cronstring, fun) {
var _Job = {};
var items = cronstring.match(/^([0-9,\-\/]+|\*{1}|\*{1}\/[0-9]+)\s+([0-9,\-\/]+|\*{1}|\*{1}\/[0-9]+)\s+([0-9,\-\/]+|\*{1}|\*{1}\/[0-9]+)\s+([0-9,\-\/]+|\*{1}|\*{1}\/[0-9]+)\s+([0-9,\-\/]+|\*{1}|\*{1}\/[0-9]+)\s*$/);
_Job.minute = cron.parse(items[1]);
_Job.hour = cron.parse(items[2]);
_Job.date = cron.parse(items[3]);
_Job.month = cron.parse(items[4]);
_Job.day = cron.parse(items[5]);
_Job.run = fun;
cron.jobs.push(_Job);
_Job = null;
items = null;
}
}
cron.start();
//if(module&&exports)module.exports=cron; // i use it in node.js
// or
//exports.cron=cron; // i use it in node.js
/*
// when cron is not running:
// queue up some jobs to run
cron.add("* * * * *", function(){alert('cron job 1 just ran')})
cron.add("5 * * * *", function(){alert('cron job 2 just ran')})
cron.add("15 * * * *", function(){alert('cron job 3 just ran')})
cron.add("30 * * * *", function(){alert('cron job 4 just ran')})
cron.start();
// Cron already running, but we can add more jobs, no problem
cron.add("0 * * * *", function(){alert('cron job 5 just ran')})
cron.add("7 9 * * 1,2,3,4,5", function(){alert('at 9:07 of every morning from sunday to thursday')})
*/
/*
testing the rulls matching:
var x;
console.log( x=cron.parse("0,1,2,3,4") ) // ruls can be 1-2/3,3-4/5 many match options are seperated by comma. but no spacing in between
console.log( cron.match(x,3) );
*/
/// each place can be : [number or number-number or *]/[modulus],[number or number-number or *]/[modulus],...
// * any number
// 5 number is 5
// 2-10 number from 5 to 10
// */3 every 3rd will run, - optionaly after the / comes modulus, modulus calulates reminder, if reminder is zero means it devides by that number exactly
// 2-10/3 between 5 to 10 every 3rd will run
//
// 2-10,12-16 many matchs but no spacing in between
// 2-10/5,12-16/2,*/10 alot matchs example
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment