robfig/cron(a cron library for go)

·

2 min read

use like linux crontab for golang link

The demo code

package main

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

func main() {

    // use WithSeconds to alow seconds,default not
    c := cron.New(cron.WithSeconds())

    _, err := c.AddFunc("*/2 * * * * *", func() {
        log.Println("execute per 2 seconds", "Hello world!")
    })

    if err != nil {
        log.Println("AddFunc err: ", err)
        return
    }

    c.Start()

    select {}
}

source code analysis

  1. 初始化Cron
  2. 通过AddFunc等方法注册Entry到Cron中,Entry携带Schedule规则,.Next方法可以返回下次执行时间
  3. 启动Cron,起一个goroutine 两个for循环 (1)根据下次执行时间将Entry排序,New一个Timer间隔为第一个Entry下次执行距现在间隔 (2) for select 收到Timer.C 起一个goroutine开始执行Entry中的任务,跳出第二个循环,进入第一个循环继续排序 for select,如果被打断return 关闭goroutine

core source code

//cron.go
func (c *Cron) run() {
    c.logger.Info("start")

    // Figure out the next activation times for each entry.
    now := c.now()
    for _, entry := range c.entries {
        entry.Next = entry.Schedule.Next(now)
        c.logger.Info("schedule", "now", now, "entry", entry.ID, "next", entry.Next)
    }

    for {
        // Determine the next entry to run.
        sort.Sort(byTime(c.entries))

        var timer *time.Timer
        if len(c.entries) == 0 || c.entries[0].Next.IsZero() {
            // If there are no entries yet, just sleep - it still handles new entries
            // and stop requests.
            timer = time.NewTimer(100000 * time.Hour)
        } else {
            timer = time.NewTimer(c.entries[0].Next.Sub(now))
        }

        for {
            select {
            case now = <-timer.C:
                now = now.In(c.location)
                c.logger.Info("wake", "now", now)

                // Run every entry whose next time was less than now
                for _, e := range c.entries {
                    if e.Next.After(now) || e.Next.IsZero() {
                        break
                    }
                    c.startJob(e.WrappedJob)
                    e.Prev = e.Next
                    e.Next = e.Schedule.Next(now)
                    c.logger.Info("run", "now", now, "entry", e.ID, "next", e.Next)
                }

            case newEntry := <-c.add:
                timer.Stop()
                now = c.now()
                newEntry.Next = newEntry.Schedule.Next(now)
                c.entries = append(c.entries, newEntry)
                c.logger.Info("added", "now", now, "entry", newEntry.ID, "next", newEntry.Next)

            case replyChan := <-c.snapshot:
                replyChan <- c.entrySnapshot()
                continue

            case <-c.stop:
                timer.Stop()
                c.logger.Info("stop")
                return

            case id := <-c.remove:
                timer.Stop()
                now = c.now()
                c.removeEntry(id)
                c.logger.Info("removed", "entry", id)
            }

            break
        }
    }
}