|
|
@@ -8,7 +8,7 @@ import (
|
|
|
)
|
|
|
|
|
|
// TaskFunc 任务执行函数
|
|
|
-type TaskFunc func(string, time.Time)
|
|
|
+type TaskFunc func(ctx context.Context, name string, now time.Time)
|
|
|
|
|
|
// 任务主体
|
|
|
type Task struct {
|
|
|
@@ -45,7 +45,7 @@ type Tasker interface {
|
|
|
// hour 当前时间小时
|
|
|
// minute 当前时间分钟
|
|
|
// second 当前时间秒
|
|
|
- Do(now time.Time, hour, minute, second int)
|
|
|
+ Do(ctx context.Context, now time.Time, hour, minute, second int)
|
|
|
}
|
|
|
|
|
|
// NewTask 新任务
|
|
|
@@ -58,7 +58,7 @@ func (t *Task) Add(tx Tasker) {
|
|
|
t.l.PushBack(tx)
|
|
|
}
|
|
|
|
|
|
-func (t *Task) do(nsec int64) {
|
|
|
+func (t *Task) do(ctx context.Context, nsec int64) {
|
|
|
var (
|
|
|
interval uint64
|
|
|
hour, minute, second int
|
|
|
@@ -77,11 +77,11 @@ func (t *Task) do(nsec int64) {
|
|
|
interval = v.Interval()
|
|
|
if interval > 1 {
|
|
|
if nsec%int64(interval) == 0 {
|
|
|
- go v.Do(now, h, m, s)
|
|
|
+ go v.Do(ctx, now, h, m, s)
|
|
|
}
|
|
|
continue
|
|
|
} else if interval == 1 { // 每秒任务
|
|
|
- go v.Do(now, h, m, s)
|
|
|
+ go v.Do(ctx, now, h, m, s)
|
|
|
continue
|
|
|
}
|
|
|
hour, minute, second = v.Timing()
|
|
|
@@ -89,10 +89,10 @@ func (t *Task) do(nsec int64) {
|
|
|
if minute == m {
|
|
|
switch hour {
|
|
|
case h:
|
|
|
- go v.Do(now, h, m, s)
|
|
|
+ go v.Do(ctx, now, h, m, s)
|
|
|
case -1:
|
|
|
// // 整点任务
|
|
|
- go v.Do(now, h, m, s)
|
|
|
+ go v.Do(ctx, now, h, m, s)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -111,10 +111,10 @@ func (t *Task) Start(ctx context.Context) {
|
|
|
for {
|
|
|
select {
|
|
|
case <-ctx.Done():
|
|
|
- log.Println("Exit")
|
|
|
+ log.Println("Task Exit", ctx.Err())
|
|
|
return
|
|
|
case <-ts.C:
|
|
|
- t.do(nsecs)
|
|
|
+ t.do(ctx, nsecs)
|
|
|
nsecs++
|
|
|
}
|
|
|
}
|
|
|
@@ -155,8 +155,8 @@ func (t *TaskElement) TaskHandle() TaskFunc {
|
|
|
return t.Handle
|
|
|
}
|
|
|
|
|
|
-func (t *TaskElement) Do(now time.Time, hour, minute, second int) {
|
|
|
- t.Handle(t.TaskName, now)
|
|
|
+func (t *TaskElement) Do(ctx context.Context, now time.Time, hour, minute, second int) {
|
|
|
+ t.Handle(ctx, t.TaskName, now)
|
|
|
}
|
|
|
|
|
|
// ***************** TaskElement end *****************
|