收口调度器与通知工作器关闭边界

This commit is contained in:
Christ committed 2026-08-15 16:46:16 +08:00
1 parent c1c065e023
commit e609f7b39a
8 files changed
+129 -10

No files matched your search

+16 -2
View File
@@ -55,11 +55,25 @@ func (s *Scheduler) Run(ctx context.Context) {
})
}
// Wait 负责Wait相关处理。
// Wait 等待调度器完成,并兼容不需要超时的旧调用方。
func (s *Scheduler) Wait() {
_ = s.WaitContext(context.Background())
}
// WaitContext 在 ctx 约束内等待调度器完成,避免关闭流程无限阻塞。
func (s *Scheduler) WaitContext(ctx context.Context) error {
if s != nil && s.done != nil {
<-s.done
if ctx == nil {
ctx = context.Background()
}
select {
case <-s.done:
return nil
case <-ctx.Done():
return ctx.Err()
}
}
return nil
}
// scan 负责scan相关处理。
+19
View File
@@ -2,6 +2,7 @@ package automation
import (
"context"
"errors"
"fmt"
"path/filepath"
"testing"
@@ -390,3 +391,21 @@ func TestAutomationSchedulerWaitsForShutdown(t *testing.T) {
t.Fatal("自动化调度器关闭后没有退出")
}
}
// TestAutomationSchedulerWaitContextHonorsDeadline 验证自动化调度器等待受关闭上下文限制。
func TestAutomationSchedulerWaitContextHonorsDeadline(t *testing.T) {
// scheduler 保存尚未完成的调度器,以验证等待超时不会永久阻塞。
scheduler := &Scheduler{done: make(chan struct{})}
// ctx、cancel 保存短时关闭上下文及其释放函数。
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Millisecond)
defer cancel()
// err 表示尚未完成调度器在超时上下文下的等待结果。
if err := scheduler.WaitContext(ctx); !errors.Is(err, context.DeadlineExceeded) {
t.Fatalf("WaitContext error=%v, want deadline exceeded", err)
}
close(scheduler.done)
// err 表示已完成调度器的等待结果。
if err := scheduler.WaitContext(context.Background()); err != nil {
t.Fatalf("completed WaitContext error=%v", err)
}
}