修复 WS Token 主动轮换与自动化任务恢复
根据服务端过期时间提前轮换 WS Token,并对缺失期限使用保守刷新窗口。 Token 获取瞬时失败时保持账号运行并在过期前持续重试;统一 Cookie、WS 恢复后的自动化唤醒和安全动作游标重试。 修复多实例恢复租约覆盖、规则查询误隔离及人工核对状态落库错误,并等待自动化调度器、续期 watcher 和批量发布恢复任务优雅退出。 补充 SQLite、PostgreSQL 17、生命周期和竞态回归测试。
This commit is contained in:
1 parent
9c742f5e7f
commit
bdc64c68f6
24 files changed
+1080
-110
No files matched your search
@@ -183,6 +183,7 @@ func (s *Server) updateFlatCookieOwnedLocked(ctx context.Context, detail *db.Coo
|
||||
}
|
||||
|
||||
func (s *Server) updateRunningCookie(ctx context.Context, cookieID, value string) {
|
||||
s.wakeCredentialBlockedAutomation(ctx, cookieID)
|
||||
if s.Manager == nil || !s.Store.Cookies.GetStatus(ctx, cookieID) {
|
||||
return
|
||||
}
|
||||
@@ -191,6 +192,15 @@ func (s *Server) updateRunningCookie(ctx context.Context, cookieID, value string
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) wakeCredentialBlockedAutomation(ctx context.Context, cookieID string) {
|
||||
if s == nil || s.Store == nil || s.Store.Automation == nil {
|
||||
return
|
||||
}
|
||||
if err := s.Store.Automation.WakeCredentialBlocked(ctx, cookieID); err != nil && s.Logger != nil {
|
||||
s.Logger.Warn("Cookie 更新后唤醒自动化任务失败", "account", cookieID, "err", err)
|
||||
}
|
||||
}
|
||||
|
||||
func scopedCookieHeader(detail *db.CookieDetail, requestURL string) string {
|
||||
if detail == nil {
|
||||
return ""
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"xianyu-go/internal/automation"
|
||||
"xianyu-go/internal/db"
|
||||
"xianyu-go/internal/xianyu/cookierefresh"
|
||||
xrenew "xianyu-go/internal/xianyu/renew"
|
||||
@@ -418,6 +419,28 @@ func TestUpdateCookie(t *testing.T) {
|
||||
requireCookieSnapshotCleared(t, store, "acc1")
|
||||
}
|
||||
|
||||
func TestUpdateRunningCookieWakesCredentialBlockedAutomationWithoutManager(t *testing.T) {
|
||||
srv, store, cleanup := newTestServer(t)
|
||||
defer cleanup()
|
||||
ctx := context.Background()
|
||||
dueAt := time.Now().UTC().Add(time.Hour).Unix()
|
||||
if err := store.Automation.DeferTask(ctx, db.DeferredAutomationTask{
|
||||
TaskKey: "acc1:credential", CookieID: "acc1", TriggerType: automation.TriggerOrderPaid,
|
||||
TaskJSON: `{}`, DueAt: dueAt, ErrorMessage: "FAIL_SYS_SESSION_EXPIRED",
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
srv.Manager = nil
|
||||
srv.updateRunningCookie(ctx, "acc1", "unb=123; _m_h5_tk=fresh_1;")
|
||||
var got int64
|
||||
if err := store.DB.QueryRowContext(ctx, `SELECT due_at FROM automation_pending_tasks WHERE task_key=?`, "acc1:credential").Scan(&got); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got != 0 {
|
||||
t.Fatalf("Cookie 更新后凭证失败任务 due_at=%d want 0", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateCookieQRLoginEnablesAccount(t *testing.T) {
|
||||
srv, store, cleanup := newTestServer(t)
|
||||
defer cleanup()
|
||||
|
||||
@@ -552,6 +552,16 @@ func (s *Server) RunPublishBatchRecovery(ctx context.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
// StartPublishBatchRecovery 先登记生命周期,再启动恢复循环,避免关闭流程在
|
||||
// goroutine 尚未调度时误判扫描器已经退出。
|
||||
func (s *Server) StartPublishBatchRecovery(ctx context.Context) {
|
||||
s.recoveryWG.Add(1)
|
||||
go func() {
|
||||
defer s.recoveryWG.Done()
|
||||
s.RunPublishBatchRecovery(ctx)
|
||||
}()
|
||||
}
|
||||
|
||||
func (s *Server) recoverPublishBatchesOnce(ctx context.Context) {
|
||||
batches, err := s.Store.PublishBatches.Recoverable(ctx, time.Now().UTC().Unix(), 20)
|
||||
if err != nil {
|
||||
@@ -940,10 +950,8 @@ func (s *Server) publishBatchRow(ctx context.Context, userID int64, client mtop.
|
||||
}
|
||||
return published, nil
|
||||
}()
|
||||
if runtimeCookieChanged && s.Manager != nil {
|
||||
if account, running := s.Manager.GetInstance(row.CookieID); running {
|
||||
account.UpdateCookie(runtimeCookie)
|
||||
}
|
||||
if runtimeCookieChanged {
|
||||
s.updateRunningCookie(ctx, row.CookieID, runtimeCookie)
|
||||
}
|
||||
if err != nil {
|
||||
if ctx.Err() != nil {
|
||||
|
||||
@@ -534,10 +534,8 @@ func (s *Server) refreshSingleOrder(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
credentialUnlock()
|
||||
credentialLocked = false
|
||||
if runtimeCookieChanged && s.Manager != nil {
|
||||
if account, running := s.Manager.GetInstance(cookieID); running {
|
||||
account.UpdateCookie(runtimeCookie)
|
||||
}
|
||||
if runtimeCookieChanged {
|
||||
s.updateRunningCookie(r.Context(), cookieID, runtimeCookie)
|
||||
}
|
||||
if callErr != nil {
|
||||
writeErr(w, http.StatusBadGateway, callErr.Error())
|
||||
@@ -779,10 +777,8 @@ func (s *Server) manualShipOrders(w http.ResponseWriter, r *http.Request) {
|
||||
continue
|
||||
}
|
||||
ok, ret, runtimeCookie, runtimeCookieChanged, err := s.consignWithCurrentCookie(r.Context(), order.CookieID, orderID, sess.UserID)
|
||||
if runtimeCookieChanged && s.Manager != nil {
|
||||
if acc, running := s.Manager.GetInstance(order.CookieID); running {
|
||||
acc.UpdateCookie(runtimeCookie)
|
||||
}
|
||||
if runtimeCookieChanged {
|
||||
s.updateRunningCookie(r.Context(), order.CookieID, runtimeCookie)
|
||||
}
|
||||
if err != nil && !ok {
|
||||
failedCount++
|
||||
|
||||
@@ -265,6 +265,7 @@ func (s *Server) persistQRLoginSuccessFor(ctx context.Context, userID int64, ses
|
||||
if d, err := s.Store.Cookies.GetDetails(ctx, accountID); err == nil {
|
||||
s.refreshAccountProfile(ctx, d)
|
||||
}
|
||||
s.wakeCredentialBlockedAutomation(ctx, accountID)
|
||||
if s.Manager != nil && s.Store.Cookies.GetStatus(ctx, accountID) {
|
||||
if err := s.Manager.Restart(ctx, accountID); err != nil && s.Logger != nil {
|
||||
s.Logger.Warn("扫码登录后重启账号失败", "cookie_id", accountID, "err", err)
|
||||
|
||||
@@ -83,3 +83,21 @@ func TestPublishWorkerTrackingWaitsForCompletion(t *testing.T) {
|
||||
t.Fatal("wait did not return after worker completed")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPublishRecoveryLifecycleStopsBeforeWorkerWait(t *testing.T) {
|
||||
srv, _, cleanup := newTestServer(t)
|
||||
defer cleanup()
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
srv.StartPublishBatchRecovery(ctx)
|
||||
cancel()
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
srv.WaitForBackground()
|
||||
close(done)
|
||||
}()
|
||||
select {
|
||||
case <-done:
|
||||
case <-time.After(2 * time.Second):
|
||||
t.Fatal("批量发布恢复扫描器关闭后没有退出")
|
||||
}
|
||||
}
|
||||
@@ -72,6 +72,7 @@ type Server struct {
|
||||
workerMu sync.Mutex
|
||||
workerCount int
|
||||
workersDone chan struct{}
|
||||
recoveryWG sync.WaitGroup
|
||||
lifecycleMu sync.RWMutex
|
||||
lifecycleCtx context.Context
|
||||
|
||||
@@ -370,10 +371,18 @@ func (s *Server) Run(ctx context.Context) error {
|
||||
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
||||
return err
|
||||
}
|
||||
s.waitForWorkers(10 * time.Second)
|
||||
return nil
|
||||
}
|
||||
|
||||
// WaitForBackground 等待恢复扫描器先退出,再等待其已经登记的批量 worker。
|
||||
func (s *Server) WaitForBackground() {
|
||||
if s == nil {
|
||||
return
|
||||
}
|
||||
s.recoveryWG.Wait()
|
||||
s.waitForWorkers(10 * time.Second)
|
||||
}
|
||||
|
||||
func closedSignal() chan struct{} {
|
||||
done := make(chan struct{})
|
||||
close(done)
|
||||
|
||||
Reference in new issue
Block a user