cron

package module
v0.29.0 Latest Latest
Warning

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

Go to latest
Published: Jun 22, 2026 License: MIT Imports: 7 Imported by: 1

README

cron

cron 是 infrago 的模块

包定位

  • 类型:模块
  • 作用:定时任务模块,负责任务注册、调度与执行。

主要功能

  • 对上提供统一模块接口
  • 对下通过驱动接口接入具体后端
  • 支持按配置切换驱动实现

快速接入

import _ "github.com/infrago/cron"
[cron]
driver = "default"

驱动实现接口列表

以下接口由驱动实现(来自模块 driver.go):

Driver
  • Connection(*Instance) (Connection, error)
Connection
  • Open() error
  • Close() error
  • Add(name string, job Job) error
  • Enable(name string) error
  • Disable(name string) error
  • Remove(name string) error
  • List() (map[string]Job, error)
  • AppendLog(log Log) error
  • History(jobName string, offset, limit int) (int64, []Log, error)
  • Lock(key string, ttl time.Duration) (bool, error)

全局配置项(所有配置键)

配置段:[cron]

  • 未检测到配置键(请查看模块源码的 configure 逻辑)

说明

  • setting 一般用于向具体驱动透传专用参数
  • 多实例配置请参考模块源码中的 Config/configure 处理逻辑

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Add added in v0.7.0

func Add(name string, job Job) error

func Disable added in v0.7.0

func Disable(name string) error

func Enable added in v0.7.0

func Enable(name string) error

func ListJobs added in v0.7.0

func ListJobs() map[string]Job

func Remove added in v0.7.0

func Remove(name string) error

Types

type Config

type Config struct {
	Driver  string
	Tick    time.Duration
	Sync    time.Duration
	LockTTL time.Duration
	Setting Map
}

type Connection added in v0.7.0

type Connection interface {
	Open() error
	Close() error

	// Add stores or updates one job definition.
	Add(name string, job Job) error
	// Enable marks one job as enabled.
	Enable(name string) error
	// Disable marks one job as disabled.
	Disable(name string) error
	// Remove deletes one job definition.
	Remove(name string) error
	// List returns all persisted job definitions.
	List() (map[string]Job, error)
	// Append writes one execution log entry.
	AppendLog(log Log) error
	// History returns logs of one job, with total count and paged results.
	History(jobName string, offset, limit int) (int64, []Log, error)

	// Lock tries to acquire a distributed lock key with ttl.
	// It returns true when lock is acquired, false when already locked.
	Lock(key string, ttl time.Duration) (bool, error)
}

type Driver added in v0.7.0

type Driver interface {
	Connection(*Instance) (Connection, error)
}

type Instance

type Instance struct {
	Config  Config
	Setting Map
}

type Job

type Job struct {
	Name      string   `json:"name"`
	Desc      string   `json:"desc"`
	Target    string   `json:"target"`
	Schedule  string   `json:"schedule"`
	Schedules []string `json:"schedules"`
	Disabled  bool     `json:"disabled"`
	Payload   Map      `json:"payload"`
	Setting   Map      `json:"setting"`
}

func (Job) RegistryComponent added in v0.7.0

func (Job) RegistryComponent() string

type Jobs added in v0.7.0

type Jobs map[string]Job

func (Jobs) RegistryComponent added in v0.7.0

func (Jobs) RegistryComponent() string

type Log added in v0.7.0

type Log struct {
	Job       string    `json:"job"`
	Schedule  string    `json:"schedule"`
	Target    string    `json:"target"`
	Payload   Map       `json:"payload"`
	Triggered time.Time `json:"triggered"`
	Started   time.Time `json:"started"`
	Ended     time.Time `json:"ended"`
	Success   bool      `json:"success"`
	State     string    `json:"state"`
	Error     string    `json:"error"`
}

func ListLogs added in v0.7.0

func ListLogs(jobName string, offset, limit int) (int64, []Log)

type Module

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

func (*Module) Close added in v0.7.0

func (m *Module) Close()

func (*Module) Config added in v0.7.0

func (m *Module) Config(global Map)

func (*Module) Delete added in v0.7.0

func (m *Module) Delete(name string) error

func (*Module) Disable added in v0.7.0

func (m *Module) Disable(name string) error

func (*Module) Enable added in v0.7.0

func (m *Module) Enable(name string) error

func (*Module) ListLogs added in v0.7.0

func (m *Module) ListLogs(jobName string, offset, limit int) (int64, []Log)

func (*Module) Open added in v0.7.0

func (m *Module) Open()

func (*Module) Register

func (m *Module) Register(name string, value Any)

func (*Module) RegisterConfig added in v0.7.0

func (m *Module) RegisterConfig(config Config)

func (*Module) RegisterDriver added in v0.7.0

func (m *Module) RegisterDriver(name string, driver Driver)

func (*Module) RegisterJob added in v0.7.0

func (m *Module) RegisterJob(name string, job Job)

func (*Module) RegisterJobs added in v0.7.0

func (m *Module) RegisterJobs(jobs Jobs)

func (*Module) Setup added in v0.7.0

func (m *Module) Setup()

func (*Module) Start added in v0.7.0

func (m *Module) Start()

func (*Module) Stop added in v0.7.0

func (m *Module) Stop()

func (*Module) Upsert added in v0.7.0

func (m *Module) Upsert(name string, job Job) error

Jump to

Keyboard shortcuts

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