阶段八:完成账号任务窄repository与凭证门禁收口
This commit is contained in:
1 parent
3638d702d0
commit
0fc7f1b433
5 files changed
+221
-105
No files matched your search
@@ -8257,457 +8257,457 @@
|
||||
},
|
||||
{
|
||||
"file": "internal/automation/account_tasks.go",
|
||||
"line": 61,
|
||||
"line": 64,
|
||||
"kind": "local-variable",
|
||||
"name": "err"
|
||||
},
|
||||
{
|
||||
"file": "internal/automation/account_tasks.go",
|
||||
"line": 61,
|
||||
"line": 64,
|
||||
"kind": "local-variable",
|
||||
"name": "paused"
|
||||
},
|
||||
{
|
||||
"file": "internal/automation/account_tasks.go",
|
||||
"line": 67,
|
||||
"line": 70,
|
||||
"kind": "local-variable",
|
||||
"name": "enabled"
|
||||
},
|
||||
{
|
||||
"file": "internal/automation/account_tasks.go",
|
||||
"line": 67,
|
||||
"line": 70,
|
||||
"kind": "local-variable",
|
||||
"name": "statusErr"
|
||||
},
|
||||
{
|
||||
"file": "internal/automation/account_tasks.go",
|
||||
"line": 76,
|
||||
"line": 79,
|
||||
"kind": "local-variable",
|
||||
"name": "allowed"
|
||||
},
|
||||
{
|
||||
"file": "internal/automation/account_tasks.go",
|
||||
"line": 76,
|
||||
"line": 79,
|
||||
"kind": "local-variable",
|
||||
"name": "err"
|
||||
},
|
||||
{
|
||||
"file": "internal/automation/account_tasks.go",
|
||||
"line": 83,
|
||||
"line": 86,
|
||||
"kind": "local-variable",
|
||||
"name": "err"
|
||||
},
|
||||
{
|
||||
"file": "internal/automation/account_tasks.go",
|
||||
"line": 83,
|
||||
"line": 86,
|
||||
"kind": "local-variable",
|
||||
"name": "settings"
|
||||
},
|
||||
{
|
||||
"file": "internal/automation/account_tasks.go",
|
||||
"line": 90,
|
||||
"line": 93,
|
||||
"kind": "function",
|
||||
"name": "runConfiguredAccountTask"
|
||||
},
|
||||
{
|
||||
"file": "internal/automation/account_tasks.go",
|
||||
"line": 91,
|
||||
"line": 94,
|
||||
"kind": "local-variable",
|
||||
"name": "blocked"
|
||||
},
|
||||
{
|
||||
"file": "internal/automation/account_tasks.go",
|
||||
"line": 91,
|
||||
"line": 94,
|
||||
"kind": "local-variable",
|
||||
"name": "err"
|
||||
},
|
||||
{
|
||||
"file": "internal/automation/account_tasks.go",
|
||||
"line": 98,
|
||||
"line": 101,
|
||||
"kind": "var",
|
||||
"name": "summary"
|
||||
},
|
||||
{
|
||||
"file": "internal/automation/account_tasks.go",
|
||||
"line": 99,
|
||||
"line": 102,
|
||||
"kind": "var",
|
||||
"name": "err"
|
||||
},
|
||||
{
|
||||
"file": "internal/automation/account_tasks.go",
|
||||
"line": 115,
|
||||
"line": 118,
|
||||
"kind": "function",
|
||||
"name": "scanAccountTasks"
|
||||
},
|
||||
{
|
||||
"file": "internal/automation/account_tasks.go",
|
||||
"line": 119,
|
||||
"line": 122,
|
||||
"kind": "local-variable",
|
||||
"name": "err"
|
||||
},
|
||||
{
|
||||
"file": "internal/automation/account_tasks.go",
|
||||
"line": 119,
|
||||
"line": 122,
|
||||
"kind": "local-variable",
|
||||
"name": "settings"
|
||||
},
|
||||
{
|
||||
"file": "internal/automation/account_tasks.go",
|
||||
"line": 124,
|
||||
"line": 127,
|
||||
"kind": "local-variable",
|
||||
"name": "now"
|
||||
},
|
||||
{
|
||||
"file": "internal/automation/account_tasks.go",
|
||||
"line": 125,
|
||||
"line": 128,
|
||||
"kind": "range-variable",
|
||||
"name": "setting"
|
||||
},
|
||||
{
|
||||
"file": "internal/automation/account_tasks.go",
|
||||
"line": 126,
|
||||
"line": 129,
|
||||
"kind": "local-variable",
|
||||
"name": "allowed"
|
||||
},
|
||||
{
|
||||
"file": "internal/automation/account_tasks.go",
|
||||
"line": 126,
|
||||
"line": 129,
|
||||
"kind": "local-variable",
|
||||
"name": "err"
|
||||
},
|
||||
{
|
||||
"file": "internal/automation/account_tasks.go",
|
||||
"line": 130,
|
||||
"line": 133,
|
||||
"kind": "local-variable",
|
||||
"name": "blockErr"
|
||||
},
|
||||
{
|
||||
"file": "internal/automation/account_tasks.go",
|
||||
"line": 130,
|
||||
"line": 133,
|
||||
"kind": "local-variable",
|
||||
"name": "blocked"
|
||||
},
|
||||
{
|
||||
"file": "internal/automation/account_tasks.go",
|
||||
"line": 134,
|
||||
"line": 137,
|
||||
"kind": "local-variable",
|
||||
"name": "err"
|
||||
},
|
||||
{
|
||||
"file": "internal/automation/account_tasks.go",
|
||||
"line": 139,
|
||||
"kind": "local-variable",
|
||||
"name": "blocked"
|
||||
},
|
||||
{
|
||||
"file": "internal/automation/account_tasks.go",
|
||||
"line": 142,
|
||||
"kind": "local-variable",
|
||||
"name": "blocked"
|
||||
},
|
||||
{
|
||||
"file": "internal/automation/account_tasks.go",
|
||||
"line": 145,
|
||||
"kind": "local-variable",
|
||||
"name": "taskErr"
|
||||
},
|
||||
{
|
||||
"file": "internal/automation/account_tasks.go",
|
||||
"line": 153,
|
||||
"line": 156,
|
||||
"kind": "function",
|
||||
"name": "recoverAccountTaskSession"
|
||||
},
|
||||
{
|
||||
"file": "internal/automation/account_tasks.go",
|
||||
"line": 154,
|
||||
"line": 157,
|
||||
"kind": "local-variable",
|
||||
"name": "fingerprint"
|
||||
},
|
||||
{
|
||||
"file": "internal/automation/account_tasks.go",
|
||||
"line": 154,
|
||||
"line": 157,
|
||||
"kind": "local-variable",
|
||||
"name": "fingerprintErr"
|
||||
},
|
||||
{
|
||||
"file": "internal/automation/account_tasks.go",
|
||||
"line": 159,
|
||||
"line": 162,
|
||||
"kind": "local-variable",
|
||||
"name": "recoverer"
|
||||
},
|
||||
{
|
||||
"file": "internal/automation/account_tasks.go",
|
||||
"line": 166,
|
||||
"line": 169,
|
||||
"kind": "function",
|
||||
"name": "accountTaskSessionBlocked"
|
||||
},
|
||||
{
|
||||
"file": "internal/automation/account_tasks.go",
|
||||
"line": 167,
|
||||
"line": 170,
|
||||
"kind": "local-variable",
|
||||
"name": "blockedFingerprint"
|
||||
},
|
||||
{
|
||||
"file": "internal/automation/account_tasks.go",
|
||||
"line": 167,
|
||||
"line": 170,
|
||||
"kind": "local-variable",
|
||||
"name": "ok"
|
||||
},
|
||||
{
|
||||
"file": "internal/automation/account_tasks.go",
|
||||
"line": 171,
|
||||
"line": 174,
|
||||
"kind": "local-variable",
|
||||
"name": "current"
|
||||
},
|
||||
{
|
||||
"file": "internal/automation/account_tasks.go",
|
||||
"line": 171,
|
||||
"line": 174,
|
||||
"kind": "local-variable",
|
||||
"name": "err"
|
||||
},
|
||||
{
|
||||
"file": "internal/automation/account_tasks.go",
|
||||
"line": 182,
|
||||
"line": 185,
|
||||
"kind": "function",
|
||||
"name": "accountCredentialFingerprint"
|
||||
},
|
||||
{
|
||||
"file": "internal/automation/account_tasks.go",
|
||||
"line": 195,
|
||||
"line": 198,
|
||||
"kind": "local-variable",
|
||||
"name": "summary"
|
||||
},
|
||||
{
|
||||
"file": "internal/automation/account_tasks.go",
|
||||
"line": 199,
|
||||
"line": 202,
|
||||
"kind": "local-variable",
|
||||
"name": "cookies"
|
||||
},
|
||||
{
|
||||
"file": "internal/automation/account_tasks.go",
|
||||
"line": 199,
|
||||
"line": 202,
|
||||
"kind": "local-variable",
|
||||
"name": "err"
|
||||
},
|
||||
{
|
||||
"file": "internal/automation/account_tasks.go",
|
||||
"line": 203,
|
||||
"line": 206,
|
||||
"kind": "local-variable",
|
||||
"name": "current"
|
||||
},
|
||||
{
|
||||
"file": "internal/automation/account_tasks.go",
|
||||
"line": 204,
|
||||
"line": 207,
|
||||
"kind": "var",
|
||||
"name": "orders"
|
||||
},
|
||||
{
|
||||
"file": "internal/automation/account_tasks.go",
|
||||
"line": 205,
|
||||
"line": 208,
|
||||
"kind": "local-variable",
|
||||
"name": "page"
|
||||
},
|
||||
{
|
||||
"file": "internal/automation/account_tasks.go",
|
||||
"line": 206,
|
||||
"line": 209,
|
||||
"kind": "local-variable",
|
||||
"name": "err"
|
||||
},
|
||||
{
|
||||
"file": "internal/automation/account_tasks.go",
|
||||
"line": 206,
|
||||
"line": 209,
|
||||
"kind": "local-variable",
|
||||
"name": "pending"
|
||||
},
|
||||
{
|
||||
"file": "internal/automation/account_tasks.go",
|
||||
"line": 217,
|
||||
"line": 220,
|
||||
"kind": "range-variable",
|
||||
"name": "order"
|
||||
},
|
||||
{
|
||||
"file": "internal/automation/account_tasks.go",
|
||||
"line": 218,
|
||||
"line": 221,
|
||||
"kind": "local-variable",
|
||||
"name": "runKey"
|
||||
},
|
||||
{
|
||||
"file": "internal/automation/account_tasks.go",
|
||||
"line": 219,
|
||||
"line": 222,
|
||||
"kind": "local-variable",
|
||||
"name": "claimed"
|
||||
},
|
||||
{
|
||||
"file": "internal/automation/account_tasks.go",
|
||||
"line": 219,
|
||||
"line": 222,
|
||||
"kind": "local-variable",
|
||||
"name": "err"
|
||||
},
|
||||
{
|
||||
"file": "internal/automation/account_tasks.go",
|
||||
"line": 228,
|
||||
"line": 231,
|
||||
"kind": "local-variable",
|
||||
"name": "rateErr"
|
||||
},
|
||||
{
|
||||
"file": "internal/automation/account_tasks.go",
|
||||
"line": 228,
|
||||
"line": 231,
|
||||
"kind": "local-variable",
|
||||
"name": "result"
|
||||
},
|
||||
{
|
||||
"file": "internal/automation/account_tasks.go",
|
||||
"line": 230,
|
||||
"line": 233,
|
||||
"kind": "local-variable",
|
||||
"name": "message"
|
||||
},
|
||||
{
|
||||
"file": "internal/automation/account_tasks.go",
|
||||
"line": 249,
|
||||
"line": 252,
|
||||
"kind": "function",
|
||||
"name": "runAutoPolish"
|
||||
},
|
||||
{
|
||||
"file": "internal/automation/account_tasks.go",
|
||||
"line": 250,
|
||||
"line": 253,
|
||||
"kind": "local-variable",
|
||||
"name": "summary"
|
||||
},
|
||||
{
|
||||
"file": "internal/automation/account_tasks.go",
|
||||
"line": 254,
|
||||
"line": 257,
|
||||
"kind": "local-variable",
|
||||
"name": "date"
|
||||
},
|
||||
{
|
||||
"file": "internal/automation/account_tasks.go",
|
||||
"line": 255,
|
||||
"line": 258,
|
||||
"kind": "local-variable",
|
||||
"name": "runKey"
|
||||
},
|
||||
{
|
||||
"file": "internal/automation/account_tasks.go",
|
||||
"line": 256,
|
||||
"line": 259,
|
||||
"kind": "local-variable",
|
||||
"name": "run"
|
||||
},
|
||||
{
|
||||
"file": "internal/automation/account_tasks.go",
|
||||
"line": 257,
|
||||
"line": 260,
|
||||
"kind": "var",
|
||||
"name": "claimed"
|
||||
},
|
||||
{
|
||||
"file": "internal/automation/account_tasks.go",
|
||||
"line": 258,
|
||||
"line": 261,
|
||||
"kind": "var",
|
||||
"name": "err"
|
||||
},
|
||||
{
|
||||
"file": "internal/automation/account_tasks.go",
|
||||
"line": 271,
|
||||
"line": 274,
|
||||
"kind": "local-variable",
|
||||
"name": "cookies"
|
||||
},
|
||||
{
|
||||
"file": "internal/automation/account_tasks.go",
|
||||
"line": 271,
|
||||
"line": 274,
|
||||
"kind": "local-variable",
|
||||
"name": "err"
|
||||
},
|
||||
{
|
||||
"file": "internal/automation/account_tasks.go",
|
||||
"line": 276,
|
||||
"line": 279,
|
||||
"kind": "local-variable",
|
||||
"name": "err"
|
||||
},
|
||||
{
|
||||
"file": "internal/automation/account_tasks.go",
|
||||
"line": 276,
|
||||
"line": 279,
|
||||
"kind": "local-variable",
|
||||
"name": "items"
|
||||
},
|
||||
{
|
||||
"file": "internal/automation/account_tasks.go",
|
||||
"line": 281,
|
||||
"line": 284,
|
||||
"kind": "local-variable",
|
||||
"name": "current"
|
||||
},
|
||||
{
|
||||
"file": "internal/automation/account_tasks.go",
|
||||
"line": 283,
|
||||
"line": 286,
|
||||
"kind": "var",
|
||||
"name": "lastError"
|
||||
},
|
||||
{
|
||||
"file": "internal/automation/account_tasks.go",
|
||||
"line": 284,
|
||||
"line": 287,
|
||||
"kind": "range-variable",
|
||||
"name": "item"
|
||||
},
|
||||
{
|
||||
"file": "internal/automation/account_tasks.go",
|
||||
"line": 285,
|
||||
"line": 288,
|
||||
"kind": "local-variable",
|
||||
"name": "polishErr"
|
||||
},
|
||||
{
|
||||
"file": "internal/automation/account_tasks.go",
|
||||
"line": 285,
|
||||
"line": 288,
|
||||
"kind": "local-variable",
|
||||
"name": "result"
|
||||
},
|
||||
{
|
||||
"file": "internal/automation/account_tasks.go",
|
||||
"line": 301,
|
||||
"line": 304,
|
||||
"kind": "local-variable",
|
||||
"name": "retryAt"
|
||||
},
|
||||
{
|
||||
"file": "internal/automation/account_tasks.go",
|
||||
"line": 301,
|
||||
"line": 304,
|
||||
"kind": "local-variable",
|
||||
"name": "status"
|
||||
},
|
||||
{
|
||||
"file": "internal/automation/account_tasks.go",
|
||||
"line": 314,
|
||||
"line": 317,
|
||||
"kind": "function",
|
||||
"name": "persistTaskCookies"
|
||||
},
|
||||
{
|
||||
"file": "internal/automation/account_tasks.go",
|
||||
"line": 319,
|
||||
"line": 322,
|
||||
"kind": "local-variable",
|
||||
"name": "err"
|
||||
},
|
||||
{
|
||||
"file": "internal/automation/account_tasks.go",
|
||||
"line": 324,
|
||||
"line": 327,
|
||||
"kind": "local-variable",
|
||||
"name": "ok"
|
||||
},
|
||||
{
|
||||
"file": "internal/automation/account_tasks.go",
|
||||
"line": 324,
|
||||
"line": 327,
|
||||
"kind": "local-variable",
|
||||
"name": "sender"
|
||||
},
|
||||
{
|
||||
"file": "internal/automation/account_tasks.go",
|
||||
"line": 331,
|
||||
"line": 334,
|
||||
"kind": "function",
|
||||
"name": "beijingNow"
|
||||
},
|
||||
{
|
||||
"file": "internal/automation/account_tasks.go",
|
||||
"line": 335,
|
||||
"line": 338,
|
||||
"kind": "function",
|
||||
"name": "polishDue"
|
||||
},
|
||||
{
|
||||
"file": "internal/automation/account_tasks.go",
|
||||
"line": 339,
|
||||
"line": 342,
|
||||
"kind": "local-variable",
|
||||
"name": "err"
|
||||
},
|
||||
{
|
||||
"file": "internal/automation/account_tasks.go",
|
||||
"line": 339,
|
||||
"line": 342,
|
||||
"kind": "local-variable",
|
||||
"name": "target"
|
||||
},
|
||||
|
||||
@@ -611,3 +611,4 @@ npm --prefix frontend run build
|
||||
| 2026-08-15 | 完成阶段 7 第十一个 PR 切片“React AccountList 子模块收口与页面组合边界” | 新增 `useAccountSubmodules`,集中管理账号编辑、长登录、通知绑定、AI 设置和密码登录状态;编辑弹窗仅保留页面组合与二维码/删除职责,子模块请求支持并行加载、取消和账号/代次隔离;新增跨子模块过期响应、密码登录取消信号和路由边界测试;前端类型检查、全量测试和构建门禁通过并合并为一个可回滚提交 | 阶段 7:React feature 依赖门禁与旧页面入口瘦身 |
|
||||
| 2026-08-15 | 完成阶段 7 第十二个 PR 切片“React feature 依赖门禁与旧页面入口瘦身” | 新增 feature 依赖架构测试,禁止生产页面绕过 API 适配层直接使用共享网络客户端或 `fetch`;会话与健康检查纳入 `session/system` feature API,Sidebar 和 App 不再直接访问共享网络层;账号、卡密、订单和规则的旧 `components/*State` 转发入口及其测试迁移/删除,状态测试归属各自 feature;前端类型检查、184 个测试、注释门禁和构建门禁通过 | 阶段 8:DB 与事务治理第一批窄 repository 边界 |
|
||||
| 2026-08-15 | 完成阶段 8 第一个 PR 切片“Chat 窄 repository 接口与服务依赖收口” | 新增 `chat.Repository`,聊天服务只持有会话、消息和账号归属所需的最小持久化接口;完整 `db.Store` 仅在构造适配器时出现,新增 `NewWithRepository` 和内存替身测试,保持聊天消息幂等、会话归属与订阅语义不变;Chat 全量测试、Go vet、lint、注释和全量门禁通过并合并为一个可回滚提交 | 阶段 8:通知与账号任务窄 repository 边界 |
|
||||
| 2026-08-15 | 完成阶段 8 第二个 PR 切片“账号任务窄 repository 与凭证门禁收口” | 新增 `automation.AccountTaskRepository`,账号任务协调器只持有账号启停/暂停、任务设置/租约、运行凭证和 Cookie 更新所需的最小接口;自动化中心保留 Store 装配适配器,Session 指纹阻断、续期恢复、任务幂等和 Cookie 同步语义保持不变;Automation 全量测试、Go vet、lint、注释和全量门禁通过并合并为一个可回滚提交 | 阶段 8:通知与应用服务 repository 边界 |
|
||||
@@ -0,0 +1,112 @@
|
||||
package automation
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"xianyu-go/internal/db"
|
||||
)
|
||||
|
||||
// AccountTaskRepository 定义账号任务协调器需要的最小账号与任务持久化能力。
|
||||
type AccountTaskRepository interface {
|
||||
// IsPaused 返回账号暂停状态及结束时间。
|
||||
IsPaused(ctx context.Context, cookieID string) (bool, int64, error)
|
||||
// Status 返回账号是否启用。
|
||||
Status(ctx context.Context, cookieID string) (bool, error)
|
||||
// GetCookieRuntimeData 返回账号运行所需的 Cookie 与 metadata。
|
||||
GetCookieRuntimeData(ctx context.Context, cookieID string) (db.CookieRuntimeData, error)
|
||||
// GetValue 返回账号当前 Cookie 明文。
|
||||
GetValue(ctx context.Context, cookieID string) (string, error)
|
||||
// UpdateValueExisting 更新已存在账号的 Cookie。
|
||||
UpdateValueExisting(ctx context.Context, cookieID, cookieValue string) error
|
||||
// Get 返回指定账号的任务设置。
|
||||
Get(ctx context.Context, cookieID string) (db.AccountTaskSettings, error)
|
||||
// Enabled 返回启用中的账号任务设置。
|
||||
Enabled(ctx context.Context) ([]db.AccountTaskSettings, error)
|
||||
// ClaimRun 抢占可重复执行的任务运行记录。
|
||||
ClaimRun(ctx context.Context, run db.AccountTaskRun, now int64) (bool, error)
|
||||
// ClaimRunImmediately 抢占人工立即执行的任务运行记录。
|
||||
ClaimRunImmediately(ctx context.Context, run db.AccountTaskRun, now int64) (bool, error)
|
||||
// FinishRun 写入任务运行结果。
|
||||
FinishRun(ctx context.Context, runKey, status string, success, failed int, message string, nextRetryAt int64) error
|
||||
// MarkRateScan 记录自动评价扫描时间。
|
||||
MarkRateScan(ctx context.Context, cookieID string, at int64) error
|
||||
// MarkPolished 记录商品擦亮日期和时间。
|
||||
MarkPolished(ctx context.Context, cookieID, date string, at int64) error
|
||||
}
|
||||
|
||||
// storeAccountTaskRepository 将完整 Store 适配为账号任务窄 repository。
|
||||
type storeAccountTaskRepository struct {
|
||||
// store 保存数据库聚合入口,仅在适配器内部使用。
|
||||
store *db.Store
|
||||
}
|
||||
|
||||
// storeAccountTaskRepositoryCompileCheck 确保 Store 适配器完整实现账号任务窄接口。
|
||||
var _ AccountTaskRepository = storeAccountTaskRepository{}
|
||||
|
||||
// IsPaused 委托账号暂停状态查询。
|
||||
func (r storeAccountTaskRepository) IsPaused(ctx context.Context, cookieID string) (bool, int64, error) {
|
||||
return r.store.Cookies.IsPaused(ctx, cookieID)
|
||||
}
|
||||
|
||||
// Status 委托账号启用状态查询。
|
||||
func (r storeAccountTaskRepository) Status(ctx context.Context, cookieID string) (bool, error) {
|
||||
return r.store.Cookies.Status(ctx, cookieID)
|
||||
}
|
||||
|
||||
// GetCookieRuntimeData 委托账号运行凭证查询。
|
||||
func (r storeAccountTaskRepository) GetCookieRuntimeData(ctx context.Context, cookieID string) (db.CookieRuntimeData, error) {
|
||||
return r.store.Cookies.GetCookieRuntimeData(ctx, cookieID)
|
||||
}
|
||||
|
||||
// GetValue 委托账号 Cookie 查询。
|
||||
func (r storeAccountTaskRepository) GetValue(ctx context.Context, cookieID string) (string, error) {
|
||||
return r.store.Cookies.GetValue(ctx, cookieID)
|
||||
}
|
||||
|
||||
// UpdateValueExisting 委托账号 Cookie 更新。
|
||||
func (r storeAccountTaskRepository) UpdateValueExisting(ctx context.Context, cookieID, cookieValue string) error {
|
||||
return r.store.Cookies.UpdateValueExisting(ctx, cookieID, cookieValue)
|
||||
}
|
||||
|
||||
// Get 委托账号任务设置查询。
|
||||
func (r storeAccountTaskRepository) Get(ctx context.Context, cookieID string) (db.AccountTaskSettings, error) {
|
||||
return r.store.AccountTasks.Get(ctx, cookieID)
|
||||
}
|
||||
|
||||
// Enabled 委托启用任务设置查询。
|
||||
func (r storeAccountTaskRepository) Enabled(ctx context.Context) ([]db.AccountTaskSettings, error) {
|
||||
return r.store.AccountTasks.Enabled(ctx)
|
||||
}
|
||||
|
||||
// ClaimRun 委托任务运行抢占。
|
||||
func (r storeAccountTaskRepository) ClaimRun(ctx context.Context, run db.AccountTaskRun, now int64) (bool, error) {
|
||||
return r.store.AccountTasks.ClaimRun(ctx, run, now)
|
||||
}
|
||||
|
||||
// ClaimRunImmediately 委托人工任务运行抢占。
|
||||
func (r storeAccountTaskRepository) ClaimRunImmediately(ctx context.Context, run db.AccountTaskRun, now int64) (bool, error) {
|
||||
return r.store.AccountTasks.ClaimRunImmediately(ctx, run, now)
|
||||
}
|
||||
|
||||
// FinishRun 委托任务运行结果写入。
|
||||
func (r storeAccountTaskRepository) FinishRun(ctx context.Context, runKey, status string, success, failed int, message string, nextRetryAt int64) error {
|
||||
return r.store.AccountTasks.FinishRun(ctx, runKey, status, success, failed, message, nextRetryAt)
|
||||
}
|
||||
|
||||
// MarkRateScan 委托自动评价扫描时间写入。
|
||||
func (r storeAccountTaskRepository) MarkRateScan(ctx context.Context, cookieID string, at int64) error {
|
||||
return r.store.AccountTasks.MarkRateScan(ctx, cookieID, at)
|
||||
}
|
||||
|
||||
// MarkPolished 委托商品擦亮时间写入。
|
||||
func (r storeAccountTaskRepository) MarkPolished(ctx context.Context, cookieID, date string, at int64) error {
|
||||
return r.store.AccountTasks.MarkPolished(ctx, cookieID, date, at)
|
||||
}
|
||||
|
||||
// newStoreAccountTaskRepository 从完整 Store 构造账号任务窄 repository。
|
||||
func newStoreAccountTaskRepository(store *db.Store) AccountTaskRepository {
|
||||
if store == nil || store.Cookies == nil || store.AccountTasks == nil {
|
||||
return nil
|
||||
}
|
||||
return storeAccountTaskRepository{store: store}
|
||||
}
|
||||
@@ -38,8 +38,8 @@ type AccountTaskSummary struct {
|
||||
// accountTaskCoordinator 负责账号自动评价、商品擦亮和凭证阻断状态。
|
||||
// 它拥有账号任务专用的 Session 指纹状态,Center 只保留兼容调用入口和依赖装配。
|
||||
type accountTaskCoordinator struct {
|
||||
// store 提供账号设置、任务租约、Cookie 和运行结果的持久化能力。
|
||||
store *db.Store
|
||||
// repository 提供账号任务所需的最小持久化能力。
|
||||
repository AccountTaskRepository
|
||||
// client 返回当前生效的账号任务平台客户端。
|
||||
client func() AccountTaskClient
|
||||
// senders 用于把任务响应 Cookie 同步到在线账号运行时。
|
||||
@@ -56,15 +56,18 @@ type accountTaskCoordinator struct {
|
||||
|
||||
// accountAutomationAllowed 判断账号是否未暂停且仍处于启用状态。
|
||||
func (c *accountTaskCoordinator) accountAutomationAllowed(ctx context.Context, accountID string) (bool, error) {
|
||||
if c == nil || c.repository == nil {
|
||||
return false, fmt.Errorf("账号任务存储未初始化")
|
||||
}
|
||||
// paused 表示账号是否被用户临时暂停。
|
||||
// err 保存读取账号暂停状态的错误。
|
||||
paused, _, err := c.store.Cookies.IsPaused(ctx, accountID)
|
||||
paused, _, err := c.repository.IsPaused(ctx, accountID)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("读取账号暂停状态: %w", err)
|
||||
}
|
||||
// enabled 表示账号是否处于启用状态。
|
||||
// statusErr 保存读取账号启用状态的错误。
|
||||
enabled, statusErr := c.store.Cookies.Status(ctx, accountID)
|
||||
enabled, statusErr := c.repository.Status(ctx, accountID)
|
||||
if statusErr != nil {
|
||||
return false, fmt.Errorf("读取账号启用状态: %w", statusErr)
|
||||
}
|
||||
@@ -80,7 +83,7 @@ func (c *accountTaskCoordinator) runAccountTask(ctx context.Context, accountID,
|
||||
if !allowed {
|
||||
return AccountTaskSummary{TaskType: taskType}, fmt.Errorf("账号已停用或暂停,无法执行任务")
|
||||
}
|
||||
settings, err := c.store.AccountTasks.Get(ctx, accountID)
|
||||
settings, err := c.repository.Get(ctx, accountID)
|
||||
if err != nil {
|
||||
return AccountTaskSummary{TaskType: taskType}, err
|
||||
}
|
||||
@@ -113,10 +116,10 @@ func (c *accountTaskCoordinator) runConfiguredAccountTask(ctx context.Context, s
|
||||
}
|
||||
|
||||
func (c *accountTaskCoordinator) scanAccountTasks(ctx context.Context) {
|
||||
if c.client() == nil || c.store.AccountTasks == nil {
|
||||
if c.client() == nil || c.repository == nil {
|
||||
return
|
||||
}
|
||||
settings, err := c.store.AccountTasks.Enabled(ctx)
|
||||
settings, err := c.repository.Enabled(ctx)
|
||||
if err != nil {
|
||||
c.logger.Warn("扫描账号任务配置失败", "err", err)
|
||||
return
|
||||
@@ -181,7 +184,7 @@ func (c *accountTaskCoordinator) accountTaskSessionBlocked(ctx context.Context,
|
||||
|
||||
func (c *accountTaskCoordinator) accountCredentialFingerprint(ctx context.Context, accountID string) (string, error) {
|
||||
// data 是生成自动化 Session 阻断指纹所需的最小 Cookie 与 metadata 输入。
|
||||
data, err := c.store.Cookies.GetCookieRuntimeData(ctx, accountID)
|
||||
data, err := c.repository.GetCookieRuntimeData(ctx, accountID)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@@ -196,7 +199,7 @@ func (c *accountTaskCoordinator) runAutoRate(ctx context.Context, settings db.Ac
|
||||
if c.client() == nil {
|
||||
return summary, fmt.Errorf("自动评价客户端未初始化")
|
||||
}
|
||||
cookies, err := c.store.Cookies.GetValue(ctx, settings.CookieID)
|
||||
cookies, err := c.repository.GetValue(ctx, settings.CookieID)
|
||||
if err != nil {
|
||||
return summary, err
|
||||
}
|
||||
@@ -216,7 +219,7 @@ func (c *accountTaskCoordinator) runAutoRate(ctx context.Context, settings db.Ac
|
||||
summary.Found = len(orders)
|
||||
for _, order := range orders {
|
||||
runKey := "rate:" + settings.CookieID + ":" + order.TradeID
|
||||
claimed, err := c.store.AccountTasks.ClaimRun(ctx, db.AccountTaskRun{RunKey: runKey, CookieID: settings.CookieID,
|
||||
claimed, err := c.repository.ClaimRun(ctx, db.AccountTaskRun{RunKey: runKey, CookieID: settings.CookieID,
|
||||
TaskType: TaskAutoRate, TargetID: order.TradeID}, time.Now().UTC().Unix())
|
||||
if err != nil {
|
||||
return summary, err
|
||||
@@ -231,7 +234,7 @@ func (c *accountTaskCoordinator) runAutoRate(ctx context.Context, settings db.Ac
|
||||
if result != nil && result.Message != "" {
|
||||
message = result.Message
|
||||
}
|
||||
_ = c.store.AccountTasks.FinishRun(ctx, runKey, "failed", 0, 1, message, time.Now().UTC().Add(10*time.Minute).Unix())
|
||||
_ = c.repository.FinishRun(ctx, runKey, "failed", 0, 1, message, time.Now().UTC().Add(10*time.Minute).Unix())
|
||||
summary.Failed++
|
||||
if mtop.IsSessionExpiredErr(rateErr) {
|
||||
return summary, rateErr
|
||||
@@ -239,10 +242,10 @@ func (c *accountTaskCoordinator) runAutoRate(ctx context.Context, settings db.Ac
|
||||
continue
|
||||
}
|
||||
current = c.persistTaskCookies(ctx, settings.CookieID, current, result.UpdatedCookies)
|
||||
_ = c.store.AccountTasks.FinishRun(ctx, runKey, "success", 1, 0, "", 0)
|
||||
_ = c.repository.FinishRun(ctx, runKey, "success", 1, 0, "", 0)
|
||||
summary.Success++
|
||||
}
|
||||
_ = c.store.AccountTasks.MarkRateScan(ctx, settings.CookieID, time.Now().UTC().Unix())
|
||||
_ = c.repository.MarkRateScan(ctx, settings.CookieID, time.Now().UTC().Unix())
|
||||
return summary, nil
|
||||
}
|
||||
|
||||
@@ -257,9 +260,9 @@ func (c *accountTaskCoordinator) runAutoPolish(ctx context.Context, settings db.
|
||||
var claimed bool
|
||||
var err error
|
||||
if manual {
|
||||
claimed, err = c.store.AccountTasks.ClaimRunImmediately(ctx, run, time.Now().UTC().Unix())
|
||||
claimed, err = c.repository.ClaimRunImmediately(ctx, run, time.Now().UTC().Unix())
|
||||
} else {
|
||||
claimed, err = c.store.AccountTasks.ClaimRun(ctx, run, time.Now().UTC().Unix())
|
||||
claimed, err = c.repository.ClaimRun(ctx, run, time.Now().UTC().Unix())
|
||||
}
|
||||
if err != nil || !claimed {
|
||||
if !claimed && err == nil {
|
||||
@@ -268,14 +271,14 @@ func (c *accountTaskCoordinator) runAutoPolish(ctx context.Context, settings db.
|
||||
}
|
||||
return summary, err
|
||||
}
|
||||
cookies, err := c.store.Cookies.GetValue(ctx, settings.CookieID)
|
||||
cookies, err := c.repository.GetValue(ctx, settings.CookieID)
|
||||
if err != nil {
|
||||
_ = c.store.AccountTasks.FinishRun(ctx, runKey, "failed", 0, 1, err.Error(), time.Now().UTC().Add(10*time.Minute).Unix())
|
||||
_ = c.repository.FinishRun(ctx, runKey, "failed", 0, 1, err.Error(), time.Now().UTC().Add(10*time.Minute).Unix())
|
||||
return summary, err
|
||||
}
|
||||
items, err := c.client().FetchAllItems(ctx, cookies, polishItemPageSize, polishItemMaxPages)
|
||||
if err != nil {
|
||||
_ = c.store.AccountTasks.FinishRun(ctx, runKey, "failed", 0, 1, err.Error(), time.Now().UTC().Add(10*time.Minute).Unix())
|
||||
_ = c.repository.FinishRun(ctx, runKey, "failed", 0, 1, err.Error(), time.Now().UTC().Add(10*time.Minute).Unix())
|
||||
return summary, err
|
||||
}
|
||||
current := c.persistTaskCookies(ctx, settings.CookieID, cookies, items.UpdatedCookies)
|
||||
@@ -290,7 +293,7 @@ func (c *accountTaskCoordinator) runAutoPolish(ctx context.Context, settings db.
|
||||
lastError = result.Message
|
||||
}
|
||||
if mtop.IsSessionExpiredErr(polishErr) {
|
||||
_ = c.store.AccountTasks.FinishRun(ctx, runKey, "failed", summary.Success, summary.Failed, lastError, 0)
|
||||
_ = c.repository.FinishRun(ctx, runKey, "failed", summary.Success, summary.Failed, lastError, 0)
|
||||
return summary, polishErr
|
||||
}
|
||||
continue
|
||||
@@ -302,9 +305,9 @@ func (c *accountTaskCoordinator) runAutoPolish(ctx context.Context, settings db.
|
||||
if summary.Failed > 0 {
|
||||
status, retryAt = "failed", time.Now().UTC().Add(10*time.Minute).Unix()
|
||||
} else {
|
||||
_ = c.store.AccountTasks.MarkPolished(ctx, settings.CookieID, date, time.Now().UTC().Unix())
|
||||
_ = c.repository.MarkPolished(ctx, settings.CookieID, date, time.Now().UTC().Unix())
|
||||
}
|
||||
_ = c.store.AccountTasks.FinishRun(ctx, runKey, status, summary.Success, summary.Failed, lastError, retryAt)
|
||||
_ = c.repository.FinishRun(ctx, runKey, status, summary.Success, summary.Failed, lastError, retryAt)
|
||||
if summary.Failed > 0 {
|
||||
return summary, fmt.Errorf("%d 个商品擦亮失败: %s", summary.Failed, lastError)
|
||||
}
|
||||
@@ -316,7 +319,7 @@ func (c *accountTaskCoordinator) persistTaskCookies(ctx context.Context, account
|
||||
if newValue == "" || newValue == oldValue {
|
||||
return oldValue
|
||||
}
|
||||
if err := c.store.Cookies.UpdateValueExisting(ctx, accountID, newValue); err != nil {
|
||||
if err := c.repository.UpdateValueExisting(ctx, accountID, newValue); err != nil {
|
||||
c.logger.Warn("保存账号任务响应 Cookie 失败", "account", accountID, "err", err)
|
||||
return oldValue
|
||||
}
|
||||
|
||||
@@ -149,9 +149,9 @@ func New(store *db.Store, senders SenderProvider, logger *slog.Logger) *Center {
|
||||
},
|
||||
}
|
||||
center.taskRunner = accountTaskCoordinator{
|
||||
store: store,
|
||||
client: func() AccountTaskClient { return center.accountTaskClient },
|
||||
senders: senders,
|
||||
repository: newStoreAccountTaskRepository(store),
|
||||
client: func() AccountTaskClient { return center.accountTaskClient },
|
||||
senders: senders,
|
||||
recoverer: func() CredentialRecoverer {
|
||||
return center.recoverer
|
||||
},
|
||||
|
||||
Reference in new issue
Block a user