This is the 7th day of my participation in Gwen Challenge
Write at the front: work needs to do a scheduled task function. After discussion, it was decided to use Quartz to implement the task scheduling, the back end receives and returns a CRon expression, and the front end needs a friendly interface to assist the user to enter normal CRon and verify the validity.
That’s the reason for writing this article.
Learn about CRon expressions
A CRON expression is a computer-readable string used for scheduled task scheduling. Different programs support different cron expression rules and implement different cron expression parsers.
Take the cron expression in Quartz, which has seven fields:
The field name | Allowed values | Special characters allowed |
---|---|---|
seconds | 0-59 | , – * / |
points | 0-59 | , – * / |
hours | 0-23 | , – * / |
day | 1-31 | , – *? / L W C |
month | 1-12 or JAN-DEC | , – * / |
Several weeks | 1-7 or SUN-SAT | , – *? / L C # |
Year (optional field) | empty, 1970-2099 | , – * / |
There’s something special about the “day of the week” field, whether it’s 0 or 7, it’s Sunday.
In addition to these fixed characters, each position supports some auxiliary characters. Details are as follows:
/ : indicates the time from which the command is executed every fixed time. Such as:
- The value under the minutes field is
0/25
Is executed every 25 minutes starting from minute 0 - The value under the minutes field is
Three quarters of
Is executed every four minutes starting from the third minute
One point to note here: the CRon expression does not record the last time it was executed, so 0/25 above is executed at 0:0, 0:25, 0:50, 1:0, 1:25, 1:50, rather than executing backwards from the current time by 25 minutes. This may not be the execution time you remember.
“?” : indicates a day of a month or a week. Represents an indeterminate value
“L” : the last day of the month, or the last week of the month. For example, “6L” means “the last Friday of the month”.
W: indicates the latest working day (Monday to Friday, no overseas holidays). For example, if 15W is set in the day-of-month field, it indicates the latest working day to the 15th of this month.
“#” : is used to specify the week X of the month. Such as:
- The value under the weekly field is
6 # 3
, the third Friday of every month
“, “character: Specifies several values, separated by commas.
– character: Specifies a range of values, for example:
- Under the minutes
0 to 5
, from minute 0 to minute 5.
The execution time is 0, 1, 2, 3, 4, 5
L: indicates the last day of a month. X indicates the last week of a month
These are some of the rules for cron expressions. For brevity, some examples are not fully shown
The CRon expression extrapolates the time after which the task will execute
Therefore, according to the rules mentioned above, you can implement an iterator that generates the time for subsequent tasks to be executed. The specific ways to reference www.npmjs.com/package/cro… .
The code analysis will be updated here, so stay tuned
Cron expression inverse parsing and generation
There are front-end plug-ins that do this. For example github.com/jingjingke/… . The author’s code comments and documentation are clear, and it’s worth looking at, even if it doesn’t seem like anyone is using it.
The code analysis will be updated here, so stay tuned