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 ¶
- func GetCurrentSchedule(ctx context.Context) time.Time
- func GetNextSchedule(ctx context.Context) time.Time
- type Config
- type Cron
- type CronJobMetrics
- type JobDescriptor
- type JobID
- type JobOption
- func DelayIfOverlap() JobOption
- func Recover(logger log.Logger) JobOption
- func SkipIfOverlap() JobOption
- func TimeoutIfOverlap() JobOption
- func WithLogging(logger log.Logger) JobOption
- func WithMetrics(metrics *CronJobMetrics) JobOption
- func WithName(name string) JobOption
- func WithSchedule(schedule cron.Schedule) JobOption
- func WithTracing(tracer opentracing.Tracer) JobOption
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetCurrentSchedule ¶
GetCurrentSchedule returns the current 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 (*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.
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) 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 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 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 ¶
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 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.