golibcron

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2023 License: MIT Imports: 11 Imported by: 0

README

Golib Cron

Cron Job for Golib project.

Setup instruction

Base setup, see GoLib Instruction

Both go get and go mod are supported.

go get github.com/golibs-starter/golib-cron
Usage

Using fx.Option to include dependencies for injection.

package main

import (
	"context"
	golibcron "github.com/golibs-starter/golib-cron"
	"github.com/golibs-starter/golib/log"
	"go.uber.org/fx"
)

func main() {
	fx.New(
		// When you want to use redis
		golibcron.Opt(),

		// When you want to add new job
		golibcron.ProvideJob(NewYourFirstJob),
		golibcron.ProvideJob(NewYourSecondCronJob),

		// When you want to enable graceful shutdown.
		golibcron.OnStopHookOpt(),
	)
}

// YourFirstJob is an example about default Job structure
type YourFirstJob struct {
}

func NewYourFirstJob() golibcron.Job {
	return &YourFirstJob{}
}

// Run function will be triggered every job run
func (y YourFirstJob) Run(ctx context.Context) {
	log.Infoc(ctx, "Job started")
	// By using context, all logs will be printed will job name and run id. Eg:
	// {..."msg":"Job started","job_meta":{"name":"YourFirstJob","run_id":"5fd8bc9e-6250-11ee-b2a1-448a5b97ab48"}}
}

// YourSecondCronJob is an example about named Job structure
type YourSecondCronJob struct {
}

// Name func
// By default job name are detect by the job structs name.
// Add this function when you want to custom job name
func (y YourSecondCronJob) Name() string {
	return "YourSecondCronJobWithCustomName"
}

func NewYourSecondCronJob() golibcron.Job {
	return &YourSecondCronJob{}
}

func (y YourSecondCronJob) Run(ctx context.Context) {
	log.Infoc(ctx, "Job started")
}

Configuration
app:
  # Configuration available for golibcron.Opt()
  cron:

    # When you want to enable debug mode for cron.
    # It will print more detail about schedule info (disabled by default)
    enabledDebugMode: false

    # Declare your jobs
    jobs:
      - name: YourFirstJob

        #  The Cron Spec pattern requires 5 entries
        #  representing: minute, hour, day of month, month and day of week, in that order.
        #  It accepts
        #   - Standard crontab specs, e.g. "* * * * ?"
        #   - Descriptors, e.g. "@midnight", "@every 1h30m"
        # Check the cron pattern at: https://en.wikipedia.org/wiki/Cron
        spec: "@every 1m"

        # When you want to disable job. Accepts: true/false
        disabled: false

      - name: YourSecondCronJobWithCustomName
        spec: "* * * * *"
        disabled: true

Documentation

Index

Constants

View Source
const ContextLogJobMeta = "job_meta"
View Source
const ContextValueJobName = "job_attributes_name"
View Source
const ContextValueJobRunId = "job_attributes_run_id"

Variables

This section is empty.

Functions

func ContextExtractor

func ContextExtractor(ctx context.Context) []field.Field

func GetJobName

func GetJobName(job Job) string

func OnStopHook

func OnStopHook(in OnStopCronIn)

func OnStopHookOpt

func OnStopHookOpt() fx.Option

func Opt

func Opt() fx.Option

func ProvideJob

func ProvideJob(jobConstructor interface{}) fx.Option

func RegisterJob

func RegisterJob(in RegisterJobIn) error

func StartCron

func StartCron(c Engine)

Types

type Engine

type Engine interface {
	AddJob(spec string, cmd Job) error
	StartSync()
	StartAsync()
	Stop()
}

func NewDefaultRobfigCron

func NewDefaultRobfigCron(logger log.Logger, props *Properties) Engine

func NewRobfigCron

func NewRobfigCron(c *cron.Cron) Engine

type Job

type Job interface {
	Run(ctx context.Context)
}

type JobAttributes

type JobAttributes struct {
	Name  string `json:"name,omitempty"`
	RunId string `json:"run_id,omitempty"`
}

func (JobAttributes) MarshalLogObject

func (c JobAttributes) MarshalLogObject(encoder field.ObjectEncoder) error

type JobConfig

type JobConfig struct {
	Name     string
	Spec     string
	Disabled bool
}

type NamedJob

type NamedJob interface {
	Name() string
	Job
}

type OnStopCronIn

type OnStopCronIn struct {
	fx.In
	Lc   fx.Lifecycle
	Cron Engine
}

type Properties

type Properties struct {
	Jobs []JobConfig

	EnabledDebugMode bool
	// contains filtered or unexported fields
}

func NewProperties

func NewProperties(loader config.Loader) (*Properties, error)

func (*Properties) GetJob

func (o *Properties) GetJob(jobName string) (job JobConfig, found bool)

func (*Properties) PostBinding

func (o *Properties) PostBinding() error

func (*Properties) Prefix

func (o *Properties) Prefix() string

type RegisterJobIn

type RegisterJobIn struct {
	fx.In
	Engine Engine
	Jobs   []Job `group:"cron_job"`
	Props  *Properties
}

type RobfigCron

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

func (RobfigCron) AddJob

func (r RobfigCron) AddJob(spec string, cmd Job) error

func (RobfigCron) StartAsync

func (r RobfigCron) StartAsync()

func (RobfigCron) StartSync

func (r RobfigCron) StartSync()

func (RobfigCron) Stop

func (r RobfigCron) Stop()

type RobfigJob

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

func NewRobfigJob

func NewRobfigJob(job Job) *RobfigJob

func (RobfigJob) Run

func (r RobfigJob) Run()

type RobfigLogger

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

func NewRobfigLogger

func NewRobfigLogger(logger log.Logger, enableScheduleInfo bool) *RobfigLogger

func (RobfigLogger) Error

func (r RobfigLogger) Error(err error, msg string, keysAndValues ...interface{})

func (RobfigLogger) Info

func (r RobfigLogger) Info(msg string, keysAndValues ...interface{})

Jump to

Keyboard shortcuts

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