Issue
TL;DR: The current CronTrigger.NextFireTime()
method needs to be fixed.
The current implementation of go-quartz
library shows a subtle error that leads to bad scheduling. I have tested the library using the Cronmaker tool and compared the expected dates with the scheduled dates calculated by go-quartz
for some tests. As a result, I have detected that the test cases are incorrect, and the current implementation skips some dates. This error is subtle, and the rest of the dates are correct. Therefore, the current tests are misleading and should be corrected with some verifiable reference values.
All experiments start from the reference date Sat Apr 22 12:00:00 2023
and calculate the next 50 scheduled dates. These are then contrasted against the output of Cronmaker.
TestCronExpression8
Taken directly from cron_test.go
. The test begins from the initial date of Mon Apr 15 18:00:00 2019
. However, for demonstration purposes I have changed the reference date to the one described above. The cron expression is */3 */51 */12 */2 */4 ? *
. Includes means that only the following are valid values:
Chunk |
Valid values |
Seconds |
0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51, 54, 57 |
Minutes |
0, 51 |
Hours |
0, 12 |
Day of Month |
1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31 |
Month |
Jan, May, Sep |
As we can see below, the current implementation skips from Sat Apr 22 12:00:00 2023
to Tue May 23 12:00:03 2023
. It misses days 1-21
of May. The error is subtle and - beyond the initial miscalculation - the rest are correct. This means that the current tests are misleading and should be corrected with some verifiable reference values. The current ones seem to have been set incorrectly.
Current | Cronmaker |
Tue May 23 12:00:03 2023
Tue May 23 12:00:06 2023
Tue May 23 12:00:09 2023
Tue May 23 12:00:12 2023
Tue May 23 12:00:15 2023
Tue May 23 12:00:18 2023
Tue May 23 12:00:21 2023
Tue May 23 12:00:24 2023
Tue May 23 12:00:27 2023
Tue May 23 12:00:30 2023
Tue May 23 12:00:33 2023
Tue May 23 12:00:36 2023
Tue May 23 12:00:39 2023
Tue May 23 12:00:42 2023
Tue May 23 12:00:45 2023
Tue May 23 12:00:48 2023
Tue May 23 12:00:51 2023
Tue May 23 12:00:54 2023
Tue May 23 12:00:57 2023
Tue May 23 12:51:00 2023
Tue May 23 12:51:03 2023
Tue May 23 12:51:06 2023
Tue May 23 12:51:09 2023
Tue May 23 12:51:12 2023
Tue May 23 12:51:15 2023
Tue May 23 12:51:18 2023
Tue May 23 12:51:21 2023
Tue May 23 12:51:24 2023
Tue May 23 12:51:27 2023
Tue May 23 12:51:30 2023
Tue May 23 12:51:33 2023
Tue May 23 12:51:36 2023
Tue May 23 12:51:39 2023
Tue May 23 12:51:42 2023
Tue May 23 12:51:45 2023
Tue May 23 12:51:48 2023
Tue May 23 12:51:51 2023
Tue May 23 12:51:54 2023
Tue May 23 12:51:57 2023
Thu May 25 00:00:00 2023
Thu May 25 00:00:03 2023
Thu May 25 00:00:06 2023
Thu May 25 00:00:09 2023
Thu May 25 00:00:12 2023
Thu May 25 00:00:15 2023
Thu May 25 00:00:18 2023
Thu May 25 00:00:21 2023
Thu May 25 00:00:24 2023
Thu May 25 00:00:27 2023
Thu May 25 00:00:30 2023
|
Mon May 1 00:00:00 2023
Mon May 1 00:00:03 2023
Mon May 1 00:00:06 2023
Mon May 1 00:00:09 2023
Mon May 1 00:00:12 2023
Mon May 1 00:00:15 2023
Mon May 1 00:00:18 2023
Mon May 1 00:00:21 2023
Mon May 1 00:00:24 2023
Mon May 1 00:00:27 2023
Mon May 1 00:00:30 2023
Mon May 1 00:00:33 2023
Mon May 1 00:00:36 2023
Mon May 1 00:00:39 2023
Mon May 1 00:00:42 2023
Mon May 1 00:00:45 2023
Mon May 1 00:00:48 2023
Mon May 1 00:00:51 2023
Mon May 1 00:00:54 2023
Mon May 1 00:00:57 2023
Mon May 1 00:51:00 2023
Mon May 1 00:51:03 2023
Mon May 1 00:51:06 2023
Mon May 1 00:51:09 2023
Mon May 1 00:51:12 2023
Mon May 1 00:51:15 2023
Mon May 1 00:51:18 2023
Mon May 1 00:51:21 2023
Mon May 1 00:51:24 2023
Mon May 1 00:51:27 2023
Mon May 1 00:51:30 2023
Mon May 1 00:51:33 2023
Mon May 1 00:51:36 2023
Mon May 1 00:51:39 2023
Mon May 1 00:51:42 2023
Mon May 1 00:51:45 2023
Mon May 1 00:51:48 2023
Mon May 1 00:51:51 2023
Mon May 1 00:51:54 2023
Mon May 1 00:51:57 2023
Mon May 1 12:00:00 2023
Mon May 1 12:00:03 2023
Mon May 1 12:00:06 2023
Mon May 1 12:00:09 2023
Mon May 1 12:00:12 2023
Mon May 1 12:00:15 2023
Mon May 1 12:00:18 2023
Mon May 1 12:00:21 2023
Mon May 1 12:00:24 2023
Mon May 1 12:00:27 2023
|
Every 15 seconds on Weekdays
This is the cron expression where I found the issue. I am using go-quartz
to schedule weekly reservations automatically. The demand is quite high so the availability runs out quickly. The app never seems to trigger on time. It has left me more often than not without some of the time slots I selected. The cron expression is the following, */15 * * ? * 1-7
. I slightly modified the cron for this description to make the error more noticeable. In this experiment the error is unmistakable. Instead of every 15 seconds during the day, it skips to the next day every minute.
Current | Cronmaker |
Sat Apr 22 12:00:00 2023
Sat Apr 22 12:00:15 2023
Sat Apr 22 12:00:30 2023
Sat Apr 22 12:00:45 2023
Sun Apr 23 12:00:00 2023
Sun Apr 23 12:00:15 2023
Sun Apr 23 12:00:30 2023
Sun Apr 23 12:00:45 2023
Mon Apr 24 12:00:00 2023
Mon Apr 24 12:00:15 2023
Mon Apr 24 12:00:30 2023
Mon Apr 24 12:00:45 2023
Tue Apr 25 12:00:00 2023
Tue Apr 25 12:00:15 2023
Tue Apr 25 12:00:30 2023
Tue Apr 25 12:00:45 2023
Wed Apr 26 12:00:00 2023
Wed Apr 26 12:00:15 2023
Wed Apr 26 12:00:30 2023
Wed Apr 26 12:00:45 2023
Thu Apr 27 12:00:00 2023
Thu Apr 27 12:00:15 2023
Thu Apr 27 12:00:30 2023
Thu Apr 27 12:00:45 2023
Fri Apr 28 12:00:00 2023
Fri Apr 28 12:00:15 2023
Fri Apr 28 12:00:30 2023
Fri Apr 28 12:00:45 2023
Sat Apr 29 12:00:00 2023
Sat Apr 29 12:00:15 2023
Sat Apr 29 12:00:30 2023
Sat Apr 29 12:00:45 2023
Sun Apr 30 12:00:00 2023
Sun Apr 30 12:00:15 2023
Sun Apr 30 12:00:30 2023
Sun Apr 30 12:00:45 2023
Mon May 1 12:00:00 2023
Mon May 1 12:00:15 2023
Mon May 1 12:00:30 2023
Mon May 1 12:00:45 2023
Tue May 2 12:00:00 2023
Tue May 2 12:00:15 2023
Tue May 2 12:00:30 2023
Tue May 2 12:00:45 2023
Wed May 3 12:00:00 2023
Wed May 3 12:00:15 2023
Wed May 3 12:00:30 2023
Wed May 3 12:00:45 2023
Thu May 4 12:00:00 2023
Thu May 4 12:00:15 2023
Thu May 4 12:00:30 2023
|
Sat Apr 22 12:00:15 2023
Sat Apr 22 12:00:30 2023
Sat Apr 22 12:00:45 2023
Sat Apr 22 12:01:00 2023
Sat Apr 22 12:01:15 2023
Sat Apr 22 12:01:30 2023
Sat Apr 22 12:01:45 2023
Sat Apr 22 12:02:00 2023
Sat Apr 22 12:02:15 2023
Sat Apr 22 12:02:30 2023
Sat Apr 22 12:02:45 2023
Sat Apr 22 12:03:00 2023
Sat Apr 22 12:03:15 2023
Sat Apr 22 12:03:30 2023
Sat Apr 22 12:03:45 2023
Sat Apr 22 12:04:00 2023
Sat Apr 22 12:04:15 2023
Sat Apr 22 12:04:30 2023
Sat Apr 22 12:04:45 2023
Sat Apr 22 12:05:00 2023
Sat Apr 22 12:05:15 2023
Sat Apr 22 12:05:30 2023
Sat Apr 22 12:05:45 2023
Sat Apr 22 12:06:00 2023
Sat Apr 22 12:06:15 2023
Sat Apr 22 12:06:30 2023
Sat Apr 22 12:06:45 2023
Sat Apr 22 12:07:00 2023
Sat Apr 22 12:07:15 2023
Sat Apr 22 12:07:30 2023
Sat Apr 22 12:07:45 2023
Sat Apr 22 12:08:00 2023
Sat Apr 22 12:08:15 2023
Sat Apr 22 12:08:30 2023
Sat Apr 22 12:08:45 2023
Sat Apr 22 12:09:00 2023
Sat Apr 22 12:09:15 2023
Sat Apr 22 12:09:30 2023
Sat Apr 22 12:09:45 2023
Sat Apr 22 12:10:00 2023
Sat Apr 22 12:10:15 2023
Sat Apr 22 12:10:30 2023
Sat Apr 22 12:10:45 2023
Sat Apr 22 12:11:00 2023
Sat Apr 22 12:11:15 2023
Sat Apr 22 12:11:30 2023
Sat Apr 22 12:11:45 2023
Sat Apr 22 12:12:00 2023
Sat Apr 22 12:12:15 2023
Sat Apr 22 12:12:30 2023
|