gf/g/os/gcron/gcron_entry.go

110 lines
2.7 KiB
Go
Raw Normal View History

// Copyright 2018 gf Author(https://gitee.com/johng/gf). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://gitee.com/johng/gf.
package gcron
2018-12-30 11:08:07 +08:00
import (
2019-01-21 22:09:51 +08:00
"gitee.com/johng/gf/g/os/gtimer"
"strconv"
2018-12-30 11:08:07 +08:00
"time"
)
// 定时任务项
type Entry struct {
2019-01-21 22:09:51 +08:00
cron *Cron // 所属定时任务
entry *gtimer.Entry // 定时器任务对象
2019-01-16 22:34:22 +08:00
schedule *cronSchedule // 定时任务配置对象
Name string // 定时任务名称
Job func() // 注册定时任务方法
Time time.Time // 注册时间
2018-12-30 11:08:07 +08:00
}
// 创建定时任务
2019-01-21 22:09:51 +08:00
func (c *Cron) addEntry(pattern string, job func(), singleton bool, times int, name ... string) (*Entry, error) {
2018-12-30 11:08:07 +08:00
schedule, err := newSchedule(pattern)
if err != nil {
return nil, err
}
entry := &Entry {
2019-01-21 22:09:51 +08:00
cron : c,
2018-12-30 11:08:07 +08:00
schedule : schedule,
Job : job,
Time : time.Now(),
}
if len(name) > 0 {
entry.Name = name[0]
2019-01-21 22:09:51 +08:00
} else {
entry.Name = strconv.Itoa(c.idgen.Add(1))
2018-12-30 11:08:07 +08:00
}
2019-01-21 22:09:51 +08:00
entry.entry = gtimer.Add(time.Second, entry.check)
c.entries.Set(entry.Name, entry)
2018-12-30 11:08:07 +08:00
return entry, nil
}
2019-01-16 22:34:22 +08:00
// 是否单例运行
func (entry *Entry) IsSingleton() bool {
2019-01-21 22:09:51 +08:00
return entry.entry.IsSingleton()
2019-01-16 22:34:22 +08:00
}
// 设置单例运行
func (entry *Entry) SetSingleton(enabled bool) {
2019-01-21 22:09:51 +08:00
entry.entry.SetSingleton(true)
2019-01-16 22:34:22 +08:00
}
// 设置任务的运行次数
func (entry *Entry) SetTimes(times int) {
2019-01-21 22:09:51 +08:00
entry.entry.SetTimes(times)
2018-12-30 11:08:07 +08:00
}
// 定时任务状态
func (entry *Entry) Status() int {
2019-01-21 22:09:51 +08:00
return entry.entry.Status()
2018-12-30 11:08:07 +08:00
}
2019-01-16 22:34:22 +08:00
// 设置定时任务状态, 返回设置之前的状态
func (entry *Entry) SetStatus(status int) int {
2019-01-21 22:09:51 +08:00
return entry.entry.SetStatus(status)
2019-01-16 22:34:22 +08:00
}
// 启动定时任务
func (entry *Entry) Start() {
2019-01-21 22:09:51 +08:00
entry.entry.Start()
}
2018-12-30 11:08:07 +08:00
// 停止定时任务
func (entry *Entry) Stop() {
2019-01-21 22:09:51 +08:00
entry.entry.Stop()
}
2019-01-16 22:34:22 +08:00
// 关闭定时任务
func (entry *Entry) Close() {
2019-01-22 13:50:10 +08:00
entry.cron.Remove(entry.Name)
2019-01-21 22:09:51 +08:00
entry.entry.Close()
}
// 定时任务检查执行
func (entry *Entry) check() {
if entry.schedule.meet(time.Now()) {
switch entry.cron.status.Val() {
case STATUS_STOPPED:
return
case STATUS_CLOSED:
2019-01-22 13:50:10 +08:00
entry.cron.Remove(entry.Name)
2019-01-21 22:09:51 +08:00
gtimer.Exit()
case STATUS_READY: fallthrough
case STATUS_RUNNING:
defer func() {
if entry.entry.Status() == STATUS_CLOSED {
entry.cron.Remove(entry.Name)
}
}()
entry.Job()
}
}
2019-01-16 22:34:22 +08:00
}