cccron

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 12, 2023 License: Apache-2.0 Imports: 10 Imported by: 0

README

cc-go-cron

Installation

go get github.com/crosect/cc-go-cron

Usage

Create Job implement cron.Job interface

package jobs

import (
	"github.com/robfig/cron/v3"
	"github.com/crosect/cc-go/log"
)

type FirstJob struct {
}

func NewFirstJob() cron.Job {
	return &FirstJob{}
}

// Run job handler
func (j *FirstJob) Run() {
	log.Infof("[FirstJob] job start")
	// TODO: implement logic here
	log.Infof("[FirstJob] job stop")
}

Provide job and provide cron to fx

package bootstrap

import (
	"github.com/crosect/cc-go-cron"
	"go.uber.org/fx"
)

func All() fx.Option {
	return fx.Options(
		// Enable Cron
		cccron.EnableCron(),
		
		// Provide Jobs
		cccron.ProvideJob(jobs.NewFirstJob),
		cccron.ProvideJob(jobs.NewSecondJob),

		// Graceful shutdown.
		cccron.EnableGracefulShutdown(),
	)
}

Update YAML config

app:
  cron:
    jobs:
      FirstJob:
        expression: "0 * * * * *" # Every minute at second 0
      SecondJob:
        expression: "@every 10s"
        disable: true

Testing

Enable cc-go-cron Test Suite

package testing

import (
	"github.com/crosect/cc-go-cron/testutil"
)

func Bootstrap() {
	fx.New(
		testutil.EnableCronTestSuite(),
	)
}

Run a job

package testing

import (
	"github.com/crosect/cc-go-cron/testutil"
)

func RunJob() {
	testutil.RunJob("JobName")
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EnableCron

func EnableCron() fx.Option

func EnableGracefulShutdown

func EnableGracefulShutdown() fx.Option

func ProvideJob

func ProvideJob(job interface{}) fx.Option

Types

type JobProperties

type JobProperties struct {
	Expression string
	Disable    bool
}

type JobsProperties

type JobsProperties struct {
	Jobs map[string]JobProperties
}

func NewJobsProperties

func NewJobsProperties(loader config.Loader) (*JobsProperties, error)

func (*JobsProperties) Prefix

func (t *JobsProperties) Prefix() string

type Scheduler

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

func NewScheduler

func NewScheduler(params SchedulerConstructorParams) (*Scheduler, error)

func (*Scheduler) Start

func (s *Scheduler) Start()

func (*Scheduler) Stop

func (s *Scheduler) Stop() error

type SchedulerConstructorParams

type SchedulerConstructorParams struct {
	fx.In
	Properties *JobsProperties
	Jobs       []cron.Job `group:"cron_job"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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