diff --git a/task.go b/task.go index f9b0e28..f7c34e8 100644 --- a/task.go +++ b/task.go @@ -82,8 +82,24 @@ func (d *dispatch) Run(ctx context.Context, cfg *Config) *Result { var ( taskInstance ITask 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{ StartTime: time.Now().UnixNano(), FinishTime: 0,