From f68c8b8add10586a29fd57af4a1f3997d04fe0cd Mon Sep 17 00:00:00 2001 From: Christ Date: Mon, 27 Jul 2026 12:08:39 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=B1=82=E8=AF=84=E4=BB=B7?= =?UTF-8?q?=E8=B0=83=E5=BA=A6=E6=97=B6=E9=97=B4=E8=A7=A3=E6=9E=90=E5=B9=B6?= =?UTF-8?q?=E5=AE=8C=E5=96=84=E7=BB=AD=E6=9C=9F=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/automation/scheduler.go | 13 +++++++++++-- internal/automation/scheduler_test.go | 11 +++++++++++ internal/db/automation.go | 15 ++++++++------- internal/renewal/scheduler.go | 14 +++++++++++++- 4 files changed, 43 insertions(+), 10 deletions(-) diff --git a/internal/automation/scheduler.go b/internal/automation/scheduler.go index c9bfe76..f1adab7 100644 --- a/internal/automation/scheduler.go +++ b/internal/automation/scheduler.go @@ -75,7 +75,9 @@ func (s *Scheduler) scan(ctx context.Context) { ChatID: order.ChatID, OrderID: order.OrderID, ItemID: order.ItemID, BuyerID: order.BuyerID, Text: "发货后一段时间未评价", Raw: map[string]any{"source": "scheduler", "rule_id": rule.ID, "order_id": order.OrderID, "attempt": order.ReviewRequestCount + 1}} - _ = s.center.executeRule(ctx, task, rule) + if err := s.center.executeRule(ctx, task, rule); err != nil { + s.center.logger.Warn("求评价计划任务执行失败", "account", order.CookieID, "order_id", order.OrderID, "rule_id", rule.ID, "err", err) + } } } if len(orders) < 200 { @@ -225,7 +227,14 @@ func intFromAny(v any) int { } func parseDBTime(s string) time.Time { - for _, layout := range []string{"2006-01-02 15:04:05", time.RFC3339, "2006-01-02T15:04:05Z07:00"} { + for _, layout := range []string{ + time.RFC3339Nano, + "2006-01-02 15:04:05.999999999Z07:00", // Postgres TEXT(CURRENT_TIMESTAMP) + "2006-01-02 15:04:05.999999999Z07", + "2006-01-02 15:04:05Z07:00", + "2006-01-02 15:04:05Z07", + "2006-01-02 15:04:05", // SQLite/MySQL 历史值;按既有 UTC 约定解释 + } { if t, err := time.ParseInLocation(layout, strings.TrimSpace(s), time.UTC); err == nil { return t } diff --git a/internal/automation/scheduler_test.go b/internal/automation/scheduler_test.go index ee6f5ce..ac9664e 100644 --- a/internal/automation/scheduler_test.go +++ b/internal/automation/scheduler_test.go @@ -133,6 +133,17 @@ func TestReviewRequestRuleDueUsesRepeatIntervalAfterFirstAttempt(t *testing.T) { } } +func TestParseDBTimeAcceptsPostgresTimestampText(t *testing.T) { + got := parseDBTime("2026-07-27 03:36:29.123456+00") + if got.IsZero() { + t.Fatal("Postgres CURRENT_TIMESTAMP 文本不应解析为零值") + } + want := time.Date(2026, 7, 27, 3, 36, 29, 123456000, time.UTC) + if !got.Equal(want) { + t.Fatalf("parseDBTime=%s want %s", got, want) + } +} + // TestFirstNonEmpty 返回首个非空串。 func TestFirstNonEmpty(t *testing.T) { if got := firstNonEmpty("", "", "x", "y"); got != "x" { diff --git a/internal/db/automation.go b/internal/db/automation.go index 8ffaa6d..0995274 100644 --- a/internal/db/automation.go +++ b/internal/db/automation.go @@ -863,22 +863,23 @@ func (a *AutomationRules) MarkOrderEventTime(ctx context.Context, orderID, field default: return fmt.Errorf("不允许更新的订单时间字段: %s", field) } - // 只在字段为空时写入当前时间,已有值则保留(幂等)。用 WHERE 过滤替代 - // CASE WHEN ... THEN CURRENT_TIMESTAMP ELSE field END:后者在 Postgres 上会 - // 因 THEN(timestamptz) 与 ELSE(text) 分支类型不可匹配而报 SQLSTATE 42804。 - // CURRENT_TIMESTAMP 直接赋值给 TEXT 列在三方言下均成立(见 IncrementReviewRequest)。 - _, err := a.DB.ExecContext(ctx, "UPDATE orders SET "+field+"=CURRENT_TIMESTAMP, updated_at=CURRENT_TIMESTAMP WHERE order_id=? AND COALESCE("+field+",'')=''", orderID) + // 事件时间列是跨方言 TEXT。不能直接写 CURRENT_TIMESTAMP:Postgres 会产生 + // "2006-01-02 15:04:05.999999+00",而 MySQL 的无时区文本又取决于会话时区, + // 调度器无法可靠解释。统一由应用写 RFC3339 UTC,已有值仍保留(幂等)。 + now := time.Now().UTC().Format(time.RFC3339Nano) + _, err := a.DB.ExecContext(ctx, "UPDATE orders SET "+field+"=?, updated_at=CURRENT_TIMESTAMP WHERE order_id=? AND COALESCE("+field+",'')=''", now, orderID) return err } // IncrementReviewRequest 记录一次求评价请求。 func (a *AutomationRules) IncrementReviewRequest(ctx context.Context, orderID string) error { + now := time.Now().UTC().Format(time.RFC3339Nano) _, err := a.DB.ExecContext(ctx, ` UPDATE orders SET review_request_count=review_request_count+1, - last_review_request_at=CURRENT_TIMESTAMP, + last_review_request_at=?, updated_at=CURRENT_TIMESTAMP - WHERE order_id=?`, orderID) + WHERE order_id=?`, now, orderID) return err } diff --git a/internal/renewal/scheduler.go b/internal/renewal/scheduler.go index 721470f..1b06ec3 100644 --- a/internal/renewal/scheduler.go +++ b/internal/renewal/scheduler.go @@ -241,6 +241,7 @@ func (s *Scheduler) apiCookieRenewOne(ctx context.Context, batchID string, accou latest, err := s.reloadRenewalAccount(ctx, account) if err != nil { s.addAPILog(ctx, db.RenewalLog{BatchID: batchID, CookieID: account.ID, Status: "failed", ErrorMessage: "重读账号凭证失败: " + err.Error(), RenewMethod: "auto_login_plugin"}) + s.logger.Warn("接口续期任务失败", "account", account.ID, "err", "重读账号凭证失败: "+err.Error()) return } account = latest @@ -254,6 +255,7 @@ func (s *Scheduler) apiCookieRenewOne(ctx context.Context, batchID string, accou message = callErr.Error() } s.addAPILog(ctx, db.RenewalLog{BatchID: batchID, CookieID: account.ID, Status: "failed", ErrorMessage: message, RenewMethod: "auto_login_plugin", DurationMS: time.Since(started).Milliseconds()}) + s.logger.Warn("接口续期任务失败", "account", account.ID, "err", message) return } if res.HasPending() { @@ -272,6 +274,7 @@ func (s *Scheduler) apiCookieRenewOne(ctx context.Context, batchID string, accou } if !s.saveRenewedCookies(ctx, account.ID, res.NewCookies, metadata) { s.addAPILog(ctx, db.RenewalLog{BatchID: batchID, CookieID: account.ID, Status: "failed", ErrorMessage: "保存 Cookie 失败", UpdatedCookieNames: updated, StepDetails: strings.Join(stepDetails, " | "), RenewMethod: res.RenewMethod, DurationMS: time.Since(started).Milliseconds(), RequestCount: res.RequestCount}) + s.logger.Warn("接口续期任务失败", "account", account.ID, "method", res.RenewMethod, "err", "保存 Cookie 失败") return } } @@ -281,17 +284,21 @@ func (s *Scheduler) apiCookieRenewOne(ctx context.Context, batchID string, accou UpdatedCookieNames: updated, StepDetails: strings.Join(stepDetails, " | "), RenewMethod: res.RenewMethod, DurationMS: time.Since(started).Milliseconds(), RequestCount: res.RequestCount, }) - s.logger.Warn("api_cookie_renew 失败,已保存响应头 Cookie", "account", account.ID, "err", callErr) + s.logger.Warn("接口续期任务失败,已保存响应头 Cookie", "account", account.ID, "method", res.RenewMethod, "updated", strings.Join(updated, ","), "err", callErr) return } if res.Success && account.Enabled { + s.logger.Info("接口续期任务成功", "account", account.ID, "method", res.RenewMethod, "updated", strings.Join(updated, ","), "message", res.Message) credentialUnlock() credentialLocked = false if restarter, ok := s.starter.(accountRestarter); ok { + s.logger.Info("接口续期成功,正在重启账号以应用最新登录凭证", "account", account.ID) if err := restarter.Restart(ctx, account.ID); err != nil { s.addAPILog(ctx, db.RenewalLog{BatchID: batchID, CookieID: account.ID, Status: "failed", ErrorMessage: "重建消息连接失败: " + err.Error(), UpdatedCookieNames: updated, StepDetails: strings.Join(stepDetails, " | "), RenewMethod: res.RenewMethod, DurationMS: time.Since(started).Milliseconds(), RequestCount: res.RequestCount}) + s.logger.Warn("接口续期成功,但重启账号失败", "account", account.ID, "err", err) return } + s.logger.Info("接口续期后的账号重启已完成", "account", account.ID) } } status := "failed" @@ -314,6 +321,11 @@ func (s *Scheduler) apiCookieRenewOne(ctx context.Context, batchID string, accou DurationMS: time.Since(started).Milliseconds(), RequestCount: res.RequestCount, }) + if res.Skipped { + s.logger.Info("接口续期任务已跳过", "account", account.ID, "reason", res.SkipReason, "message", res.Message) + } else if !res.Success { + s.logger.Warn("接口续期任务未成功", "account", account.ID, "method", res.RenewMethod, "message", res.Message) + } } func (s *Scheduler) renewAPI(ctx context.Context, cookieStr string, snapshot []cookierefresh.BrowserCookie) (*apirenew.Result, error) {