Skip to content

Commit

Permalink
GH-467 GH-478 GH-469 GH-470 GH-497 GH-498 - Allow soft warning when a…
Browse files Browse the repository at this point in the history
… hard error isn't required.

Signed-off-by: Nick Campbell <nicholas.j.campbell@gmail.com>
  • Loading branch information
ncb000gt committed May 28, 2020
1 parent 9a51726 commit b79391b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,11 @@ Parameter Based
loop. For more information take a look at
[timers#timers_timeout_unref](https://nodejs.org/api/timers.html#timers_timeout_unref)
from the NodeJS docs.
- `softTimeout` - [OPTIONAL] - This module needs to determine the time at which to run.
However, there have been cases when an infinite loop can occur in this determination.
When `true`, the module will display a warning if the time has slipped by more than 5
seconds trying to determine the next time to trigger the onTick. When `false`, this
will throw an exception stopping the execution.
- `start` - Runs your job.
- `stop` - Stops your job.
- `setTime` - Stops and changes the time for the `CronJob`. Param must be a `CronTime`.
Expand Down
9 changes: 6 additions & 3 deletions lib/job.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@ function CronJob(CronTime, spawn) {
context,
runOnInit,
utcOffset,
unrefTimeout
unrefTimeout,
softTimeout
) {
var _cronTime = cronTime;
var argCount = 0;
for (var i = 0; i < arguments.length; i++) {
for (var i = 0; i < arguments.length; i++) {: w

This comment has been minimized.

Copy link
@DaniilVysotskiy

DaniilVysotskiy May 28, 2020

Here is a typo: {: w ?
Looks like unsuccessful attempt to quit from vim %)


if (arguments[i] !== undefined) {
argCount++;
}
Expand All @@ -54,12 +56,13 @@ function CronJob(CronTime, spawn) {
_cronTime = cronTime.cronTime;
utcOffset = cronTime.utcOffset;
unrefTimeout = cronTime.unrefTimeout;
softTimeout = cronTime.softTimeout || true; // default to "opt in" soft timeout warning rather than throwing an exception if dwthe detecting a new time slips slightly. this can mean that an "infinite loop" is possible when trying to determine a new date
}

this.context = context || this;
this._callbacks = [];
this.onComplete = fnWrap(onComplete);
this.cronTime = new CronTime(_cronTime, timeZone, utcOffset);
this.cronTime = new CronTime(_cronTime, timeZone, utcOffset, softTimeout);
this.unrefTimeout = unrefTimeout;

addCallback.call(this, fnWrap(onTick));
Expand Down
18 changes: 12 additions & 6 deletions lib/time.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,9 @@ const RE_WILDCARDS = /\*/g;
const RE_RANGE = /^(\d+)(?:-(\d+))?(?:\/(\d+))?$/g;

function CronTime(luxon) {
function CT(source, zone, utcOffset) {
function CT(source, zone, utcOffset, softTimeout) {
this.source = source;
this.softTimeout = softTimeout;

if (zone) {
const dt = luxon.DateTime.fromObject({ zone: zone });
Expand Down Expand Up @@ -265,13 +266,18 @@ function CronTime(luxon) {

// hard stop if the current date is after the expected execution
if (Date.now() > timeout) {
throw new Error(
`Something went wrong. cron reached maximum iterations.
var msg = `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: ${luxon.DateTime.local().toString()}`
);
'Z'
)} - current Date: ${luxon.DateTime.local().toString()}`;

if (this.softTimeout) {
// a soft timeout here could allow for an infinite loop. to prevent this, set the soft timeout to true
console.warn(msg);
} else {
throw new Error(msg);
}
}

if (
Expand Down

0 comments on commit b79391b

Please sign in to comment.