1. Introduction
The Golang cron library is a powerful and easy-to-use tool that allows for scheduling jobs to run at specific times or intervals. It is modeled after the UNIX cron service but is implemented as a standalone library that can be integrated into Go applications. The cron library takes care of the time-based scheduling, leaving you to focus on the job that needs to be executed.
2. Example
Installing the cron library
To begin using the cron library in your project, you need to first install it using the go get
command:
go get github.com/robfig/cron/[email protected]
Import cron package
After installing, you can import it into your Go program as follows:
import "github.com/robfig/cron/v3"
Here's a simple example of how to set up a Cron job that prints a message every minute:
package main
import (
"fmt"
"github.com/robfig/cron/v3"
)
func main() {
c := cron.New()
c.AddFunc("0 30 * * * *", func() { fmt.Println("Every hour on the half hour") })
c.AddFunc("@hourly", func() { fmt.Println("Every hour") })
c.AddFunc("@every 1h30m", func() { fmt.Println("Every hour thirty") })
c.Start()
..
// Funcs are invoked in their own goroutine, asynchronously.
...
// Funcs may also be added to a running Cron
c.AddFunc("@daily", func() { fmt.Println("Every day") })
..
..
c.Stop() // Stop the scheduler (does not stop any jobs already running).
}
3. CRON Expression Format
A cron expression represents a set of times, using 6 space-separated fields.
Field name | Mandatory? | Allowed values | Allowed special characters |
---|---|---|---|
Seconds | Yes | 0-59 | * / , - |
Minutes | Yes | 0-59 | * / , - |
Hours | Yes | 0-23 | * / , - |
Day of month | Yes | 1-31 | * / , - ? |
Month | Yes | 1-12 or JAN-DEC | * / , - |
Day of week | Yes | 0-6 or SUN-SAT | * / , - ? |
4. Special Characters in CRON Expressions
- Asterisk ( * ) The asterisk represents all possible values for the given field. For example, an asterisk in the minute field signifies every minute.
- Slash ( / ) The slash character is used to specify increments. For example, "*/15" in the seconds field means every 15 seconds starting from zero.
- Comma ( , ) The comma character is used for listing multiple discrete values. For instance, "MON,WED,FRI" in the day-of-week field means the job will run on Monday, Wednesday, and Friday.
- Hyphen ( - ) The hyphen denotes a range of values. "9-17" in the hours field indicates every hour from 9 AM to 5 PM.
- Question mark ( ? ) The question mark can be used in the day-of-month and day-of-week fields to signify 'no specific value', which is useful when you need to specify a value for one and not the other.
5. Predefined Schedules
Detailed explanation of pre-defined schedules available, displayed in a table format:
Entry | Description | Equivalent To |
---|---|---|
@yearly (or @annually) | Run once a year, midnight, Jan. 1st | 0 0 0 1 1 * |
@monthly | Run once a month, midnight, first of month | 0 0 0 1 * * |
@weekly | Run once a week, midnight between Sat/Sun | 0 0 0 * * 0 |
@daily (or @midnight) | Run once a day, midnight | 0 0 0 * * * |
@hourly | Run once an hour, beginning of hour | 0 0 * * * * |
6. Intervals and Fixed Intervals Scheduling
Scheduling jobs to execute at fixed intervals is another powerful feature of the cron library. For example, to schedule a job that runs every 2 hours:
c.AddFunc("@every 2h", func() { fmt.Println("Job runs every two hours") })
7. Time Zones and Job Scheduling
Interpretation and scheduling are done in the local time zone of the machine where the Go application is running. Care must be taken during daylight-saving time changes, as certain times may not exist or be repeated.
8. Thread Safety and Synchronization
The cron library is designed to be thread-safe and concurrent. It is crucial for the caller to maintain a correct sequencing of method invocations to avoid race conditions. Proper synchronization mechanisms must be used when necessary to ensure jobs are scheduled and executed correctly.