12345678910111213141516171819202122232425262728293031 |
- package cron
- import (
- "app.yhyue.com/moapp/jybase/common"
- "log"
- "strings"
- "time"
- )
- func CrontabByTimer(flag bool, c string, f func()) {
- array := strings.Split(c, ":")
- if len(array) != 2 {
- log.Fatalln("定时任务参数错误!", c)
- }
- if flag {
- go f()
- }
- now := time.Now()
- t := time.Date(now.Year(), now.Month(), now.Day(), common.IntAll(array[0]), common.IntAll(array[1]), 0, 0, time.Local)
- if t.Before(now) {
- t = t.AddDate(0, 0, 1)
- }
- timer := time.NewTimer(t.Sub(now))
- for {
- select {
- case <-timer.C:
- go f()
- timer.Reset(24 * time.Hour)
- }
- }
- }
|