cronopts

package
v0.11.1 Latest Latest
Warning

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

Go to latest
Published: Jan 6, 2022 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package cronopts contains the options for cron.

Example (CronJobMetrics)
package main

import (
	"context"
	"fmt"
	"github.com/DoNewsCode/core"
	"github.com/DoNewsCode/core/cronopts"
	"github.com/DoNewsCode/core/observability"
	"github.com/robfig/cron/v3"
	"math/rand"
	"time"
)

type CronModule struct {
	metrics *cronopts.CronJobMetrics
}

func NewCronModule(metrics *cronopts.CronJobMetrics) CronModule {
	return CronModule{metrics: metrics.Module("test_module")}
}

func (c CronModule) ProvideCron(crontab *cron.Cron) {
	// Create a new cron job, and measure its execution durations.
	crontab.AddJob("* * * * *", c.metrics.Job("test_job").Measure(cron.FuncJob(func() {
		fmt.Println("running")
		// For 50% chance, the job may fail. Report it to metrics collector.
		if rand.Float64() > 0.5 {
			c.metrics.Fail()
		}
	})))
}

func main() {
	c := core.Default()
	c.Provide(observability.Providers())
	c.AddModuleFunc(NewCronModule)

	ctx, cancel := context.WithTimeout(context.Background(), 1500*time.Millisecond)
	defer cancel()

	c.Serve(ctx)
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Measure added in v0.11.0

func Measure(c *CronJobMetrics) cron.JobWrapper

Measure returns a job wrapper that wraps the given job and records the duration and success.

Types

type CronJobMetrics added in v0.11.0

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

CronJobMetrics collects metrics for cron jobs.

func NewCronJobMetrics added in v0.11.0

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

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

func (*CronJobMetrics) Fail added in v0.11.0

func (c *CronJobMetrics) Fail()

Fail marks the job as failed.

func (*CronJobMetrics) Job added in v0.11.0

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

Job specifies the job label for CronJobMetrics.

func (*CronJobMetrics) Measure added in v0.11.0

func (c *CronJobMetrics) Measure(job cron.Job) cron.Job

Measure wraps the given job and records the duration and success.

func (*CronJobMetrics) Module added in v0.11.0

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

Module specifies the module label for CronJobMetrics.

type CronLogAdapter

type CronLogAdapter struct {
	Logging log.Logger
}

CronLogAdapter is an adapter between kitlog and cron logger interface

func (CronLogAdapter) Error

func (c CronLogAdapter) Error(err error, msg string, keysAndValues ...interface{})

Error implements cron.Logger

func (CronLogAdapter) Info

func (c CronLogAdapter) Info(msg string, keysAndValues ...interface{})

Info implements cron.Logger

Jump to

Keyboard shortcuts

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