node-cron icon indicating copy to clipboard operation
node-cron copied to clipboard

cron reached maximum iterations.

Open KevinSailly opened this issue 3 years ago • 3 comments

Hi,

Just received an error about cron reached maximum iterations.

Is it possible to bypass iterations limit ?

/myapp/node_modules/cron/lib/cron.js:235
					throw new Error(
					^

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: "" - Cron String: 0,10,20,30,40,50 * * * * * - UTC offset: +02:00 - current Date: Sat May 15 2021 12:31:43 GMT+0200
    at CronTime._getNextDateFrom (/myapp/node_modules/cron/lib/cron.js:235:12)
    at CronTime.sendAt (/myapp/node_modules/cron/lib/cron.js:156:17)
    at CronTime.getTimeout (/myapp/node_modules/cron/lib/cron.js:175:29)
    at CronJob.start (/myapp/node_modules/cron/lib/cron.js:613:31)
    at Timeout.callbackWrapper [as _onTimeout] (/myapp/node_modules/cron/lib/cron.js:665:29)
    at listOnTimeout (internal/timers.js:554:17)
    at processTimers (internal/timers.js:497:7)

Thank you for this module.

Regards, Kévin

KevinSailly avatar May 16 '21 07:05 KevinSailly

PS: using pm2 to start node server, is a potential workaround.

KevinSailly avatar May 16 '21 07:05 KevinSailly

here is the code responsible for this :

                        // 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 timeout = Date.now() + 5000;
                        // determine next date
                        while (true) {
                                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 (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
                                                Time Zone: ${zone || '""'} - Cron String: ${this} - UTC offset: ${date.format(
                                                        'Z'
                                                )} - current Date: ${moment().toString()}`
                                        );
                                }

The error message is wrong, it should be something like this : "unable to calculate next iteration in due time (5s)"

In my case, I think that this error has occur when I was compiling another app and went to an overthrow of CPU/RAM, so no resource were available to calculate next iteration.

Maybe a warn and a wait for a few occurences before throwing an error, or just a warn, could be the solution.

KevinSailly avatar May 16 '21 08:05 KevinSailly

I think it should limit the iteration times, not the duration. For example, count++ in every loop, and limit the count.

tjpgt avatar May 18 '21 04:05 tjpgt