Module jls.util.Scheduler
Provide a simple scheduler.
Class Schedule
Schedule:new (minute, hour, day, month, weekDay) | Creates a new Schedule. |
schedule:format () | Returns a string representation of this schedule. |
Schedule.parse (value) | Returns a new schedule from the specified string. |
Class Scheduler
Scheduler:new () | Creates a new Scheduler. |
scheduler:schedule (schedule, fn) | Schedules the specified function using the specified schedule. |
scheduler:stop () | Stops this scheduler from running. |
scheduler:run () | Runs this scheduler. |
Class Schedule
The Schedule class.
- Schedule:new (minute, hour, day, month, weekDay)
-
Creates a new Schedule.
Parameters:
- minute table a list of range for the minutes, 0-59
- hour table a list of range for the hours, 0-23
- day table a list of range for the days, 1-31
- month table a list of range for the months, 1-12
- weekDay table a list of range for the weekDays, 0-6 Sunday to Saturday
Returns:
-
a new Schedule
- schedule:format ()
-
Returns a string representation of this schedule.
Returns:
-
string
a representation of this schedule.
- Schedule.parse (value)
-
Returns a new schedule from the specified string.
The string representation is similar of the cron syntax, see https://en.wikipedia.org/wiki/Cron
The value is a space separated list of definition for the minute, hour, day, month, weekDay fields.
Each field could be a comma separated list of numerical value, numerical range, the star symbol.
A range can be followed by a slash and a numerical value indicating the step to use in the range.
Parameters:
- value string the string representation of the schedule
Returns:
-
Schedule
the new schedule
Class Scheduler
A Scheduler class.
- Scheduler:new ()
-
Creates a new Scheduler.
Returns:
-
a new Scheduler
- scheduler:schedule (schedule, fn)
-
Schedules the specified function using the specified schedule.
Parameters:
- schedule the schedule as a string or a Schedule
- fn function the function to call depending on the schedule
Returns:
-
an opaque schedule id that can be used to remove the schedule from this scheduler
Usage:
local scheduler = Scheduler:new() local fn = function() print(os.date()) end scheduler:schedule('0 * * * *', fn) -- every hour scheduler:schedule('*/5 * * * *', fn) -- every five minutes scheduler:schedule('0 0 * * *', fn) -- every day at midnight scheduler:schedule('0 0 * * 1-5', fn) -- every weekday at midnight scheduler:schedule('0 0 1 * *', fn) -- every first day of the month at midnight scheduler:schedule('0 0 1 1 *', fn) -- every year the 1st January at midnight
- scheduler:stop ()
- Stops this scheduler from running.
- scheduler:run ()
- Runs this scheduler. If there are no schedule for some time then this scheduler will sleep.