// Package stopresume 提供自动停复机请求评估场景在 ctx 中的传递,供各自动入口与停复机服务共用(无业务依赖)。 package stopresume import ( "context" "github.com/break/junhong_cmp_fiber/pkg/constants" ) type sceneKey struct{} // WithScene 声明本次自动停复机评估的触发场景(constants.StopResumeScene*);未声明时按套餐步骤(周期评估)处理。 func WithScene(ctx context.Context, scene string) context.Context { if scene == "" { return ctx } return context.WithValue(ctx, sceneKey{}, scene) } // Scene 返回 ctx 中声明的场景;未声明返回套餐步骤场景。 func Scene(ctx context.Context) string { if scene, ok := ctx.Value(sceneKey{}).(string); ok && scene != "" { return scene } return constants.StopResumeScenePolling } // QueueAware 由停复机服务实现:停复机出口开关开启时请求评估只写共享缓存。 type QueueAware interface { CommandQueueEnabled(ctx context.Context) bool } // RunAfterCommit 在业务事务提交后执行自动停复机请求:开关开启时同步执行(只写共享缓存,不起 goroutine); // 关闭时沿用旧路径在 goroutine 中当场执行(含网关调用与重试,不阻塞业务流程)。 func RunAfterCommit(ctx context.Context, callback any, fn func()) { if aware, ok := callback.(QueueAware); ok && aware.CommandQueueEnabled(ctx) { fn() return } go fn() }