cron

package
v0.12.3 Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2022 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package cron is a partial rewrite based on the github.com/robfig/cron/v3 package. The API in this package enforces context passing and error propagation, and consequently enables better logging, metrics and tracing support.

Example
package main

import (
	"context"
	"fmt"
	"time"

	"github.com/DoNewsCode/core"
	"github.com/DoNewsCode/core/cron"
	"github.com/DoNewsCode/core/di"
	"github.com/DoNewsCode/core/observability"
)

type CronModule struct {
	metrics *cron.CronJobMetrics
}

func NewCronModule(metrics *cron.CronJobMetrics) *CronModule {
	return &CronModule{metrics: metrics}
}

func (module *CronModule) ProvideCron(crontab *cron.Cron) {
	crontab.Add("* * * * * *", func(ctx context.Context) error {
		fmt.Println("I am a cron")
		return nil
	}, cron.WithMetrics(module.metrics), cron.WithName("foo"))
}

func main() {
	c := core.Default(core.WithInline("log.level", "none"))
	c.Provide(observability.Providers())
	c.Provide(
		di.Deps{func() *cron.Cron {
			return cron.New(cron.Config{EnableSeconds: true})
		}},
	)

	c.AddModuleFunc(NewCronModule)
	ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
	defer cancel()

	c.Serve(ctx)
}
Output:

I am a cron

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetCurrentSchedule

func GetCurrentSchedule(ctx context.Context) time.Time

GetCurrentSchedule returns the current schedule for the given context.

func GetNextSchedule

func GetNextSchedule(ctx context.Context) time.Time

GetNextSchedule returns the next schedule for the given context.

Types

type Config

type Config struct {
	// Parser is the parser to parse cron expressions.
	Parser cron.ScheduleParser
	// Location is the timezone to use in parsing cron expressions.
	Location *time.Location
	// GlobalOptions are the job options that are applied to all jobs.
	GlobalOptions []JobOption
	// EnableSeconds is whether to enable seconds in the cron expression.
	EnableSeconds bool
}

Config is the configuration for the cron package.

type Cron

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

Cron schedules jobs to be run on the specified schedule.

func New

func New(config Config) *Cron

New returns a new Cron instance.

func (*Cron) Add

func (c *Cron) Add(spec string, runner func(ctx context.Context) error, middleware ...JobOption) (JobID, error)

Add adds a new job to the cron scheduler.A list of middleware can be supplied. Note the error returned by the runner will be discarded. It is the user's responsibility to handle the error via middleware.

func (*Cron) Descriptors

func (c *Cron) Descriptors() []JobDescriptor

Descriptors returns a list of all job descriptors.

func (*Cron) Remove

func (c *Cron) Remove(id JobID)

Remove removes a job from the cron scheduler.

func (*Cron) Run

func (c *Cron) Run(ctx context.Context) error

Run starts the cron scheduler. It is a blocking call.

type CronJobMetrics

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

CronJobMetrics collects metrics for cron jobs.

func NewCronJobMetrics

func NewCronJobMetrics(histogram metrics.Histogram, counter metrics.Counter) *CronJobMetrics

NewCronJobMetrics constructs a new *CronJobMetrics, setting default labels to "unknown".

func (*CronJobMetrics) Fail

func (c *CronJobMetrics) Fail()

Fail marks the job as failed.

func (*CronJobMetrics) Job

func (c *CronJobMetrics) Job(job string) *CronJobMetrics

Job specifies the job label for CronJobMetrics.

func (*CronJobMetrics) Module

func (c *CronJobMetrics) Module(module string) *CronJobMetrics

Module specifies the module label for CronJobMetrics.

func (*CronJobMetrics) Observe

func (c *CronJobMetrics) Observe(duration time.Duration)

Observe records the duration of the job.

func (*CronJobMetrics) Schedule

func (c *CronJobMetrics) Schedule(schedule string) *CronJobMetrics

Schedule specifies the schedule label for CronJobMetrics.

type JobDescriptor

type JobDescriptor struct {
	// ID is the identifier of job
	ID JobID
	// Name is an optional field typically added by WithName. It can be useful in logging and metrics.
	Name string
	// RawSpec contains the string format of cron schedule format.
	RawSpec string
	// Schedule is the parsed version of RawSpec. It can be overridden by WithSchedule.
	Schedule cron.Schedule
	// Run is the actual work to be done.
	Run func(ctx context.Context) error
	// contains filtered or unexported fields
}

JobDescriptor contains the information about jobs.

type JobID

type JobID int

JobID is the identifier of jobs.

type JobOption

type JobOption func(descriptors *JobDescriptor)

JobOption is a middleware for cron jobs.

func DelayIfOverlap

func DelayIfOverlap() JobOption

DelayIfOverlap returns a new JobDescriptor that will delay the job if it overlaps with another job.

func Recover

func Recover(logger log.Logger) JobOption

Recover returns a new JobDescriptor that will recover from panics.

func SkipIfOverlap

func SkipIfOverlap() JobOption

SkipIfOverlap returns a new JobDescriptor that will skip the job if it overlaps with another job.

func TimeoutIfOverlap

func TimeoutIfOverlap() JobOption

TimeoutIfOverlap returns a new JobDescriptor that will cancel the job's context if the next schedule is due.

func WithLogging

func WithLogging(logger log.Logger) JobOption

WithLogging returns a new Universal job that will log.

func WithMetrics

func WithMetrics(metrics *CronJobMetrics) JobOption

WithMetrics returns a new JobDescriptor that will report metrics.

func WithName

func WithName(name string) JobOption

WithName sets the name of the job.

func WithSchedule

func WithSchedule(schedule cron.Schedule) JobOption

WithSchedule sets the cron schedule of the job.

func WithTracing

func WithTracing(tracer opentracing.Tracer) JobOption

WithTracing returns a new Universal job that will trace.

Jump to

Keyboard shortcuts

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