cronrunner

package
v2.1.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 30, 2024 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Overview

Package cronrunner provides module wrapper for github.com/robfig/cron/v3.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrNilCronAfterInit = fmt.Errorf("cron.Cron was nil, WithCron is required option")
	ErrAddFuncToNilCron = fmt.Errorf("WithCron has to be applied before WithFunc")
)

Functions

This section is empty.

Types

type Opt

type Opt func(r *Runner) error

func WithCron

func WithCron(c *cron.Cron) Opt

WithCron sets cron.Cron instance.

func WithFunc

func WithFunc(spec string, fn func()) Opt

WithFunc adds given functions to cron runner with given spec.

type Runner

type Runner struct {
	// contains filtered or unexported fields
}

Runner is a wrapper around cron.Cron implementing service.Module interface.

func New

func New(opts ...Opt) *Runner

New creates Runner with given options. Options are applied in same order as they were provided. WithCron is required option.

Example
runner := cronrunner.New(
	cronrunner.WithCron(cron.New(cron.WithSeconds())),
	cronrunner.WithFunc("@every 1s", func() {
		fmt.Println("cron job executed")
		syscall.Kill(syscall.Getpid(), syscall.SIGINT) //nolint: errcheck
	}),
)

err := service.Run(service.Modules{siglistener.New(os.Interrupt), runner})
if err != nil {
	fmt.Println(err)
	os.Exit(1)
}
Output:

cron job executed
Example (CustomAddFunc)
var (
	id  cron.EntryID
	err error
)

c := cron.New(cron.WithSeconds())
id, err = c.AddFunc("@every 1s", func() {
	fmt.Printf("hello from job with id: %d\n", id)
	syscall.Kill(syscall.Getpid(), syscall.SIGINT) //nolint: errcheck
})
if err != nil {
	fmt.Println(err)
	os.Exit(1)
}

err = service.Run(service.Modules{
	siglistener.New(os.Interrupt),
	cronrunner.New(cronrunner.WithCron(c)),
})
if err != nil {
	fmt.Println(err)
	os.Exit(1)
}
Output:

hello from job with id: 1

func (*Runner) Init

func (r *Runner) Init() error

Init initializes Runner with given options.

func (*Runner) Name

func (r *Runner) Name() string

func (*Runner) Run

func (r *Runner) Run() error

Run starts cron job runner.

func (*Runner) Stop

func (r *Runner) Stop() error

Stop stops cron job runner.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL