Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better detection an infinite loop #442

Merged
merged 1 commit into from
Oct 4, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 9 additions & 13 deletions lib/cron.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@

/* 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 = NaN;
for (var i = 0; i < months.length; i++) {
var m = months[i];
var con = CronTime.monthConstraints[parseInt(m, 10)];
Expand Down Expand Up @@ -221,10 +221,7 @@
// it shouldn't take more than 5 seconds to find the next execution time
// being very generous with this. Throw error if it takes too long to find the next time to protect from
// infinite loop.
var finding = true;
var findingTimeout = setTimeout(function() {
finding = false;
}, 5000);
var timeout = Date.now() + 5000;
// determine next date
while (true) {
var diff = date - start;
Expand All @@ -234,7 +231,7 @@
var prevSeconds = date.seconds();
var origDate = new Date(date);

if (!finding) {
if (Date.now() > timeout) {
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
Expand Down Expand Up @@ -304,14 +301,14 @@
origDate = moment(date);
var curHour = date.hours();
date.hours(
date.hours() === 23 && diff > 86400000 ? 0 : date.hours() + 1
date.hours() === 23 && diff > 86400000 ? 0 : date.hours() + 1
);
/*
* Moment Date will not allow you to set the time to 2 AM if there is no 2 AM (on the day we change the clock)
* We will therefore jump to 3AM if time stayed at 1AM
*/
if(curHour === date.hours()){
date.hours(date.hours() + 2);
* Moment Date will not allow you to set the time to 2 AM if there is no 2 AM (on the day we change the clock)
* We will therefore jump to 3AM if time stayed at 1AM
*/
if (curHour === date.hours()) {
date.hours(date.hours() + 2);
}
date.minutes(0);
date.seconds(0);
Expand Down Expand Up @@ -360,7 +357,6 @@
break;
}

clearTimeout(findingTimeout);
return date;
},

Expand Down