flc1125 / go-cron

The cron library is a cron job library for Go. It is a fork of robfig/cron with some improvements.
https://github.com/flc1125/go-cron
MIT License
2 stars 0 forks source link

proposal: Middleware for a single job supports configuring some names, etc. #37

Open flc1125 opened 2 weeks ago

flc1125 commented 2 weeks ago

ex:

package main

import (
    "context"
    "log"
    "time"

    "github.com/flc1125/go-cron/v4"
    "github.com/flc1125/go-cron/v4/middleware/recovery"
)

func main() {
    c := cron.New(
        cron.WithSeconds(),
        cron.WithMiddleware(
            recovery.New(),
        ),
    )

    c.AddFunc("* * * * * *", func(ctx context.Context) error {
        log.Println("hello")
        return nil
    })
    c.AddFunc("* * * * * *", func(ctx context.Context) error {
        log.Println("world")
        return nil
    }, cron.WithName("world"))

    c.Start()
    defer c.Stop()

    time.Sleep(time.Second * 10)
}