From 8fddf20c680c4658dba5e43162cb69f8e090c174 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A9=AC=E9=B8=BF=E9=A3=9E?= Date: Tue, 31 Oct 2023 16:50:55 +0800 Subject: [PATCH] fix(scheduledtask): supportCycleType week and month --- pkg/scheduledtask/models/scheduled_tasks.go | 2 +- pkg/scheduledtask/models/timer.go | 64 ++++++++++++++++----- 2 files changed, 52 insertions(+), 14 deletions(-) diff --git a/pkg/scheduledtask/models/scheduled_tasks.go b/pkg/scheduledtask/models/scheduled_tasks.go index 9e2758ecb5..05ea3a9a04 100644 --- a/pkg/scheduledtask/models/scheduled_tasks.go +++ b/pkg/scheduledtask/models/scheduled_tasks.go @@ -141,7 +141,7 @@ func (st *SScheduledTask) getMoreDetails(ctx context.Context, userCred mcclient. case api.ST_TYPE_CYCLE: out.CycleTimer = st.STimer.CycleTimerDetails() } - out.TimerDesc = st.Description(ctx, zone) + out.TimerDesc = st.Description(ctx, st.CreatedAt, zone) // fill label stLabels, err := st.STLabels() if err != nil { diff --git a/pkg/scheduledtask/models/timer.go b/pkg/scheduledtask/models/timer.go index 836c54d5fd..261aa322bd 100644 --- a/pkg/scheduledtask/models/timer.go +++ b/pkg/scheduledtask/models/timer.go @@ -109,6 +109,16 @@ func (st *STimer) Update(now time.Time) { 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, 0, st.CycleNum) + } + case api.TIMER_TYPE_WEEK: + 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, 0, st.CycleNum*7) + } + case api.TIMER_TYPE_MONTH: 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) @@ -188,13 +198,13 @@ func init() { timerDescTable.Set("timerLang", i18n.NewTableEntry().EN("en").CN("cn")) } -func (st *STimer) Description(ctx context.Context, zone *time.Location) string { +func (st *STimer) Description(ctx context.Context, createdAt time.Time, zone *time.Location) string { lang := timerDescTable.Lookup(ctx, TIMERLANG) switch lang { case "en": - return st.descEnglish(zone) + return st.descEnglish(createdAt, zone) case "cn": - return st.descChinese(zone) + return st.descChinese(createdAt, zone) } return "" } @@ -204,14 +214,19 @@ var ( wdsEN = []string{"", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"} ) -func (st *STimer) descChinese(zone *time.Location) string { +func (st *STimer) descChinese(createdAt time.Time, zone *time.Location) string { format := "2006-01-02 15:04:05" var prefix string switch st.Type { case api.TIMER_TYPE_ONCE: return fmt.Sprintf("单次 %s触发", st.StartTime.In(zone).Format(format)) + case api.TIMER_TYPE_HOUR: + prefix = fmt.Sprintf("每%d小时", st.CycleNum) case api.TIMER_TYPE_DAY: prefix = "每天" + if st.CycleNum > 0 { + prefix = fmt.Sprintf("每%d天", st.CycleNum) + } case api.TIMER_TYPE_WEEK: wds := st.GetWeekDays() weekDays := make([]string, len(wds)) @@ -219,6 +234,9 @@ func (st *STimer) descChinese(zone *time.Location) string { weekDays[i] = fmt.Sprintf("星期%s", wdsCN[wds[i]]) } prefix = fmt.Sprintf("每周 【%s】", strings.Join(weekDays, "|")) + if st.CycleNum > 0 { + prefix = fmt.Sprintf("每%d周", st.CycleNum) + } case api.TIMER_TYPE_MONTH: mns := st.GetMonthDays() monthDays := make([]string, len(mns)) @@ -226,6 +244,12 @@ func (st *STimer) descChinese(zone *time.Location) string { monthDays[i] = fmt.Sprintf("%d号", mns[i]) } prefix = fmt.Sprintf("每月 【%s】", strings.Join(monthDays, "|")) + if st.CycleNum > 0 { + prefix = fmt.Sprintf("每%d月", st.CycleNum) + } + } + if st.CycleNum > 0 { + return fmt.Sprintf("%s同步一次,开始时间: 有效时间为%s至%s", prefix, createdAt.In(zone).Format(format), st.EndTime.In(zone).Format(format)) } return fmt.Sprintf("%s %s触发 有效时间为%s至%s", prefix, st.hourMinutesDesc(zone), st.StartTime.In(zone).Format(format), st.EndTime.In(zone).Format(format)) } @@ -236,18 +260,35 @@ func (st *STimer) hourMinutesDesc(zone *time.Location) string { return fmt.Sprintf("%02d:%02d", t.Hour(), t.Minute()) } -func (st *STimer) descEnglish(zone *time.Location) string { +func (st *STimer) descEnglish(createdAt time.Time, zone *time.Location) string { var detail string format := "2006-01-02 15:04:05" switch st.Type { case api.TIMER_TYPE_ONCE: return st.EndTime.In(zone).Format(format) + case api.TIMER_TYPE_HOUR: + detail = fmt.Sprintf("every %d hours", st.CycleNum) case api.TIMER_TYPE_DAY: - detail = fmt.Sprintf("%s every day", st.hourMinutesDesc(zone)) + if st.CycleNum > 0 { + detail = fmt.Sprintf("every %d days", st.CycleNum) + } else { + detail = fmt.Sprintf("%s every day", st.hourMinutesDesc(zone)) + } case api.TIMER_TYPE_WEEK: - detail = st.weekDaysDesc(zone) + if st.CycleNum > 0 { + detail = fmt.Sprintf("every %d weeks", st.CycleNum) + } else { + detail = st.weekDaysDesc(zone) + } case api.TIMER_TYPE_MONTH: - detail = st.monthDaysDesc(zone) + if st.CycleNum > 0 { + detail = fmt.Sprintf("every %d months", st.CycleNum) + } else { + detail = st.monthDaysDesc(zone) + } + } + if st.CycleNum > 0 { + return fmt.Sprintf("Execute %s, start at:%s , from %s to %s", detail, createdAt.In(zone).Format(format), st.StartTime.In(zone).Format(format), st.EndTime.In(zone).Format(format)) } if st.EndTime.IsZero() { return detail @@ -327,18 +368,15 @@ func checkCycleTimerCreateInput(in api.CycleTimerCreateInput) (api.CycleTimerCre 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: - if len(in.WeekDays) == 0 { + if len(in.WeekDays) == 0 && in.CycleNum == 0 { return in, fmt.Errorf("week_days should not be empty") } in.MonthDays = []int{} case api.TIMER_TYPE_MONTH: - if len(in.MonthDays) == 0 { + if len(in.MonthDays) == 0 && in.CycleNum == 0 { return in, fmt.Errorf("month_days should not be empty") } in.WeekDays = []int{}