feat(scheduledTask): support cycle_hour

This commit is contained in:
马鸿飞
2023-09-27 14:25:28 +08:00
parent 675d453f02
commit 50bd35b558
9 changed files with 46 additions and 5 deletions
@@ -73,9 +73,10 @@ func init() {
}
type CycleTimer struct {
CycleCycleType string `help:"Cycle type for cycle timer" json:"cycle_type" choices:"day|week|month"`
CycleCycleType string `help:"Cycle type for cycle timer" json:"cycle_type" choices:"hour|day|week|month"`
CycleMinute int `help:"Minute of cycle timer" json:"minute"`
CycleHour int `help:"Hour of cycle timer" json:"hour"`
CycleCycleNum int `help:"Cycle count of cycle timer" json:"cycle_num"`
CycleWeekdays []int `help:"Weekdays for cycle timer" json:"weekdays"`
CycleMonthDays []int `help:"Month days for cycle timer" json:"month_days"`
CycleStartTime string `help:"Start time for cycle timer, format:'2006-01-02 15:04:05'" json:"start_time"`
@@ -123,6 +124,7 @@ func init() {
},
CycleTimer: apis.CycleTimerCreateInput{
CycleType: args.CycleCycleType,
CycleNum: args.CycleCycleNum,
Minute: args.CycleMinute,
Hour: args.CycleHour,
WeekDays: args.CycleWeekdays,
+1
View File
@@ -51,6 +51,7 @@ const (
OPERATOR_LT = "lt" // 小于
TIMER_TYPE_ONCE = "once"
TIMER_TYPE_HOUR = "hour"
TIMER_TYPE_DAY = "day"
TIMER_TYPE_WEEK = "week"
TIMER_TYPE_MONTH = "month"
+1
View File
@@ -28,6 +28,7 @@ type CycleTimerCreateInput struct {
// enum: day,week,month
CycleType string `json:"cycle_type"`
CycleHour int `json:"cycle_hour"`
// description: 分(0-59)
// example: 13
Minute int `json:"minute"`
+5 -1
View File
@@ -122,7 +122,7 @@ type TimerCreateInput struct {
type CycleTimerCreateInput struct {
// description: 周期类型
// enum: day,week,month
// enum: hour,day,week,month
CycleType string `json:"cycle_type"`
// description: 分(0-59)
@@ -133,6 +133,10 @@ type CycleTimerCreateInput struct {
// example: 13
Hour int `json:"hour"`
// 频率为小时或天时启用,泛指间隔单位
// example: 2
CycleNum int `json:"cycle_num"`
// description: 每周的周几; 1-7, 1: Monday, 7: Sunday
// example: [1,3,5,7]
WeekDays []int `json:"week_days"`
@@ -39,6 +39,7 @@ const (
ST_ACTIVITY_STATUS_REJECT = "reject" // 拒绝
TIMER_TYPE_ONCE = "once"
TIMER_TYPE_HOUR = "hour"
TIMER_TYPE_DAY = "day"
TIMER_TYPE_WEEK = "week"
TIMER_TYPE_MONTH = "month"
+6
View File
@@ -300,6 +300,12 @@ func checkCycleTimerCreateInput(in api.CycleTimerCreateInput) (api.CycleTimerCre
return in, fmt.Errorf("hour should between 0 and 23")
}
switch in.CycleType {
case api.TIMER_TYPE_HOUR:
if in.CycleHour <= 0 || in.CycleHour >= 24 {
return in, fmt.Errorf("cycle_hour should between 0 and 23")
}
in.WeekDays = []int{}
in.MonthDays = []int{}
case api.TIMER_TYPE_DAY:
in.WeekDays = []int{}
in.MonthDays = []int{}
+2 -2
View File
@@ -355,7 +355,7 @@ func init() {
"加入回收站",
},
sI18nElme{
string(api.ActionSyncUpdate),
string(api.ActionSyncCreate),
"sync_create",
"同步新建",
},
@@ -365,7 +365,7 @@ func init() {
"同步更新",
},
sI18nElme{
string(api.ActionSyncUpdate),
string(api.ActionSyncDelete),
"sync_delete",
"同步删除",
},
@@ -243,6 +243,7 @@ func (st *SScheduledTask) PostCreate(ctx context.Context, userCred mcclient.Toke
Hour: input.CycleTimer.Hour,
StartTime: input.CycleTimer.StartTime,
EndTime: input.CycleTimer.EndTime,
CycleNum: input.CycleTimer.CycleNum,
NextTime: time.Time{},
}
st.SetWeekDays(input.CycleTimer.WeekDays)
+26 -1
View File
@@ -36,6 +36,9 @@ type STimer struct {
Minute int `nullable:"false"`
// 0-23
Hour int `nullable:"false"`
CycleNum int `nullable:"false"`
// 0-7 1 is Monday 0 is unlimited
WeekDays uint8 `nullable:"false"`
// 0-31 0 is unlimited
@@ -98,9 +101,22 @@ func (st *STimer) Update(now time.Time) {
suitTime.Minute(), 0, 0, suitTime.Location())
break
}
case st.CycleNum != 0:
switch st.Type {
case api.TIMER_TYPE_HOUR:
newNextTime = time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), now.Minute(), 0, 0, time.UTC).In(now.Location())
if now.After(newNextTime) {
newNextTime = newNextTime.Add(time.Duration(st.CycleNum) * time.Hour)
}
case api.TIMER_TYPE_DAY:
newNextTime = time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), now.Minute(), 0, 0, time.UTC).In(now.Location())
if now.After(newNextTime) {
newNextTime = newNextTime.AddDate(0, st.CycleNum, 0)
}
}
default:
// day
}
log.Debugf("The final NextTime: %s", newNextTime)
st.NextTime = newNextTime
@@ -304,7 +320,16 @@ func checkCycleTimerCreateInput(in api.CycleTimerCreateInput) (api.CycleTimerCre
return in, fmt.Errorf("hour should between 0 and 23")
}
switch in.CycleType {
case api.TIMER_TYPE_HOUR:
if in.CycleNum <= 0 || in.CycleNum >= 24 {
return in, fmt.Errorf("hour cycle_num should between 1 and 23")
}
in.WeekDays = []int{}
in.MonthDays = []int{}
case api.TIMER_TYPE_DAY:
if in.CycleNum <= 0 {
return in, fmt.Errorf("day cycle_num must upper than 0")
}
in.WeekDays = []int{}
in.MonthDays = []int{}
case api.TIMER_TYPE_WEEK: