scheduler

package
v1.22.8 Latest Latest
Warning

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

Go to latest
Published: Sep 25, 2024 License: Zlib Imports: 13 Imported by: 1

Documentation

Index

Constants

View Source
const (
	DurationOpt = iota + 1
	GroupNumOpt
	CapacityOpt
	LoggEnableOpt
	StopOpt
	QuitedOpt
	TimeLocationOpt
	UniqueOpt           //唯一检测添加,存在就不添加了
	GenerateOrInitFnOpt //更新或初始化函数,在更新时,有 NotExistNewOpt 时生成函数
	NotExistNewOpt      //更新时,不存在就生成
	MinDurationOpt
)
View Source
const (
	InitState      = State(0)
	RunningState   = State(1)
	PauseState     = State(2)
	CancelState    = State(3)
	CompletedState = State(4)
	TimeoutState   = State(5)
	ErrorState     = State(6)
)
View Source
const (
	QuitExecMustFlag = 1 //退出时,也执行一下
)

Variables

View Source
var ErrJobExist = result.Error(result.ErrParam, "record exist")
View Source
var ErrJobFuncNil = result.Error(result.ErrParam, "job func is nil")
View Source
var ErrJobQuited = result.Error(result.ErrParam, "job quited")
View Source
var ErrJobStop = result.Error(result.ErrParam, "job stop")

Functions

func IsQuited added in v1.21.0

func IsQuited(opts ...option.Option) bool

func IsStop added in v1.21.0

func IsStop(opts ...option.Option) bool

func With added in v1.21.0

func With(s int) []option.Option

func WithCapacity added in v1.20.90

func WithCapacity(capacity int) option.Option

func WithGenerateOrInitFunc added in v1.22.5

func WithGenerateOrInitFunc(fn GenerateOrInitFunc) option.Option

func WithGroupNum added in v1.20.90

func WithGroupNum(num int) option.Option

func WithLoggerEnabled added in v1.20.103

func WithLoggerEnabled() option.Option

func WithNotExistNew added in v1.22.5

func WithNotExistNew() option.Option

func WithQuited added in v1.21.0

func WithQuited() option.Option

func WithStop added in v1.21.0

func WithStop() option.Option

func WithTaskDuration added in v1.20.24

func WithTaskDuration(t time.Duration) option.Option

func WithTaskMinDuration added in v1.22.7

func WithTaskMinDuration(t time.Duration) option.Option

func WithTimeLocation added in v1.21.1

func WithTimeLocation(lc *time.Location) option.Option

func WithUnique added in v1.22.5

func WithUnique() option.Option

Types

type CronTrigger

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

CronTrigger implements Examples: http://www.quartz-scheduler.org/documentation/quartz-2.3.0/tutorials/tutorial-lesson-06.html {秒数} {分钟} {小时} {日期} {月份} {星期} 1 Seconds (0-59) 2 Minutes (0-59) 3 Hours (0-23) 4 Day-of-Month (1-31) 5 Month (0-11) 6 Day-of-Week(1-7) 7 Year (optional field)

每个符号的意义 * 表示所有值; ? 表示未说明的值,即不关心它为何值; - 表示一个指定的范围; , 表示附加一个可能值; / 符号前表示开始时间,符号后表示每次递增的值

一些cron表达式案例 */5 * * * * ? 每隔5秒执行一次 0 */1 * * * ? 每隔1分钟执行一次 0 0 5-15 * * ? 每天5-15点整点触发 0 0/3 * * * ? 每三分钟触发一次 0 0-5 14 * * ? 在每天下午2点到下午2:05期间的每1分钟触发 0 0/5 14 * * ? 在每天下午2点到下午2:55期间的每5分钟触发 0 0/5 14,18 * * ? 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发 0 0/30 9-17 * * ? 朝九晚五工作时间内每半小时 0 0 10,14,16 * * ? 每天上午10点,下午2点,4点 0 0 12 ? * WED 表示每个星期三中午12点 0 0 17 ? * TUES,THUR,SAT 每周二、四、六下午五点 0 10,44 14 ? 3 WED 每年三月的星期三的下午2:10和2:44触发 0 15 10 ? * MON-FRI 周一至周五的上午10:15触发 0 0 23 L * ? 每月最后一天23点执行一次 0 15 10 L * ? 每月最后一日的上午10:15触发 0 15 10 ? * 6L 每月的最后一个星期五上午10:15触发 0 15 10 * * ? 2005 2005年的每天上午10:15触发 0 15 10 ? * 6L 2002-2005 2002年至2005年的每月的最后一个星期五上午10:15触发 0 15 10 ? * 6#3 每月的第三个星期五上午10:15触发

