代码拉取完成,页面将自动刷新
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)
//}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。