收口调度器与通知工作器关闭边界
This commit is contained in:
1 parent
c1c065e023
commit
e609f7b39a
8 files changed
+129
-10
No files matched your search
+13
-4
@@ -350,9 +350,14 @@ func runServer(parent context.Context, opts serverOptions) error {
|
||||
// stopCtx、stopCancel 保存stopCtx、stop取消,供当前处理流程使用
|
||||
stopCtx, stopCancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
_ = srv.Stop(stopCtx)
|
||||
stopCancel()
|
||||
automationScheduler.Wait()
|
||||
renewalScheduler.Wait()
|
||||
// err 表示自动化调度器在关闭上下文内未完成退出的错误。
|
||||
if err := automationScheduler.WaitContext(stopCtx); err != nil {
|
||||
logger.Warn("自动化调度器关闭未完成", "err", err)
|
||||
}
|
||||
// err 表示续期调度器在关闭上下文内未完成退出的错误。
|
||||
if err := renewalScheduler.WaitContext(stopCtx); err != nil {
|
||||
logger.Warn("续期调度器关闭未完成", "err", err)
|
||||
}
|
||||
// err 表示账号运行时未能在关闭上下文内完整停止的错误。
|
||||
if err := mgr.StopAllContext(stopCtx); err != nil {
|
||||
logger.Warn("账号运行时关闭未完成", "err", err)
|
||||
@@ -360,7 +365,11 @@ func runServer(parent context.Context, opts serverOptions) error {
|
||||
if bm != nil {
|
||||
_ = bm.Close()
|
||||
}
|
||||
notifier.Wait()
|
||||
// err 表示通知 worker 在关闭上下文内未完成退出的错误。
|
||||
if err := notifier.WaitContext(stopCtx); err != nil {
|
||||
logger.Warn("通知 worker 关闭未完成", "err", err)
|
||||
}
|
||||
stopCancel()
|
||||
return runErr
|
||||
}
|
||||
|
||||
|
||||
@@ -747,3 +747,4 @@ npm --prefix frontend run build
|
||||
| 2026-08-15 | P2 订单复合游标索引与查询计划切片 | 已完成 | SQLite/MySQL/Postgres 新增 `idx_orders_cursor(cookie_id, deleted_at, created_at, order_id)` 复合索引;新增 SQLite `EXPLAIN QUERY PLAN` 回归,确认订单游标查询使用该索引;迁移、订单扫描和中文注释门禁通过。MySQL/Postgres 实际执行计划仍需外部数据库环境验证,P2 批量同步保持“进行中” |
|
||||
| 2026-08-15 | P1 订单刷新持久化后台任务切片 | 已完成 | 新增 SQLite/MySQL/Postgres `order_refresh_jobs` 迁移及应用层 `RefreshJobRepository` Port;订单刷新 HTTP 端点改为 202 + 用户隔离的状态查询,后台 worker 受 Server 生命周期管理,使用 worker token/lease fencing,并提供过期任务恢复扫描;React 客户端改为可取消的状态轮询;新增任务生命周期、跨用户隔离、旧 worker 拒绝写入、成功/失败/取消和 HTTP/前端契约回归;Go 全量测试、架构/中文注释门禁、前端 317 测试、类型检查和生产构建通过。仍待补充取消端点、进度事件、MySQL/Postgres 实际任务迁移回归与统一运维查询,阶段 4/5/8/9 继续保持“进行中” |
|
||||
| 2026-08-15 | P1 订单运行时 Port 基础设施类型收口切片 | 已完成 | `orderRuntimePort` 的完整自动化发货参数改为应用层 `orders.Order`,Cookie 会话持久化参数改为 `orders.PlatformRuntimeData`;`db.Order`/`db.CookieDetail` 转换下沉到 Server 装配适配器,业务服务不再通过运行时 Port 接触数据库模型;补充订单与平台视图边界转换回归,架构门禁、中文注释门禁和订单定向测试通过。订单应用服务实现仍位于 `internal/server`,Port 仍含平台协议接口,其他应用服务和基础设施适配器继续按后续切片迁移,阶段 4/8/9 保持“进行中” |
|
||||
| 2026-08-15 | P1 调度器与通知 worker 关闭上下文切片 | 已完成 | 自动化调度器、续期调度器和通知 outbox worker 新增 `WaitContext`;`cmd/server` 在同一个 10 秒关闭 Context 内等待 HTTP、后台 worker、调度器、账号和通知,并将 `stopCancel` 延后到全部等待结束,避免使用已取消 Context;补充三个 worker 超时/完成回归测试,相关包测试和中文注释门禁通过。Engine 内部任务等待、平台浏览器关闭和生命周期统一清单仍待继续治理,阶段 5 保持“进行中” |
|
||||
@@ -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相关处理。
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -61,6 +61,7 @@ type Notifier struct {
|
||||
httpc *http.Client
|
||||
started atomic.Bool
|
||||
workers sync.WaitGroup
|
||||
done chan struct{}
|
||||
}
|
||||
|
||||
// newOutboundHTTPClient 保存newOutboundHTTPClient,供当前处理流程使用
|
||||
@@ -84,6 +85,7 @@ func NewWithRepository(cookieID string, repository Repository, logger *slog.Logg
|
||||
repository: repository,
|
||||
logger: logger.With("account", cookieID, "subsys", "notify"),
|
||||
httpc: newOutboundHTTPClient(),
|
||||
done: make(chan struct{}),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -97,12 +99,34 @@ func (n *Notifier) Start(ctx context.Context) {
|
||||
n.workers.Add(1)
|
||||
go func() {
|
||||
defer n.workers.Done()
|
||||
defer close(n.done)
|
||||
n.runOutbox(ctx)
|
||||
}()
|
||||
}
|
||||
|
||||
// Wait 等待 outbox worker 随生命周期 context 退出。
|
||||
func (n *Notifier) Wait() { n.workers.Wait() }
|
||||
// Wait 等待 outbox worker 随生命周期 context 退出,并兼容旧调用方。
|
||||
func (n *Notifier) Wait() {
|
||||
_ = n.WaitContext(context.Background())
|
||||
}
|
||||
|
||||
// WaitContext 在 ctx 约束内等待 outbox worker 退出。
|
||||
func (n *Notifier) WaitContext(ctx context.Context) error {
|
||||
if n == nil {
|
||||
return nil
|
||||
}
|
||||
if !n.started.Load() || n.done == nil {
|
||||
return nil
|
||||
}
|
||||
if ctx == nil {
|
||||
ctx = context.Background()
|
||||
}
|
||||
select {
|
||||
case <-n.done:
|
||||
return nil
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
}
|
||||
}
|
||||
|
||||
// NotifyDelivery 发送发货结果通知。
|
||||
// accountID 为 cookie_id。向该账号所有已启用渠道发送发货通知。
|
||||
|
||||
@@ -3,15 +3,36 @@ package notify
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"xianyu-go/internal/db"
|
||||
)
|
||||
|
||||
// TestNotifierWaitContextHonorsDeadline 验证通知 worker 等待受关闭上下文限制。
|
||||
func TestNotifierWaitContextHonorsDeadline(t *testing.T) {
|
||||
// notifier 保存已启动但尚未完成的通知器,以验证等待超时不会永久阻塞。
|
||||
notifier := &Notifier{done: make(chan struct{})}
|
||||
notifier.started.Store(true)
|
||||
// ctx、cancel 保存短时关闭上下文及其释放函数。
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Millisecond)
|
||||
defer cancel()
|
||||
// err 表示尚未完成 worker 在超时上下文下的等待结果。
|
||||
if err := notifier.WaitContext(ctx); !errors.Is(err, context.DeadlineExceeded) {
|
||||
t.Fatalf("WaitContext error=%v, want deadline exceeded", err)
|
||||
}
|
||||
close(notifier.done)
|
||||
// err 表示已完成 worker 的等待结果。
|
||||
if err := notifier.WaitContext(context.Background()); err != nil {
|
||||
t.Fatalf("completed WaitContext error=%v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// newNotifyStore 负责newNotifyStore相关处理。
|
||||
func newNotifyStore(t *testing.T) (*db.Store, func()) {
|
||||
t.Helper()
|
||||
|
||||
@@ -127,10 +127,23 @@ func (s *Scheduler) Run(ctx context.Context) {
|
||||
|
||||
// Wait 等待定时循环和迟到响应 watcher 完成。
|
||||
func (s *Scheduler) Wait() {
|
||||
_ = s.WaitContext(context.Background())
|
||||
}
|
||||
|
||||
// WaitContext 在 ctx 约束内等待定时循环和 watcher 完成。
|
||||
func (s *Scheduler) WaitContext(ctx context.Context) error {
|
||||
if s == nil || s.done == nil {
|
||||
return
|
||||
return nil
|
||||
}
|
||||
if ctx == nil {
|
||||
ctx = context.Background()
|
||||
}
|
||||
select {
|
||||
case <-s.done:
|
||||
return nil
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
}
|
||||
<-s.done
|
||||
}
|
||||
|
||||
// runAPICookieRenewFixed 负责运行API登录凭证RenewFixed相关处理。
|
||||
|
||||
@@ -331,6 +331,24 @@ func TestPendingAPIRenewStopsWithSchedulerContext(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestRenewalSchedulerWaitContextHonorsDeadline 验证续期调度器等待受关闭上下文限制。
|
||||
func TestRenewalSchedulerWaitContextHonorsDeadline(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)
|
||||
}
|
||||
}
|
||||
|
||||
// TestPendingAPIRenewRestartFailureIsFinalFailure 负责TestPendingAPIRenewRestartFailureIsFinalFailure相关处理。
|
||||
func TestPendingAPIRenewRestartFailureIsFinalFailure(t *testing.T) {
|
||||
// store、cleanup 保存store、cleanup,供当前处理流程使用
|
||||
|
||||
Reference in new issue
Block a user