修复求评价调度时间解析并完善续期日志

This commit is contained in:
Christ committed 2026-07-27 12:08:39 +08:00
1 parent cef2ca92c4
commit f68c8b8add
4 files changed
+43 -10

No files matched your search

+11 -2
View File
@@ -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
}
+11
View File
@@ -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" {
+8 -7
View File
@@ -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
}
+13 -1
View File
@@ -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) {