"30 * * * * ?" 每半分钟触发任务 "30 10 * * * ?" 每小时的10分30秒触发任务 "30 10 1 * * ?" 每天1点10分30秒触发任务 "30 10 1 20 * ?" 每月20号1点10分30秒触发任务 "30 10 1 20 10 ? *" 每年10月20号1点10分30秒触发任务 "30 10 1 20 10 ? 2011" 2011年10月20号1点10分30秒触发任务 "30 10 1 ? 10 * 2011" 2011年10月每天1点10分30秒触发任务 "30 10 1 ? 10 SUN 2011" 2011年10月每周日1点10分30秒触发任务 "15,30,45 * * * * ?" 每15秒,30秒,45秒时触发任务 "15-45 * * * * ?" 15到45秒内,每秒都触发任务 "15/5 * * * * ?" 每分钟的每15秒开始触发,每隔5秒触发一次 "15-30/5 * * * * ?" 每分钟的15秒到30秒之间开始触发,每隔5秒触发一次 "0 0/3 * * * ?" 每小时的第0分0秒开始,每三分钟触发一次 "0 15 10 ? * MON-FRI" 星期一到星期五的10点15分0秒触发任务 "0 15 10 L * ?" 每个月最后一天的10点15分0秒触发任务 "0 15 10 LW * ?" 每个月最后一个工作日的10点15分0秒触发任务 "0 15 10 ? * 5L" 每个月最后一个星期四的10点15分0秒触发任务 "0 15 10 ? * 5#3" 每个月第三周的星期四的10点15分0秒触发任务 五:表达式生成器 有很多的cron表达式在线生成器,这里给大家推荐几款 http://www.pdtools.net/tools/becron.jsp http://cron.qqe2.com/

func NewCronTrigger

func NewCronTrigger(expr string) (*CronTrigger, error)

NewCronTrigger returns a new CronTrigger using the UTC location.

func (*CronTrigger) Description

func (ct *CronTrigger) Description() string

Description returns the description of the trigger.

func (*CronTrigger) NextFireTime

func (ct *CronTrigger) NextFireTime(prev int64) (int64, error)

NextFireTime returns the next time at which the CronTrigger is scheduled to fire.

func (*CronTrigger) Type added in v1.3.58

func (ct *CronTrigger) Type() string

func (*CronTrigger) WithLocation

func (ct *CronTrigger) WithLocation(lc *time.Location) *CronTrigger

type Delay added in v1.21.0

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

func NewDelay added in v1.21.0

func NewDelay() *Delay

func (*Delay) Add added in v1.21.0

func (d *Delay) Add(job *Job)

func (*Delay) Check added in v1.21.0

func (d *Delay) Check(pool *Pool)

func (*Delay) Exec added in v1.21.0

func (d *Delay) Exec(opts ...option.Option)

type GenerateOrInitFunc added in v1.22.5

type GenerateOrInitFunc func(group int, tk Task) (Task, Trigger, bool)

type HandlerFunc added in v1.8.5

type HandlerFunc func(job *Job, opts ...option.Option) (interface{}, error)

func (HandlerFunc) ProcessJob added in v1.8.5

func (fn HandlerFunc) ProcessJob(job *Job, opts ...option.Option) (interface{}, error)

type IdleFunc added in v1.8.5

type IdleFunc func(num int32)

IdleFunc idle working number

func (IdleFunc) Idle added in v1.8.5

func (fn IdleFunc) Idle(num int32)

type IdleHandler added in v1.8.5

type IdleHandler interface {
	Idle(num int32)
}

type Job

type Job struct {
	Id       string
	Type     string
	TraceId  string
	State    State
	MustFlag int32         // QuitExecMustFlag quit时,要不要被执行,默认不执行
	Delay    time.Duration // 延后执行,卡住
	Timeout  time.Duration // 超时
	// contains filtered or unexported fields
}

func BuildJob added in v1.0.59

func BuildJob(jobType, jobId string, timeout time.Duration) Job

func NewEmptyJob added in v1.4.0

func NewEmptyJob(fn HandlerFunc) *Job

func NewJob

func NewJob(jobType, jobId string, timeout time.Duration) *Job

func NewShellJob

func NewShellJob(cmd string) *Job

func (*Job) Cancel

func (job *Job) Cancel()

