1. Basic use
Quote package: "/robfig/cron/v3"
1.1 Initialize the instance new()
- grammar
func New(opts ...Option) *Cron
- Syntax example
c := ()
1.2 Add timed tasks AddFunc()
- grammar
func (c *Cron) AddFunc(spec string, cmd func()) (EntryID, error)
- Syntax example
("* * * * *", func() { ("====== Recruitment completed: Soldier +1 =======") })
1.3 Start the timing task Start()
- grammar
func (c *Cron) Start()
- Syntax example
()
1.4 Complete Example
- Code
package main import ( "fmt" "/robfig/cron/v3" "time" ) func main() { //c := (()) c := () () (()) ("* * * * *", func() { ((),"====== Recruitment completed: Soldier +1 =======") }) ("*/5 * * * *", func() { ((),"====== Recruitment completed: Chief +1 =======") }) select {} }
- result
2022-12-16 16:40:32.155401624 +0800 CST m=+0.000051359
2022-12-16 16:41:00.012072936 +0800 CST m=+27.856722654 ====== Recruitment completed: Soldier +1 =======
2022-12-16 16:42:00.061328164 +0800 CST m=+87.905977883 ===== Recruitment completed: Soldier +1 =======
2022-12-16 16:43:00.003474685 +0800 CST m=+147.848124410 ===== Recruitment completed: Soldier +1 =======
2022-12-16 16:44:00.059798048 +0800 CST m=+207.904447766 ===== Recruitment completed: Soldier +1 ======
2022-12-16 16:45:00.06000457 +0800 CST m=+267.904654288 ===== Recruitment completed: Soldier +1 =======
2022-12-16 16:45:00.060084718 +0800 CST m=+267.904734436 ===== Recruitment completed: Soldier +1 =======
2022-12-16 16:46:00.042836167 +0800 CST m=+327.887485885 ===== Recruitment completed: Soldier +1 =======
2022-12-16 16:47:00.000225283 +0800 CST m=+387.844875007 ===== Recruitment completed: Soldier +1 =======
2022-12-16 16:48:00.000241943 +0800 CST m=+447.844891661 ===== Recruitment completed: Soldier +1 =======
2022-12-16 16:49:00.040677982 +0800 CST m=+507.885327700 ====== Recruitment completed: Soldier +1 =======
2022-12-16 16:50:00.00034055 +0800 CST m=+567.844990268 ===== Recruitment completed: Soldier +1 ======
1.5 Opportunities for the first timed task
As can be seen above, the first time the scheduled task is executed is the first opportunity for the program to begin:
- 16:40:32 The program starts executing.
- 16:41:00 is a whole minute, and soldiers are recruited every minute to start execution.
- 16:45:00 is the first five minutes after the start, so the recruitment sergeant will start to execute once every five minutes.
1.6 spec settings
- grammar
The five values are:
Minutes Hours Day Month Week
- Example of usage
* * * * * //Execute every minute00 */3 * * * //Execute every 3 hours10 03 * * * //Enforced at 3:10 am every day (00:10)00 02 * * 7 //Execute at 2:00 am every Sunday00 02 14 2 7 //Enforced at 2:00 am on February 14th or at 2:00 am on Sunday (the last 0 and 7 represent Sunday)00 02 2,5,8 * * //Execute at 2:00 on 2, 5 and 8 of every month00 02 1-8 * * //Execute from 2:00 am every month from 1 to 8
2. Granularity to seconds
2.1 Syntax Example
- initialization
c := (())
- spec
The leftmost bit is the second, and the other bits are the same as the previous minute.
2.2 Complete Example
- Code
package main import ( "fmt" "/robfig/cron/v3" "time" ) func main() { c := (()) (()) ("*/7 * * * * *", func() { ((),"====== Construction completed: Catstone +1 =======") }) ("*/3 * * * * *", func() { ((),"====== Recruitment completed: Soldier +1 =======") }) () select {} }
- result
2022-12-16 17:29:26.672306792 +0800 CST m=+0.000036478
2022-12-16 17:29:27.001131052 +0800 CST m=+0.328860748 ====== Recruitment completed: Soldier +1 =======
2022-12-16 17:29:28.000333997 +0800 CST m=+1.328063678 ====== Construction completed: Catstone +1 =======
2022-12-16 17:29:30.000180946 +0800 CST m=+3.327910638 ====== Recruitment completed: Soldier +1 =======
2022-12-16 17:29:33.002458758 +0800 CST m=+6.330188430 ====== Recruitment completed: Soldier +1 =======
2022-12-16 17:29:35.000249936 +0800 CST m=+8.327979610 ====== Construction completed: Catstone +1 =======
2022-12-16 17:29:36.000311745 +0800 CST m=+9.328041418 ===== Recruitment completed: Soldier +1 =======
2022-12-16 17:29:39.002463183 +0800 CST m=+12.330192861 ====== Recruitment completed: Soldier +1 =======
2022-12-16 17:29:42.000297949 +0800 CST m=+15.328027640 ====== Construction completed: Catstone +1 =======
2022-12-16 17:29:42.000353037 +0800 CST m=+15.328082842 ====== Recruitment completed: Soldier +1 =======
2022-12-16 17:29:45.00024908 +0800 CST m=+18.327978755 ====== Recruitment completed: Soldier +1 =======
2022-12-16 17:29:48.002469543 +0800 CST m=+21.330199229 ====== Recruitment completed: Soldier +1 ======
As can be seen above:
- 17:29:26 The program starts executing
- 17:29:27 , 27 seconds is the integer multiple of the first 3 seconds, and the task of recruiting a soldier begins to be executed every 3 seconds
- 17:29:35, 35 seconds is an integer multiple of the first 7 seconds. The task of building a catapult every 7 seconds begins to be executed.
3. By time interval
3.1 Syntax
symbol | illustrate | Example |
---|---|---|
@yearly | Performed every year | |
@monthly | Monthly execution | |
@weekly | Perform weekly | |
@daily | Perform every day | |
@hourly | Perform every hour | |
@every | Specify interval | "@every 1h25m" |
3.2 Complete Example (@every)
- Code
package main import ( "fmt" "/robfig/cron/v3" "time" ) func main() { c := (()) (()) ("@every 1m", func() { ((),"====== Construction completed: Catstone +1 =======") }) () select {} }
result
As can be seen below, it is different from the execution opportunity of the example in the previous article "Basic Use". It will be executed one minute after the program starts, rather than in an integer multiple of the next minute.
- The program starts at 09:53:11 and executes the first time after one minute (09:54:11).
- If it is a basic method: the program starts at 09:53:11, it will be executed for the first time at 09:54:00
2022-12-21 09:53:11.620438535 +0800 CST m=+0.000041561
2022-12-21 09:54:11.008589266 +0800 CST m=+59.388192276 ====== Construction completed: Catstone +1 =======
2022-12-21 09:55:11.052366134 +0800 CST m=+119.431969148 ====== Construction completed: Catstone +1 =======
2022-12-21 09:56:11.008584381 +0800 CST m=+179.388187390 ====== Construction completed: Catstone +1 =======
2022-12-21 09:57:11.018895862 +0800 CST m=+239.398498872 ====== Construction completed: Catstone +1 =======
2022-12-21 09:58:11.018836287 +0800 CST m=+299.398439302 ====== Construction completed: Catstone +1 =======
2022-12-21 09:59:11.008604569 +0800 CST m=+359.388207580 ====== Construction completed: Catstone +1 =======
2022-12-21 10:00:11.006091578 +0800 CST m=+419.385694593 ====== Construction completed: Catstone +1 =======
2022-12-21 10:01:11.010293781 +0800 CST m=+479.389896790 ====== Construction completed: Catstone +1 =======
3.3 Complete Example
- Code
package main import ( "fmt" "/robfig/cron/v3" "time" ) func main() { c := (()) (()) ("@weekly", func() { ((),"====== Construction completed: Catstone +1 =======") }) () select {} }
- result
It's too long to wait
The above is the detailed explanation of the basic usage of gocron timing tasks. For more information about gocron timing tasks, please pay attention to my other related articles!