Skip to content

Commit

Permalink
调整:tasks.RunNow立即执行的逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
steden committed Feb 28, 2024
1 parent a28799d commit 152d824
Showing 1 changed file with 5 additions and 16 deletions.
21 changes: 5 additions & 16 deletions run.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,11 @@ func Run(taskName string, interval time.Duration, taskFn func(context *TaskConte
// interval:任务运行的间隔时间
// taskFn:要运行的任务
func RunNow(taskName string, interval time.Duration, taskFn func(context *TaskContext), ctx context.Context) {
// 不立即运行,则先休眠interval时间
if interval <= 0 {
panic("interval参数,必须大于0")
}

go func() {
taskInterval := runTask(taskName, interval, taskFn)
for {
select {
case <-ctx.Done():
return
case <-time.After(taskInterval):
taskInterval = runTask(taskName, interval, taskFn)
}
}
}()
// 立即执行
taskFn(&TaskContext{
sw: stopwatch.StartNew(),
})
Run(taskName,interval,taskFn,ctx)
}

// 运行任务
Expand Down

0 comments on commit 152d824

Please sign in to comment.