func (*Job) CheckTimeout

func (job *Job) CheckTimeout() bool

func (*Job) Execute

func (job *Job) Execute(opts ...option.Option) (interface{}, error)

func (*Job) GetCount added in v1.22.0

func (job *Job) GetCount() int64

func (*Job) GetExecAt added in v1.22.0

func (job *Job) GetExecAt() int64

func (*Job) GetId added in v1.5.8

func (job *Job) GetId() string

func (*Job) GetPayload added in v1.8.5

func (job *Job) GetPayload() any

func (*Job) GetStartAt added in v1.22.0

func (job *Job) GetStartAt() int64

func (*Job) GetState

func (job *Job) GetState() State

func (*Job) GetType added in v1.3.58

func (job *Job) GetType() string

func (*Job) IsQuit

func (job *Job) IsQuit() bool

func (*Job) NewResult added in v1.21.0

func (job *Job) NewResult(data any, err error) *Result

func (*Job) Pause

func (job *Job) Pause()

func (*Job) SetHandler added in v1.8.5

func (job *Job) SetHandler(fn ProcessHandler) *Job

func (*Job) SetPayload added in v1.20.91

func (job *Job) SetPayload(payload any)

func (*Job) SetResultHandler added in v1.8.5

func (job *Job) SetResultHandler(fn ResultHandler) *Job

func (*Job) Start

func (job *Job) Start()

func (*Job) WithDelay added in v1.9.8

func (job *Job) WithDelay(delay time.Duration) *Job

func (*Job) WithHandlerFunc added in v1.8.5

func (job *Job) WithHandlerFunc(fn func(job *Job, opts ...option.Option) (interface{}, error)) *Job

func (*Job) WithMustFlag added in v1.22.0

func (job *Job) WithMustFlag(m int32) *Job

func (*Job) WithPayload added in v1.8.5

func (job *Job) WithPayload(payload interface{}) *Job

func (*Job) WithResultFunc added in v1.8.5

func (job *Job) WithResultFunc(fn func(res *Result, opts ...option.Option)) *Job

func (*Job) WithTimeOut

func (job *Job) WithTimeOut(timeout time.Duration) *Job

func (*Job) WithTraceId added in v1.20.68

func (job *Job) WithTraceId(traceId string) *Job

type OnceTrigger

type OnceTrigger struct {
	Delay time.Duration
	// contains filtered or unexported fields
}

OnceTrigger implements the quartz.Trigger interface. This type of Trigger can only be fired once and will expire immediately.

func NewOnceTrigger

func NewOnceTrigger(delay time.Duration) *OnceTrigger

NewOnceTrigger returns a new OnceTrigger with the given delay time.

func (*OnceTrigger) Description

func (tr *OnceTrigger) Description() string

Description returns the description of the trigger.

func (*OnceTrigger) NextFireTime

func (tr *OnceTrigger) NextFireTime(prev int64) (int64, error)

NextFireTime returns the next time at which the OnceTrigger is scheduled to fire. Sets expired to true afterwards.

func (*OnceTrigger) Type added in v1.3.58

func (tr *OnceTrigger) Type() string

type Pool

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

func NewPool

func NewPool(numWorkers, jobQueueLen int) *Pool

func (*Pool) AddJob added in v1.3.59

func (p *Pool) AddJob(job *Job)

func (*Pool) HasWorking added in v1.2.5

func (p *Pool) HasWorking() bool

func (*Pool) Release

func (p *Pool) Release()

func (*Pool) Run added in v1.3.59

func (p *Pool) Run()

func (*Pool) SetIdleHandler added in v1.8.5

func (p *Pool) SetIdleHandler(fn IdleHandler) *Pool

func (*Pool) WithIdleFunc added in v1.8.5

func (p *Pool) WithIdleFunc(fn func(num int32)) *Pool

type ProcessHandler added in v1.21.0

type ProcessHandler interface {
	ProcessJob(job *Job, opts ...option.Option) (interface{}, error)
}

type Result added in v1.8.5

type Result struct {
	TraceId string `json:"traceId"`
	Type    string `json:"type"`
	Id      string `json:"id"`
	Err     error  `json:"err,omitempty"`
	Data    any    `json:"data,omitempty"` //处理结果
}

type ResultFunc added in v1.8.5

type ResultFunc func(res *Result, opts ...option.Option)

func (ResultFunc) ResultJob added in v1.8.5

func (fn ResultFunc) ResultJob(res *Result, opts ...option.Option)

type ResultHandler added in v1.8.5

