cron

package
v0.0.15 Latest Latest
Warning

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

Go to latest
Published: Mar 5, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CronJob

type CronJob struct {
	ID             string       `json:"id" gorm:"column:id;primaryKey;type:varchar(32)"`
	UserID         string       `json:"userId,omitempty" gorm:"column:user_id;type:varchar(64);index"`
	Name           string       `json:"name" gorm:"column:name;type:varchar(100)"`
	Enabled        bool         `json:"enabled" gorm:"column:enabled"`
	Schedule       CronSchedule `json:"schedule" gorm:"embedded"`
	Payload        CronPayload  `json:"payload" gorm:"embedded"`
	State          CronJobState `json:"state" gorm:"embedded"`
	CreatedAtMS    int64        `json:"createdAtMs" gorm:"column:created_at_ms"`
	UpdatedAtMS    int64        `json:"updatedAtMs" gorm:"column:updated_at_ms"`
	DeleteAfterRun bool         `json:"deleteAfterRun" gorm:"column:delete_after_run"`
}

CronJob 表示一个定时任务

func (CronJob) TableName

func (CronJob) TableName() string

TableName 指定 gorm 表名

type CronJobState

type CronJobState struct {
	NextRunAtMS *int64 `json:"nextRunAtMs,omitempty" gorm:"column:next_run_at_ms"`
	LastRunAtMS *int64 `json:"lastRunAtMs,omitempty" gorm:"column:last_run_at_ms"`
	LastStatus  string `json:"lastStatus,omitempty" gorm:"column:last_status;type:varchar(20)"`
	LastError   string `json:"lastError,omitempty" gorm:"column:last_error;type:text"`
}

CronJobState 定义任务的运行状态

type CronPayload

type CronPayload struct {
	Message string `json:"message" gorm:"column:message;type:text"`
}

CronPayload 定义任务触发时携带的信息

type CronSchedule

type CronSchedule struct {
	Kind    string `json:"kind" gorm:"column:kind;type:varchar(10);not null"`   // "at" | "every" | "cron"
	AtMS    *int64 `json:"atMs,omitempty" gorm:"column:at_ms"`                  // Kind=at 时的触发时间戳(毫秒)
	EveryMS *int64 `json:"everyMs,omitempty" gorm:"column:every_ms"`            // Kind=every 时的间隔(毫秒)
	Expr    string `json:"expr,omitempty" gorm:"column:expr;type:varchar(100)"` // Kind=cron 时的 cron 表达式
}

CronSchedule 定义任务的调度方式

type CronService

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

CronService 定时调度服务

func NewCronService

func NewCronService(store Store, onJob JobHandler) *CronService

NewCronService 创建定时调度服务

func (*CronService) AddJob

func (cs *CronService) AddJob(name string, schedule CronSchedule, message string, userID string) (*CronJob, error)

AddJob 添加定时任务

func (*CronService) EnableJob

func (cs *CronService) EnableJob(jobID string, enabled bool) *CronJob

EnableJob 启用/禁用任务

func (*CronService) ListJobs

func (cs *CronService) ListJobs(includeDisabled bool) []CronJob

ListJobs 列出任务

func (*CronService) RemoveJob

func (cs *CronService) RemoveJob(jobID string) bool

RemoveJob 删除任务

func (*CronService) SetMaxJobs

func (cs *CronService) SetMaxJobs(max int)

SetMaxJobs 设置最大任务数量限制,0 表示不限制

func (*CronService) SetMaxJobsPerUser

func (cs *CronService) SetMaxJobsPerUser(max int)

SetMaxJobsPerUser 设置单用户最大任务数量限制,0 表示不限制

func (*CronService) SetOnJob

func (cs *CronService) SetOnJob(handler JobHandler)

SetOnJob 设置任务触发回调

func (*CronService) Start

func (cs *CronService) Start() error

Start 启动调度循环

func (*CronService) Status

func (cs *CronService) Status() map[string]any

Status 返回服务状态

func (*CronService) Stop

func (cs *CronService) Stop()

Stop 停止调度循环

type FileStore

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

func (*FileStore) Delete

func (s *FileStore) Delete(id string) error

Delete 删除任务

func (*FileStore) Get

func (s *FileStore) Get(id string) (*CronJob, error)

Get 根据 ID 获取任务

func (*FileStore) List

func (s *FileStore) List() ([]CronJob, error)

List 列出所有任务

func (*FileStore) Save

func (s *FileStore) Save(job *CronJob) error

Save 保存任务(新增或更新)

type GormStore

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

GormStore 基于 GORM 的数据库存储实现 支持 MySQL、PostgreSQL、SQLite

func (*GormStore) Delete

func (s *GormStore) Delete(id string) error

Delete 删除任务

func (*GormStore) Get

func (s *GormStore) Get(id string) (*CronJob, error)

Get 根据 ID 获取任务

func (*GormStore) List

func (s *GormStore) List() ([]CronJob, error)

List 列出所有任务

func (*GormStore) Save

func (s *GormStore) Save(job *CronJob) error

Save 保存任务(新增或更新)

type JobHandler

type JobHandler func(job *CronJob) (string, error)

JobHandler 任务触发回调

type Store

type Store interface {
	// List 列出所有任务
	List() ([]CronJob, error)

	// Get 根据 ID 获取任务
	Get(id string) (*CronJob, error)

	// Save 保存任务(新增或更新)
	Save(job *CronJob) error

	// Delete 根据 ID 删除任务
	Delete(id string) error
}

Store 定义定时任务的存储接口

func NewFileStore

func NewFileStore(path string) Store

NewFileStore 创建文件存储

func NewGormStore

func NewGormStore(db *gorm.DB) (Store, error)

NewGormStore 创建 GORM 数据库存储 自动建表(如不存在),支持 MySQL、PostgreSQL、SQLite

Jump to

Keyboard shortcuts

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