fix: 修复待机控制器的 context 使用和忙循环问题
- interval: 添加 Sleep 避免默认分支的忙循环(CPU 100%) - cron: 使用 context.Background() 确保定时任务完整执行,不受外部取消影响 - wait_card: 使用 context.Background() 确保读卡器监听完整执行 这些修复确保了关键操作能够完整运行,同时避免 CPU 资源浪费。
This commit is contained in:
@@ -65,7 +65,8 @@ func Cron(rootRules []cronrange.Rule, cron string, play func(c context.Context)
|
||||
case r := <-a:
|
||||
if r {
|
||||
if ok := m.TryLock(); ok {
|
||||
ctx, cc := context.WithCancel(context.TODO())
|
||||
// 使用独立 context 确保任务完整执行,不受外部取消影响
|
||||
ctx, cc := context.WithCancel(context.Background())
|
||||
cancel = cc
|
||||
waitGroup.Add(1)
|
||||
go func() {
|
||||
|
||||
@@ -28,6 +28,8 @@ func Interval(interval int64, play func(c context.Context) error) func(c context
|
||||
case <-c.Done():
|
||||
return nil
|
||||
default:
|
||||
// 避免忙循环,短暂休眠
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user