type ResultHandler interface {
	ResultJob(res *Result, opts ...option.Option)
}

type Scheduler added in v1.5.8

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

func Instance

func Instance(opts ...option.Option) *Scheduler

func NewScheduler added in v1.5.8

func NewScheduler(groupNum uint32, opts ...option.Option) *Scheduler

func NewSchedulerCap added in v1.5.8

func NewSchedulerCap(groupNum uint32, capacity int, opts ...option.Option) *Scheduler

func (*Scheduler) AddGTask added in v1.20.90

func (s *Scheduler) AddGTask(group uint32, task Task, opts ...option.Option) error

func (*Scheduler) AddTask added in v1.5.8

func (s *Scheduler) AddTask(task Task, opts ...option.Option) error

func (*Scheduler) AddTriggerGTask added in v1.20.90

func (s *Scheduler) AddTriggerGTask(group uint32, task Task, trigger Trigger, opts ...option.Option) error

func (*Scheduler) AddTriggerTask added in v1.5.8

func (s *Scheduler) AddTriggerTask(task Task, trigger Trigger, opts ...option.Option) error

func (*Scheduler) Foreach added in v1.21.1

func (s *Scheduler) Foreach(update func(group int, task Task, time time.Duration, now, trigger int64) bool)

func (*Scheduler) ForeachTask added in v1.21.1

func (s *Scheduler) ForeachTask(group uint32, update func(group int, task Task, time time.Duration, now, trigger int64) bool)

func (*Scheduler) GroupIndex added in v1.20.90

func (s *Scheduler) GroupIndex(key string) uint32

func (*Scheduler) HasGTask added in v1.20.90

func (s *Scheduler) HasGTask(group uint32, taskId string) bool

func (*Scheduler) HasTask added in v1.5.8

func (s *Scheduler) HasTask(taskId string) bool

func (*Scheduler) Index added in v1.20.90

func (s *Scheduler) Index(id uint32) uint32

func (*Scheduler) RemoveGTask added in v1.20.90

func (s *Scheduler) RemoveGTask(group uint32, taskId string)

func (*Scheduler) RemoveTask added in v1.5.8

func (s *Scheduler) RemoveTask(taskId string)

func (*Scheduler) Run added in v1.5.8

func (s *Scheduler) Run()

func (*Scheduler) StopGTask added in v1.20.90

func (s *Scheduler) StopGTask(group uint32, taskId string)

func (*Scheduler) StopTask added in v1.5.8

func (s *Scheduler) StopTask(taskId string)

func (*Scheduler) UpdateGTask added in v1.20.90

func (s *Scheduler) UpdateGTask(group uint32, taskId string, opts ...option.Option) bool

func (*Scheduler) UpdateTask added in v1.9.10

func (s *Scheduler) UpdateTask(taskId string, opts ...option.Option) bool

type SimpleTrigger

type SimpleTrigger struct {
	Interval time.Duration
}

SimpleTrigger implements the quartz.Trigger interface; uses a fixed interval.

func NewSimpleTrigger

func NewSimpleTrigger(interval time.Duration) *SimpleTrigger

NewSimpleTrigger returns a new SimpleTrigger using the given interval.

func (*SimpleTrigger) Description

func (st *SimpleTrigger) Description() string

Description returns the description of the trigger.

func (*SimpleTrigger) NextFireTime

func (st *SimpleTrigger) NextFireTime(prev int64) (int64, error)

NextFireTime returns the next time at which the SimpleTrigger is scheduled to fire.

func (*SimpleTrigger) Type added in v1.3.58

func (st *SimpleTrigger) Type() string

type State

type State int

func (State) Is added in v1.20.101

func (s State) Is(ss State) bool

func (State) IsQuit

func (s State) IsQuit() bool

func (State) String

func (s State) String() string

type Task

type Task interface {
	GetId() string
	GetType() string
	GetState() State
	GetStartAt() int64 // new object time
	GetExecAt() int64  // 最近一次执行时间
	GetCount() int64   // 执行次数
	GetPayload() any
	Execute(opts ...option.Option) (any, error)
	Cancel()
	Pause()
	Start()
}

type Trigger

type Trigger interface {
	Type() string

	// NextFireTime returns the next time at which the Trigger is scheduled to fire.
	NextFireTime(prev int64) (int64, error)

	// Description returns the description of the Trigger.
	Description() string
}

Trigger represents the mechanism by which Jobs are scheduled.

Jump to

Keyboard shortcuts

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