node-cron / node-cron

A simple cron-like job scheduler for Node.js

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Offset syntax works incorrectly

satoshiman opened this issue · comments

*/10 * * * * should be run at min 0th, 10th, 20th, 30th...
2-59/10 * * * * should be run at 2nd, 12nd, 22nd, 32nd...

Currently, the 2 syntaxes above return the same result.

Should I need to put in any option to run correctly?

Note that you have not defined the particular hour you want your task to execute, so it takes the current hour. This results in unexpected occurrences because the calculation is already in progress. For example, You start your server at 1 : 09, if you run the first syntax it would take just one minute to reach the first 10th minute of the current hour, if you start the server at 2 : 25, it would take 5 minutes to reach the 3rd 10th minute.
Same thing is applicable to the second syntax as it is already calculating from your current time. To get your schedule to run smoothly, you can add a specified hour or watch from the second execution, it will automatically align with the 10mins interval. I hope this helps.

Thanks for your feedback.
When I use first syntax */10 * * * * I expect it will run at 00:00, 00:10, 00:20, ... (hh:mm = xx:x0)
When I use second syntax 2-59/10 * * * * I expect it will run at 00:02, 00:12, 00:22, ... (hh:mm = xx:x2)
I suppose that the cronjob is running based on system time, but not depending on the time I start my computer.

I refer to the expected results those syntaxes on this website https://cronjob.xyz/

The first and second expressions do not behave like that. The first runs every 10mins, but begins counting from 0, and the second expression runs every 10mins but begins counting from the 2nd minute. If wanted something that ran on the 0min then increments by 10 for the first it would be something like this (0, 10, 20, 30, 40, 50 * * * *).
The second would be (2, 12, 22, 32, 42, 52 * * * *) .

@mooosh-milllie That's exactly what the issue is about:

2-59/10 * * * * should be equivalent to 2,12,22,32,42,52 * * * * but with node-cron it's (wrongly) expanded to 10,20,30,40,50 * * * *

Because the 10 in 2-59/10 is not a divisor but a stepsize. Ie, the semantics of this expression is: start at minute 2 and progress in steps of 10 whereas node-cron implements the (wrong) logic, take all minutes in the range 2-59 which are divisible by 10 without remainder

@wiggisser the syntax 2-59/10 * * * * from the documentation means that the cron job will run every 10 minutes, the counting begins at the second minute of every hour, so the first execution is at the 12th minute. How do you expect it run from the 2nd minute when */10 * * * * does not run at the first minute? The point is following what the documentation explains.

@mooosh-milllie You don't get the point. This issue has nothing to do with when you start you task. The problem is, node-cron expands a pattern like 2-59/10 * * * * to 10,20,30,40,50 * * * * instead of 12,22,32,42,52 * * * * So the task will be run at the 10th, 20th, 30th 40th, 50th minute of every hour instead of the 12th, 22nd, 32nd, 42nd, 52nd minute of every hour ...

Actually, I found this issue because I need to run a task at even minutes */2 * * * *, and odd minutes 1-59/2 * * * *. But both syntaxes return the same result.
Syntax 1,3,5,7,9,11,13,...,55,57,59 * * * * (super long syntax) is really bad idea for odd minutes.

Actually, this is also wrong for days. A pattern like 0 0 */2 * * will run on the 2nd, 4th, 6th ... day of every month instead of 1st, 3rd, 5th ... because for the date the * expands to 1-31, thus */2 should start at 1 and progress in steps of 2

But actually, this wrong behavious seems to be intentional (or just easier to implement). At least it's documented this way ...

e.g: 1-10/2 that is the same as 2,4,6,8,10

Which is a pity, because it means that

  1. It differs from the expected behaviour of standard unix cronjobs
  2. It maybe won't be fixed any time soon, because it would mean a breaking change

@wiggisser I don't have this issue you are complaining about, I have used node-cron for project and I have written a book about cron jobs in Node JS using node-cron, I took enough time to test these out, and it runs as it says in the documentation. 2-59/10 * * * * runs from the 12th 22nd etc. 1-10/2 * * * * isn't the same as 2,4,6,8,10.

@mooosh-milllie I don't know which version of node-cron you are using or where you got it from. But the one in this repository explicitely has written in the docs that it treats 1-10/2 * * * * as 2,4,6,8,10... (which differs from the expected cron behaviour on unix, where it would schedule the task on every odd minute, instead of every even minute) See the following screenshot (highlighting by me)

image

And this simple test

var cron = require("node-cron");
var task = cron.schedule("4-59/10 * * * *", () => { console.log(new Date().toISOString())})
console.log(task);
setTimeout(() => {}, 10000000);

with node-cron@3.0.2 ( freshly installed via npm i node-cron and started at 17:13 local time reveals the following behaviour

  • in the console output of the created task you can see that the pattern is expanded to '0 10,20,30,40,50 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31 1,2,3,4,5,6,7,8,9,10,11,12 0,1,2,3,4,5,6' Ie the minutes in the pattern are set to 10,20,30,40,50 which is not hte expected value of 4,14,24,34,44,54
  • no output at 17:14 although there should be one
  • an output at 17:20 although there should be none

And looking at the respective sourcecode, this is to be expected. Because it works as follows

  • expand a pattern like 2-59 to 2,3,4,5,6....58,59
  • if there is a step, take only those values where v % step === 0 so with a stepsize of 10, it is impossible to schedule a task at the 12th minute of an hour ...

I decided to run this with the latest version and I was able to recreate the issue. Everything that has to do with this "4-59/10 * * * *" syntax, now defaults to "*/min * * * *". I ran a test using "4-59/2 * * * *" and "5-59/2 * * * *" and they both defaulted to "*/2 * * * *". The first to log was "4-59/2 * * * *" and it logged at the fourth minute and "5-59/2 * * * *" logged did not log at the fifth minute to show consistent behavior but instead logged at the sixth minute, together with this "4-59/2 * * * *" task.
So this is really an issue and I'm sorry 🙏 for not trying to recreate the issue before now.