笔记:golang cron

package main

import (
    "context"
    "fmt"
    "github.com/robfig/cron/v3"
    "sync"
)
type MyJob struct {
    Name string
}
func main() {
    mj := MyJob{"pp"}
    mjP := &mj
    var wg sync.WaitGroup
    c := cron.New()
    ctx := context.WithValue(context.Background(), "id", 1)
    wg.Add(1)
    c.AddFunc("@every 2s", func() {
        PrintName(ctx, &wg, mjP.Name)
    })
    c.AddJob("@every 2s", MyJob{mjP.Name})
    c.Start()
    wg.Wait()
}

func PrintName(ctx context.Context, wg *sync.WaitGroup, name string) {
    fmt.Printf("name:%snid:%dn", name, ctx.Value("id"))
}

func (mj MyJob) Run() {
    fmt.Printf("Hello,%sn", mj.Name)
    if err := errp(); err != nil {
    }
}

func errp() error {
    return nil
}
none
最后修改于:2022年05月04日 00:04

添加新评论

召唤看板娘