间隔执行
time包中有个Ticker可以用来实现简单的定时任务。
ticker := time.NewTicker(5 * time.Second)for _ = range ticker.C {fmt.Println(time.Now())}
Ticker会在每隔一段时间执行,比如上面的例子中,每隔5秒打印一下当前时间。
但是,这显然满足不了我们的需求,我们还需要在某个固定时刻才开始。
对于固定时刻T,计算T和当前时间的时间差,然后sleep到T,然后用Ticker开始定时任务,每隔时间间隔D执行一次任务。
以何种形式提供固定时刻T以及时间间隔D
time包预定义了一些格式
const (ANSIC = "Mon Jan _2 15:04:05 2006"UnixDate = "Mon Jan _2 15:04:05 MST 2006"RubyDate = "Mon Jan 02 15:04:05 -0700 2006"RFC822 = "02 Jan 06 15:04 MST"RFC822Z = "02 Jan 06 15:04 -0700" // RFC822 with numeric zoneRFC850 = "Monday, 02-Jan-06 15:04:05 MST"RFC1123 = "Mon, 02 Jan 2006 15:04:05 MST"RFC1123Z = "Mon, 02 Jan 2006 15:04:05 -0700" // RFC1123 with numeric zoneRFC3339 = "2006-01-02T15:04:05Z07:00"RFC3339Nano = "2006-01-02T15:04:05.999999999Z07:00"Kitchen = "3:04PM"// Handy time stamps.Stamp = "Jan _2 15:04:05"StampMilli = "Jan _2 15:04:05.000"StampMicro = "Jan _2 15:04:05.000000"StampNano = "Jan _2 15:04:05.000000000")
这里吐槽一下,一开始不明白为什么是2006-01-02 15:04:05这个点,一直在想那个点到底发生了什么重大的事。后来才知道,按照美式的时间格式,也就是上面的ANSIC,月,日,时,分,秒,年,排列起来正好是123456。这么设计是为了方便记忆...<br />当然也可以自定义时间格式,比如2006-01-02 15:04:05。一般在我们的定时任务中,我们常用的是时分秒这样的时刻,所以T的表达式定义为:
15:04:05
D的格式
D的格式比较简单,可以使用300ms, -1.5h, 2h45m这种格式。
代码
// sched to start scheduler job at start time by interval duration.func sched(jobFunc interface{}, start, interval string, jobArgs ...interface{}) {jobValue := reflect.ValueOf(jobFunc)if jobValue.Kind() != reflect.Func {log.Panic("only function can be schedule.")}if len(jobArgs) != jobValue.Type().NumIn() {log.Panic("The number of args valid.")}// Get job function args.in := make([]reflect.Value, len(jobArgs))for i, arg := range jobArgs {in[i] = reflect.ValueOf(arg)}// Get interval d.d, err := time.ParseDuration(interval)if err != nil {log.Panic(err)}location, err := time.LoadLocation("Asia/Shanghai")if err != nil {log.Panic(err)}t, err := time.ParseInLocation("15:04:05", start, location)if err != nil {log.Panic(err)}now := time.Now()// Start time.t = time.Date(now.Year(), now.Month(), now.Day(), t.Hour(), t.Minute(), t.Second(), 0, location)if now.After(t) {t = t.Add((now.Sub(t)/d + 1) * d)}time.Sleep(t.Sub(now))go jobValue.Call(in)ticker := time.NewTicker(d)go func() {for _ = range ticker.C {go jobValue.Call(in)}}()}

