123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- package p
- import (
- util "app.yhyue.com/moapp/jybase/common"
- "strings"
- "time"
- "github.com/donnie4w/go-logger/logger"
- )
- var VarTimeTask = &timeTask{}
- type timeTask struct {
- }
- /* 定时任务,一轮一轮执行,不停歇
- * @param msg 打印输出的信息
- * @param start 开始时间
- * @param end 结束时间
- * @param duration 定时任务时间间隔
- * @param runRightNow 是否立即执行
- * @param dayFirst 是否是今天第一次执行
- * @param nowRunHours 这些小时,任务不执行
- * @param f 需要执行的方法逻辑
- */
- func (t *timeTask) RunInTimeLoop(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.Minute
- 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 开始时间
- * @param end 结束时间
- * @param f 需要执行的方法逻辑
- */
- func (t *timeTask) RunInTimeSection(msg, start, end string, duration int64, f func(dayFirst bool)) {
- now := time.Now()
- //开始时间
- startArr := strings.Split(start, ":")
- starttime := time.Date(now.Year(), now.Month(), now.Day(), util.IntAll(startArr[0]), util.IntAll(startArr[1]), 0, 0, time.Local)
- var sub time.Duration
- if now.Before(starttime) {
- sub = starttime.Sub(time.Now())
- } else {
- if t.IsInTimeSection(start, end) {
- if duration > 0 {
- go t.RunInTimeLoop(msg, start, end, duration, true, false, nil, func() {
- f(false)
- })
- } else {
- go f(false)
- }
- } else {
- logger.Info(msg, "当前时间不在", start, end, "范围内,不执行")
- }
- sub = starttime.AddDate(0, 0, 1).Sub(time.Now())
- }
- logger.Info("start runInTimeSection after", sub)
- timer := time.NewTimer(sub)
- for {
- select {
- case <-timer.C:
- timer.Reset(time.Hour * 24)
- if t.IsInTimeSection(start, end) {
- if duration > 0 {
- go t.RunInTimeLoop(msg, start, end, duration, true, false, nil, func() {
- f(true)
- })
- } else {
- go f(true)
- }
- } else {
- logger.Info(msg, "当前时间不在", start, end, "范围内,不执行")
- }
- }
- }
- }
- /* 是否在时间区间内
- * @param start 开始时间
- * @param end 结束时间
- */
- func (t *timeTask) IsInTimeSection(start, end string) bool {
- if start == "" || end == "" {
- return true
- }
- now := time.Now()
- start_time := strings.Split(start, ":")
- starttime := time.Date(now.Year(), now.Month(), now.Day(), util.IntAll(start_time[0]), util.IntAll(start_time[1]), 0, 0, time.Local)
- //
- end_time := strings.Split(end, ":")
- endtime := time.Date(now.Year(), now.Month(), now.Day(), util.IntAll(end_time[0]), util.IntAll(end_time[1]), 0, 0, time.Local)
- 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)
- startDate := endDate.AddDate(0, 0, -6)
- 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)
- endDate := startDate.AddDate(0, 1, -1)
- return startDate.Unix(), endDate.Unix()
- }
|