cherryCron

package module
v1.3.12 Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2023 License: MIT Imports: 5 Imported by: 3

README

cron组件

  • 支持cron表达式
  • 根据设定的时间规则定时执行函数

Install

Prerequisites
  • GO >= 1.17
Using go get
go get github.com/cherry-game/cherry/components/cron@latest

Quick Start

import cherryCron "github.com/cherry-game/cherry/components/cron"
// 以组件方式注入到cherry引擎
func Run(path, env, node string) {
    // 加载profile配置
    cherry.Configure(path, env, node)
    // cron以组件方式注册到cherry引擎
    cherryCron.RegisterComponent()
    // 启动cherry引擎
    cherry.Run(false, cherry.Cluster)
}

// 手工方式启动cron
func main() {
    cherryCron.Init()

    for i := 0; i <= 23; i++ {
        cherryCron.AddEveryDayFunc(func() {
            now := cherryTime.Now()
            cherryLogger.Infof("每天第%d点%d分%d秒运行", now.Hour(), now.Minute(), now.Second())
        }, i, 12, 34)
        cherryLogger.Infof("添加 每天第%d点执行的定时器", i)
    }

    for i := 0; i <= 59; i++ {
        cherryCron.AddEveryHourFunc(func() {
            cherryLogger.Infof("每小时第%d分执行一次", cherryTime.Now().Minute())
        }, i, 0)
        cherryLogger.Infof("添加 每小时第%d分的定时器", i)
    }

    cherryCron.Run()
}

example

Documentation

Index

Constants

View Source
const (
	Name = "cron_component"
)

Variables

This section is empty.

Functions

func AddDurationFunc

func AddDurationFunc(cmd func(), duration time.Duration) (cron.EntryID, error)

AddDurationFunc 每间隔x秒执行一次

func AddEveryDayFunc

func AddEveryDayFunc(cmd func(), hour, minutes, seconds int) (cron.EntryID, error)

AddEveryDayFunc 每天的x时x分x秒执行一次(每天1次)

func AddEveryHourFunc

func AddEveryHourFunc(cmd func(), minute, second int) (cron.EntryID, error)

AddEveryHourFunc 每小时的x分x秒执行一次(每天24次)

func AddFunc

func AddFunc(spec string, cmd func()) (cron.EntryID, error)

func AddJob

func AddJob(spec string, cmd cron.Job) (cron.EntryID, error)

func Entries

func Entries() []cron.Entry

func Entry

func Entry(id cron.EntryID) cron.Entry

func Init

func Init(opts ...cron.Option)

func Location

func Location() *time.Location

func New added in v1.3.1

func New(opts ...cron.Option) cfacade.IComponent

func Remove

func Remove(id cron.EntryID)

func Run

func Run()

func Schedule

func Schedule(schedule cron.Schedule, cmd cron.Job) cron.EntryID

func Start

func Start()

func Stop

func Stop()

Types

type Component

type Component struct {
	cfacade.Component
}

func (*Component) Init

func (p *Component) Init()

func (*Component) Name

func (*Component) Name() string

Name unique components name

func (*Component) OnStop

func (p *Component) OnStop()

type CronLogger

type CronLogger struct {
}

func (CronLogger) Error

func (CronLogger) Error(err error, _ string, _ ...interface{})

func (CronLogger) Info

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

Jump to

Keyboard shortcuts

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