Kaynağa Gözat

新增方法

WH01243 10 ay önce
ebeveyn
işleme
9776873fed
1 değiştirilmiş dosya ile 27 ekleme ve 3 silme
  1. 27 3
      p/timetask.go

+ 27 - 3
p/timetask.go

@@ -49,6 +49,32 @@ func (t *timeTask) RunInTimeLoop(msg, start, end string, duration int64, runRigh
 	})
 }
 
+// 秒级定时任务
+func (t *timeTask) RunInSecondTimeLoop(msg, start, end string, duration int64, runRightNow, dayFirst bool, nowRunHours []int, f func()) {
+	if !dayFirst && !t.IsInTimeSection(start, end) {
+		logger.Info(msg, "当前时间不在", start, end, "范围内,不执行")
+		return
+	}
+	if runRightNow {
+		isRun := true
+		hour := time.Now().Hour()
+		for _, v := range nowRunHours {
+			if v == hour {
+				isRun = false
+				break
+			}
+		}
+		if isRun {
+			f()
+		}
+	}
+	d := time.Duration(duration) * time.Second
+	logger.Info(msg, "start runInTimeLoop after", d)
+	time.AfterFunc(d, func() {
+		t.RunInTimeLoop(msg, start, end, duration, true, false, nowRunHours, f)
+	})
+}
+
 /* 定时任务,在时间区间内一轮一轮执行
  * @param msg 打印输出的信息
  * @param start 开始时间
@@ -115,7 +141,6 @@ func (t *timeTask) IsInTimeSection(start, end string) bool {
 	return now.After(starttime) && now.Before(endtime)
 }
 
-//
 func (t *timeTask) Week() (int64, int64) {
 	endDate := time.Now().AddDate(0, 0, -WeekNum[time.Now().Weekday().String()])
 	endDate = time.Date(endDate.Year(), endDate.Month(), endDate.Day(), 0, 0, 0, 0, time.Local)
@@ -123,7 +148,6 @@ func (t *timeTask) Week() (int64, int64) {
 	return startDate.Unix(), endDate.Unix()
 }
 
-//
 func (t *timeTask) Month() (int64, int64) {
 	now := time.Now().AddDate(0, -1, 0)
 	startDate := time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, time.Local)
@@ -131,7 +155,7 @@ func (t *timeTask) Month() (int64, int64) {
 	return startDate.Unix(), endDate.Unix()
 }
 
-//每小时整点执行任务
+// 每小时整点执行任务
 func (t *timeTask) RunEveryHour(runRightNow bool, f func()) {
 	if runRightNow {
 		go f()