任务支持超时自动取消

This commit is contained in:
白茶清欢 2022-06-27 11:54:25 +08:00
parent 384a975cc7
commit 1cd0f10b2a

16
task.go
View File

@ -82,8 +82,24 @@ func (d *dispatch) Run(ctx context.Context, cfg *Config) *Result {
var ( var (
taskInstance ITask taskInstance ITask
exist bool exist bool
cancelFunc context.CancelFunc
) )
if nil == ctx {
if cfg.Timeout > 0 {
ctx, cancelFunc = context.WithCancel(context.Background())
} else {
ctx = context.Background()
}
} else {
ctx, cancelFunc = context.WithCancel(ctx)
}
if nil != cancelFunc {
// 带了超时时间
cancelFunc()
}
result := &Result{ result := &Result{
StartTime: time.Now().UnixNano(), StartTime: time.Now().UnixNano(),
FinishTime: 0, FinishTime: 0,