Comparing version 1.4.1 to 1.5.0
const CronJob = require('../lib/cron.js').CronJob; | ||
console.log('Before job instantiation'); | ||
const job = new CronJob('* */10 * * * *', function() { | ||
const job = new CronJob('0 */10 * * * *', function() { | ||
const d = new Date(); | ||
@@ -6,0 +6,0 @@ console.log('Every Tenth Minute:', d); |
const CronJob = require('../lib/cron.js').CronJob; | ||
console.log('Before job instantiation'); | ||
const job = new CronJob('* */30 9-17 * * *', function() { | ||
const job = new CronJob('0 */30 9-17 * * *', function() { | ||
const d = new Date(); | ||
@@ -6,0 +6,0 @@ console.log('Every 30 minutes between 9-17:', d); |
@@ -93,5 +93,6 @@ (function(root, factory) { | ||
var ok = false; | ||
/* if a dayOfMonth is not found in all months, we only need to fix the last | ||
wrong month to prevent infinite loop */ | ||
var lastWrongMonth={}; | ||
var lastWrongMonth = {}; | ||
for (var i = 0; i < months.length; i++) { | ||
@@ -104,25 +105,27 @@ var m = months[i]; | ||
var dom = dsom[j]; | ||
if (dom <= con) | ||
if (dom <= con) { | ||
ok = true; | ||
} | ||
} | ||
if (!ok) { | ||
// save the month inorder to be fixed if all months fails (infinite loop) | ||
lastWrongMonth=m; | ||
// save the month in order to be fixed if all months fails (infinite loop) | ||
lastWrongMonth = m; | ||
console.warn("Month '" + m + "' is limited to '" + con + "' days."); | ||
} | ||
} | ||
// infinit loop detected (dayOfMonth is not found in all months) | ||
if(!ok){ | ||
// infinite loop detected (dayOfMonth is not found in all months) | ||
if (!ok) { | ||
var con = CronTime.monthConstraints[parseInt(lastWrongMonth, 10)]; | ||
var dsom = Object.keys(this.dayOfMonth); | ||
for (var j = 0; j < dsom.length; j++) { | ||
var dom = dsom[j]; | ||
if(dom > con){ | ||
delete this.dayOfMonth[dom]; | ||
var fixedDay = Number(dom)% con; | ||
this.dayOfMonth[fixedDay] = true; | ||
} | ||
for (var k = 0; k < dsom.length; k++) { | ||
var dom = dsom[k]; | ||
if (dom > con) { | ||
delete this.dayOfMonth[dom]; | ||
var fixedDay = Number(dom) % con; | ||
this.dayOfMonth[fixedDay] = true; | ||
} | ||
} | ||
} | ||
}, | ||
@@ -192,3 +195,2 @@ | ||
*/ | ||
_getNextDateFrom: function(start, zone) { | ||
@@ -225,7 +227,27 @@ var date; | ||
// sanity check | ||
/* | ||
max itterations shouldn't be more than | ||
11 months + 30 days + 23 hours + 59 minutes + 59 seconds | ||
anything more than that value should be considered a bug. | ||
note: *2 is added incase we missed something. | ||
*/ | ||
var MAX_ITERATIONS = (11 + 30 + 23 + 59 + 59) * 2; | ||
var i = 0; | ||
// determine next date | ||
while (true) { | ||
i++; | ||
var diff = date - start; | ||
var prevMonth = date.month(); | ||
var prevDay = date.days(); | ||
var prevMinute = date.minutes(); | ||
var prevSeconds = date.seconds(); | ||
var origDate = new Date(date); | ||
if (i > MAX_ITERATIONS) { | ||
throw new Error( | ||
`Something went wrong. cron reached maximum iterations. | ||
Please open an issue (https://github.com/kelektiv/node-cron/issues/new) and provide the following string | ||
Time Zone: ${zone || '""'} - Cron String: ${this} - UTC offset: ${date.format('Z')} - current Date: ${moment().toString()}` | ||
); | ||
} | ||
if ( | ||
@@ -236,2 +258,5 @@ !(date.month() in this.month) && | ||
date.add(1, 'M'); | ||
if (date.month() === prevMonth) { | ||
date.add(1, 'M'); | ||
} | ||
date.date(1); | ||
@@ -243,2 +268,3 @@ date.hours(0); | ||
} | ||
if ( | ||
@@ -249,2 +275,5 @@ !(date.date() in this.dayOfMonth) && | ||
date.add(1, 'd'); | ||
if (date.days() === prevDay) { | ||
date.add(1, 'd'); | ||
} | ||
date.hours(0); | ||
@@ -261,2 +290,5 @@ date.minutes(0); | ||
date.add(1, 'd'); | ||
if (date.days() === prevDay) { | ||
date.add(1, 'd'); | ||
} | ||
date.hours(0); | ||
@@ -270,2 +302,3 @@ date.minutes(0); | ||
} | ||
if ( | ||
@@ -277,3 +310,3 @@ !(date.hours() in this.hour) && | ||
date.hours( | ||
date.hours() == 23 && diff > 86400000 ? 0 : date.hours() + 1 | ||
date.hours() === 23 && diff > 86400000 ? 0 : date.hours() + 1 | ||
); | ||
@@ -287,2 +320,3 @@ date.minutes(0); | ||
} | ||
if ( | ||
@@ -304,2 +338,3 @@ !(date.minutes() in this.minute) && | ||
} | ||
if ( | ||
@@ -335,3 +370,4 @@ !(date.seconds() in this.second) && | ||
var newDate = moment(date); | ||
while (newDate <= date) { // eslint seems to trigger here, it is wrong | ||
while (newDate <= date) { | ||
// eslint seems to trigger here, it is wrong | ||
newDate.add(1, 's'); | ||
@@ -338,0 +374,0 @@ } |
{ | ||
"name": "cron", | ||
"description": "Cron jobs for your node", | ||
"version": "1.4.1", | ||
"version": "1.5.0", | ||
"author": "Nick Campbell <nicholas.j.campbell@gmail.com> (http://github.com/ncb000gt)", | ||
@@ -6,0 +6,0 @@ "bugs": { |
@@ -53,3 +53,3 @@ node-cron | ||
Note - You need to explictly start a job in order to make it run. This gives a | ||
Note - You need to explicitly start a job in order to make it run. This gives a | ||
little more control over running your jobs. | ||
@@ -73,5 +73,6 @@ | ||
There are tools that help when constructing your cronjobs. You might find | ||
something like https://cronjob.xyz/ helpful. But, note that it doesn't accept | ||
the exact same syntax as this library, for instance, it doesn't accept the | ||
`seconds` field, so keep that in mind. | ||
something like https://crontab.guru/ or https://cronjob.xyz/ helpful. But, | ||
note that these don't necessarily accept the exact same syntax as this | ||
library, for instance, it doesn't accept the `seconds` field, so keep that in | ||
mind. | ||
@@ -78,0 +79,0 @@ |
var chai = require('chai'); | ||
var expect = chai.expect; | ||
var cron = require('../lib/cron'); | ||
var moment = require('moment-timezone'); | ||
@@ -229,3 +230,64 @@ // most eslint errors here are due to side effects. i don't care much about them right now | ||
}); | ||
it('should work around time zone changes that shifts time back (1)',function(){ | ||
var d = new Date("10-7-2018") | ||
// America/Sao_Paulo has a time zone change around NOV 3 2018. | ||
var cronTime = new cron.CronTime('0 0 9 4 * *'); | ||
var nextDate = cronTime._getNextDateFrom(d, 'America/Sao_Paulo'); | ||
expect(nextDate.valueOf()).to.equal(moment('2018-11-04T09:00:00.000-02:00').valueOf()) | ||
}); | ||
it('should work around time zone changes that shifts time back (2)',function(){ | ||
// Asia/Amman DST ends in 26 - OCT-2018 (-1 to hours) | ||
var d = moment.tz('2018-10-25T23:00','Asia/Amman'); | ||
var cronTime = new cron.CronTime('0 0 * * *'); | ||
var nextDate = cronTime._getNextDateFrom(d, 'Asia/Amman'); | ||
expect(nextDate-moment.tz('2018-10-26T00:00','Asia/Amman')).to.equal(0); | ||
}); | ||
it('should work around time zone changes that shifts time forward',function(){ | ||
// Asia/Amman DST starts in 30-March-2018 (+1 to hours) | ||
var currentDate = moment.tz('2018-03-29T23:00','Asia/Amman'); | ||
var cronTime = new cron.CronTime('* * * * *'); | ||
for(var i=0;i<100;i++){ | ||
var nextDate = cronTime._getNextDateFrom(currentDate,'Asia/Amman'); | ||
expect(nextDate-currentDate).to.equal(1000*60); | ||
currentDate=nextDate; | ||
} | ||
}); | ||
it('should generate the right N next days for * * * * *',function(){ | ||
var cronTime = new cron.CronTime('* * * * *'); | ||
var currentDate=moment().seconds(0).milliseconds(0); | ||
for(var i=0;i<100;i++){ | ||
var nextDate = cronTime._getNextDateFrom(currentDate); | ||
expect(nextDate-currentDate).to.equal(1000*60); | ||
currentDate=nextDate; | ||
} | ||
}); | ||
it('should generate the right N next days for 0 0 9 * * *',function(){ | ||
var cronTime = new cron.CronTime('0 0 9 * * *'); | ||
var currentDate=moment().utc().seconds(0).milliseconds(0).hours(9).minutes(0); | ||
for(var i=0;i<100;i++){ | ||
var nextDate = cronTime._getNextDateFrom(currentDate); | ||
expect(nextDate-currentDate).to.equal(1000*60*60*24); | ||
currentDate=nextDate; | ||
} | ||
}); | ||
it('should generate the right N next days for 0 0 * * * with a time zone',function(){ | ||
var cronTime = new cron.CronTime('0 * * * *'); | ||
var currentDate=moment.tz('2018-11-02T23:00','America/Sao_Paulo').seconds(0).milliseconds(0); | ||
for(var i=0;i<25;i++){ | ||
var nextDate = cronTime._getNextDateFrom(currentDate,'America/Sao_Paulo'); | ||
expect(nextDate-currentDate).to.equal(1000*60*60); | ||
currentDate=nextDate; | ||
} | ||
}); | ||
it('should generate the right N next days for */3 * * * * with a time zone',function(){ | ||
var cronTime = new cron.CronTime('*/3 * * * *'); | ||
var currentDate=moment.tz('2018-11-02T23:00','America/Sao_Paulo').seconds(0).milliseconds(0); | ||
for(var i=0;i<25;i++){ | ||
var nextDate = cronTime._getNextDateFrom(currentDate,'America/Sao_Paulo'); | ||
expect(nextDate-currentDate).to.equal(1000*60*3); | ||
currentDate=nextDate; | ||
} | ||
}); | ||
it('should generete the right next day when cron is set to every 15 min in Feb', function() { | ||
@@ -232,0 +294,0 @@ var cronTime = new cron.CronTime('*/15 * * FEB *'); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
60555
24
1859
173
1