1 Star 4 Fork 0

magicianlyx/GoTask

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
schedule.go 3.77 KB
一键复制 编辑 原始数据 按行查看 历史
magicianlyx 提交于 2021-11-20 21:44 +08:00 . -
package GoTask
import (
"fmt"
jsoniter "github.com/json-iterator/go"
"time"
)
// 任务调度接口
type ISchedule interface {
GetAddTime() time.Time // 获取添加时间
AddExecuteCount() // 添加执行次数 任务执行完毕后调用
GetExecuteCount() int // 获取执行次数
Expression() (nt time.Time, isValid bool) // 表达式
ToString() string
}
// 指定时长循环调度
type SpecSchedule struct {
addTime time.Time // 添加时间
count int // 执行次数
spec time.Duration // 执行时间间距
}
func NewSpecSchedule(addTime time.Time, count int, spec time.Duration) *SpecSchedule {
return &SpecSchedule{
addTime: addTime,
count: count,
spec: spec,
}
}
func (p *SpecSchedule) GetAddTime() time.Time {
return p.addTime
}
func (p *SpecSchedule) AddExecuteCount() {
p.count += 1
}
func (p *SpecSchedule) GetExecuteCount() int {
return p.count
}
func (p *SpecSchedule) Expression() (nt time.Time, isValid bool) {
return p.addTime.Add(time.Duration(p.count+1) * p.spec), true
}
func (p *SpecSchedule) ToString() string {
s, _ := jsoniter.MarshalToString(map[string]interface{}{
"spec": fmt.Sprintf("%.6fs", p.spec.Seconds()),
})
return s
}
// 指定时长指定次数调度器
type SpecTimeSchedule struct {
addTime time.Time // 添加时间
count int // 执行次数
spec time.Duration // 执行时间间距
limit int
}
func NewSpecTimeSchedule(spec time.Duration, limit int) *SpecTimeSchedule {
return &SpecTimeSchedule{
addTime: time.Now(),
spec: spec,
limit: limit,
}
}
func (p *SpecTimeSchedule) GetAddTime() time.Time {
return p.addTime
}
func (p *SpecTimeSchedule) AddExecuteCount() {
p.count += 1
}
func (p *SpecTimeSchedule) GetExecuteCount() int {
return p.count
}
func (p *SpecTimeSchedule) Expression() (nt time.Time, isValid bool) {
if p.count >= p.limit {
return time.Time{}, false
}
nt = p.addTime.Add(time.Duration(p.count+1) * p.spec)
isValid = true
return
}
func (p *SpecTimeSchedule) ToString() string {
s, _ := jsoniter.MarshalToString(map[string]interface{}{
"limit": p.limit,
"spec": fmt.Sprintf("%.6fs", p.spec.Seconds()),
})
return s
}
//
//// 指定时间点调度器
//type PlanSchedule struct {
// tList []time.Time // 计划任务时间点 有次数限制
//}
//
//func NewPlanSchedule(tList []time.Time) *PlanSchedule {
// return &PlanSchedule{tList: tList}
//}
//
//func (p *PlanSchedule) GetAddTime() time.Time {
//
// return p.addTime
//}
//
//func (p *PlanSchedule) AddExecuteCount() {
// p.count += 1
//}
//
//func (p *PlanSchedule) GetExecuteCount() int {
// return p.count
//}
//
//func (p *PlanSchedule) Expression(t *TaskInfo) (nt time.Time, isValid bool) {
// if p.tList == nil || len(p.tList) == 0 || t.Count >= len(p.tList) {
// return time.Time{}, false
// } else {
// return p.tList[t.Count], true
// }
//}
//
//func (p *PlanSchedule) ToString() string {
// s, _ := jsoniter.MarshalToString(map[string]interface{}{
// "tList": p.tList,
// })
// return s
//}
//
//// 每日指定时刻调度器
//type EveryDaySchedule struct {
// hour int
// minute int
// second int
// mSecond int
//}
//
//func NewEveryDaySchedule(hour, minute, second, mSecond int) *EveryDaySchedule {
// return &EveryDaySchedule{hour, minute, second, mSecond}
//}
//
//func (e *EveryDaySchedule) Expression(t *TaskInfo) (nt time.Time, isValid bool) {
// now := time.Now()
// nt = time.Date(now.Year(), now.Month(), now.Day(), e.hour, e.minute, e.second, e.mSecond, time.Local)
// if nt.UnixNano() < now.UnixNano() {
// return nt.AddDate(0, 0, 1), true
// }
// return nt, true
//}
//
//func (e *EveryDaySchedule) ToString() string {
// return fmt.Sprintf("every day %d h %d m %d s %d ms", e.hour, e.minute, e.second, e.mSecond)
//}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/magicianlyx/GoTask.git
git@gitee.com:magicianlyx/GoTask.git
magicianlyx
GoTask
GoTask
v1.0.15

搜索帮助