Categorygithub.com/flc1125/go-cron/v4
package
4.2.0
Repository: https://github.com/flc1125/go-cron.git
Documentation: pkg.go.dev

# Packages

No description provided by the author

# README

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()
}

Middleware

  • recovery: Recovers from panics in job execution, ensuring system stability.
  • delayoverlapping: Delays execution of overlapping jobs instead of running them concurrently.
  • nooverlapping: Prevents concurrent execution of the same job.
  • distributednooverlapping: Prevents concurrent execution across multiple instances using distributed locking.
  • otel: Provides OpenTelemetry integration for job execution tracing.

License