1. Overview of scheduled crON tasks

Cron uses expressions to specify when a task should be executed, asynchronously.The expression is described as follows

# # file format specification ┌ ─ ─ minutes (0-59) # │ ┌ ─ ─ hours (0-23) # │ │ ┌ ─ ─ day (# 1-31) │ │ │ ┌ ─ month (# 1-12) │ │ │ │ ┌ ─ week (0 to 6, (from Sunday to Saturday) # │ │ # * * * * *Copy the code

Check out the image above at https://crontab.guru/#0_4_8-14_*_* to see if your expression is correct

Golang cron releases scheduled tasks

1. Install the latest cron third-party library. The biggest difference between cron and the old version is that if the second level definition is required, additional parameters need to be passed in, and parameters can be freely configured

go get github.com/robfig/cron/v3
Copy the code

2. Example:

package main

import (
   "fmt"
   "github.com/robfig/cron/v3"
   "time"
)

func TestCron(a) {
   c := cron.New()
   i := 1
   EntryID, err := c.AddFunc("*/1 * * * *".func(a) {
      fmt.Println(time.Now(), "Execute every minute.", i)
      i++
   })
   fmt.Println(time.Now(), EntryID, err)

   c.Start()
   time.Sleep(time.Minute * 5)}func main(a) {
   TestCron()
}
Copy the code

Get the result ->

2021-09-06 10:26:32.7499139 +0800 CST m=+0.003000001 1 <nil> 2021-09-06 10:27:00.0009139 +0800 CST m=+27.254000001 Run the following command every minute: 1 2021-09-06 10:28:00.0009139 +0800 CST M =+87.254000001 Run the following command every minute :2 2021-09-06 10:29:00.0009139 +0800 CST M =+147.254000001 Run the following command every minute. 3 2021-09-06 10:30:00.0009139 +0800 CST M =+207.254000001 Run the following command every minute +0800 CST m=+267.254000001 Run 5 every minuteCopy the code

3. Relative changes of the old version (common parts, please refer to the detailsGo A library of Cron per day)

3.1. Second level operation

cron.WithSeconds()

3.2. Skip this function before it finishes executing

cron.WithChain(cron.SkipIfStillRunning(cron.DefaultLogger))

3.3. Print task logs

cron.WithLogger(cron.VerbosePrintfLogger(log.New(os.Stdout, "cron: ", log.LstdFlags)))

3.4. To summarize the above examples:

package main

import (
   "fmt"
   "github.com/robfig/cron/v3"
   "log"
   "os"
   "time"
)

func TestCron(a) {
   c := cron.New(cron.WithSeconds(), cron.WithChain(cron.SkipIfStillRunning(cron.DefaultLogger)), cron.WithLogger(
      cron.VerbosePrintfLogger(log.New(os.Stdout, "cron: ", log.LstdFlags))))
   i := 1
   EntryID, err := c.AddFunc("*/5 * * * * *".func(a) {
      fmt.Println(time.Now(), "Every 5s ----------------", i)
      time.Sleep(time.Second *6)
      i++
   })
   fmt.Println(time.Now(), EntryID, err)

   c.Start()
   time.Sleep(time.Second * 30)}func main(a) {
   TestCron()
}
Copy the code
The result is that the function is still there when the second time comestime.sleep“, then skipped ->
2021-09-06 10:55:44.5709139 +0800 CST m=+0.003000001 1 <nil> cron: 2021/09/06 10:55:44 start cron: 2021/09/06 10:55:44 schedule, now=2021-09-06T10:55:44+08:00, entry=1, next=2021-09-06T10:55:45+08:00 cron: 2021/09/06 10:55:45 wake, now=2021-09-06T10:55:45+08:00 cron: 2021/09/06 10:55:45 run, now=2021-09-06T10:55:45+08:00, entry=1, Next =2021-09-06T10:55:50+08:00 2021-09-06 10:55:45.0009139 +0800 CST m=+0.433000001 every 5s ---------------- 1 cron: 2021/09/06 10:55:50 wake, now=2021-09-06T10:55:50+08:00 cron: 2021/09/06 10:55:50 run, now=2021-09-06T10:55:50+08:00, entry=1, next=2021-09-06T10:55:55+08:00 cron: 2021/09/06 10:55:55 wake, now=2021-09-06T10:55:55+08:00 cron: 2021/09/06 10:55:55 run, now=2021-09-06T10:55:55+08:00, entry=1, Next =2021-09-06T10:56:00+08:00 2021-09-06 10:55:55.0009139 +0800 CST m=+10.433000001 every 5s ---------------- 2 cron: 2021/09/06 10:56:00 wake, now=2021-09-06T10:56:00+08:00 cron: 2021/09/06 10:56:00 run, now=2021-09-06T10:56:00+08:00, entry=1, next=2021-09-06T10:56:05+08:00 cron: 2021/09/06 10:56:05 wake, now=2021-09-06T10:56:05+08:00 cron: 2021/09/06 10:56:05 run, now=2021-09-06T10:56:05+08:00, entry=1, Next =2021-09-06T10:56:10+08:00 2021-09-06 10:56:05.0009139 +0800 CST m=+20.433000001 every 5s ---------------- 3 cron: 2021/09/06 10:56:10 wake, now=2021-09-06T10:56:10+08:00 cron: 2021/09/06 10:56:10 run, now=2021-09-06T10:56:10+08:00, entry=1, next=2021-09-06T10:56:15+08:00Copy the code

3.5. Start multiple tasksAddJob:

package main

import (
   "fmt"
   "github.com/robfig/cron/v3"
   "time"
)

type Job1 struct{}func (t Job1) Run(a) {
   fmt.Println(time.Now(), "I'm Job1")}type Job2 struct{}func (t Job2) Run(a) {
   fmt.Println(time.Now(), "I'm Job2")}func TestCron(a) {
   c := cron.New(cron.WithSeconds())

   EntryID, err := c.AddJob("*/5 * * * * *", Job1{})
   fmt.Println(time.Now(), EntryID, err)

   EntryID, err = c.AddJob("*/10 * * * * *", Job2{})
   fmt.Println(time.Now(), EntryID, err)

   c.Start()
   time.Sleep(time.Second * 30)}func main(a) {
   TestCron()
}
Copy the code
Get the result ->
2021-09-06 11:14:19.5669139 +0800 CST m=+0.003000001 1 <nil> 2021-09-06 11:14:19.5959139 +0800 CST m=+0.032000001 2 <nil> 2021-09-06 11:14:20.0009139 +0800 CST M =+0.437000001 I'm Job2 2021-09-06 11:14:20.0009139 +0800 CST M =+0.437000001 I'm Job1 2021-09-06 11:14:25.0009139 +0800 CST M =+5.437000001 I'm Job1 2021-09-06 11:14:30.0009139 +0800 CST I'm Job2 2021-09-06 11:14:35.0009139 +0800 CST M =+10.437000001 I'm Job1 2021-09-06 11:14:35.0009139 +0800 CST m=+15.437000001 I'm Job1 2021-09-06 11:14:40.0009139 +0800 CST M =+20.437000001 I'm Job1 2021-09-06 11:14:40.0009139 +0800 CST M =+20.437000001 I'm Job2 2021-09-06 11:14:45.0009139 +0800 CST M =+25.437000001 I'm Job1Copy the code