sched
The sched module exposes a simple interface to schedule tasks, powered by chrono (opens in a new tab).
once := sched.once("1s", func(){
print("once")
})
cron := sched.cron("*/1 * * * * *", func(){
print("hola")
})
print(cron.is_running())
every := sched.every("1m", func() {
print("every 1 minute")
})
for {
time.sleep(1)
}Functions
Functions are non-blocking and return a task object.
cron
cron(cronline string, fn func)Creates a new cron job.
The cronline string is a space-separated list of 6 fields, representing the time to run the task.
SECOND MINUTE HOUR DAY MONTH DAYOFWEEK
* * * * * *Some examples:
* * * * * *every second*/5 * * * * *every 5 seconds0 * * * * *every minute0 0 * * * *every hour20 45 18 5-20/3 * *every day at 18:45:20, from the 5th to the 20th of the month, every 3 days0 0 0 1 SEP *every year on September 1st at midnight0 0 0 1 5 SUNevery year on the first Sunday of May at midnight
// Run every second
task := sched.cron("*/1 * * * * *", func() {
print("hello world!")
})Functions run in a separate goroutine, and errors are ignored, so the main program can continue to run. Make sure to handle errors in your function.
every
every(duration string, fn func)Creates a new task that runs regularly, at the specified duration.
The string format is documented in the standard Go library (opens in a new tab).
// Run every minute
task := sched.every("1m"", func() {
print("hello world!")
})once
once(duration string, fn func)Creates a new task that runs once, after the specified duration.
// Run a task in one 1h, once
task := sched.once("1h", func() {
print("hello world!")
})cron can be used to run a task once at a specific time, if more control is needed.
Types
task
A Task object returned by cron, every and once functions.
Attributes
| Name | Type | Description |
|---|---|---|
| cancel | func() | Cancels the task |
| is_running | func() | True if the task is running |