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
cron crontab go golang library robfig-cron

Cron

Supported Go Versions Package Version GoDoc codecov Go Report Card lint tests MIT license

The cron library is a cron job library for Go.

It is a fork of robfig/cron with some improvements.

Thanks to robfig/cron for the original work, and thanks to all the contributors.

[!IMPORTANT]
v4.x may introduce situations that are not backward compatible.

The reason for this is that we are using v4.x as a transitional version. In this version, we will try to improve the functionality of the components as much as possible until the release of v5.x.

When releasing a new version, backward compatibility is the default behavior. If there are any incompatibilities, they will be indicated in the release notes.

Installation

go get github.com/flc1125/go-cron/v4

Usage

package main

import (
    "context"

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

func main() {
    c := cron.New(
        cron.WithSeconds(), // if you want to use seconds, you can use this option
        cron.WithMiddleware(
            recovery.New(),      // recover panic
            nooverlapping.New(), // prevent job overlapping
        ),
        cron.WithContext(context.Background()), // use custom context
        // ... other options
    )

    // use middleware
    c.Use(recovery.New(), nooverlapping.New()) // use middleware

    // add job
    entryID, _ := c.AddJob("* * * * * *", cron.JobFunc(func(ctx context.Context) error {
        // do something
        return nil
    }))
    _ = entryID

    // add func
    _, _ = c.AddFunc("* * * * * *", func(ctx context.Context) error {
        // do something
        return nil
    }, recovery.New(), nooverlapping.New()) // use middleware for this job

    // start cron
    c.Start()

    // stop cron
    c.Stop()
}

License