From a2a22ad3e328848d2594965b159b254845c8008d Mon Sep 17 00:00:00 2001 From: Christ Date: Sat, 15 Aug 2026 03:20:36 +0800 Subject: [PATCH] =?UTF-8?q?=E9=98=B6=E6=AE=B5=E5=85=AB=EF=BC=9A=E5=AE=8C?= =?UTF-8?q?=E6=88=90=E5=8F=91=E5=B8=83worker=E7=A7=9F=E7=BA=A6=E7=8A=B6?= =?UTF-8?q?=E6=80=81repository=E6=94=B6=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/architecture/refactoring-master-plan.md | 1 + .../server/item_publish_batch_handlers.go | 74 +++++------ internal/server/item_publish_repository.go | 119 ++++++++++++++++++ internal/server/item_publish_service.go | 5 + 4 files changed, 162 insertions(+), 37 deletions(-) diff --git a/docs/architecture/refactoring-master-plan.md b/docs/architecture/refactoring-master-plan.md index 5304398..c6f562a 100644 --- a/docs/architecture/refactoring-master-plan.md +++ b/docs/architecture/refactoring-master-plan.md @@ -616,3 +616,4 @@ npm --prefix frontend run build | 2026-08-15 | 完成阶段 8 第四个 PR 切片“分析应用服务只读 repository 边界” | 新增分析服务窄 repository,统一封装 Dashboard 固定统计、订单分析聚合、有效订单分页、卡密库存读取和数据库方言;应用服务不再持有完整 `Server` 或直接访问 `Store.Analytics/Cards`,查询阶段错误、金额表达式和响应聚合语义保持不变;Server 全量测试、Go vet、lint、注释和全量门禁通过并合并为一个可回滚提交 | 阶段 8:订单与发布应用服务 repository 边界 | | 2026-08-15 | 完成阶段 8 第五个 PR 切片“订单应用服务 repository 与事务边界” | 新增 `orderRepository`,订单应用服务的订单/商品读写、用户归属、事务、凭证锁、续期 Cookie 和远端缺失清理均通过窄接口;平台 MTOP、运行时 Cookie 更新、自动化发货和通知编排仍由 Server 负责;订单列表、详情、导入、手动发货、单笔/批量刷新语义保持不变;Server 全量测试、Go vet、lint、注释和全量门禁通过并合并为一个可回滚提交 | 阶段 8:发布应用服务 repository 与 worker 事务边界 | | 2026-08-15 | 完成阶段 8 第六个 PR 切片“发布应用服务 repository 边界” | 新增 `itemPublishRepository`,单商品发布、类目推荐、预检批次创建、批次查询/取消/删除/失败重试及凭证 Cookie 持久化不再直接访问完整 `db.Store`;批量 worker 的租约状态机和后台生命周期暂保持原有 Server 接线,下一片整体迁移;单商品/批量发布测试、Go vet、lint、注释和全量门禁通过并合并为一个可回滚提交 | 阶段 8:发布 worker 租约状态机 repository 边界 | +| 2026-08-15 | 完成阶段 8 第七个 PR 切片“发布 worker 租约状态机 repository 边界” | 发布恢复扫描、批次租约续期、明细抢占、远端发布检查点、结果落库、取消/中断收口和过期上传清理统一通过 `itemPublishRepository`;worker 不再直接访问 `PublishBatches`、Cookies 或 Items,平台 MTOP、自动化规则和生命周期等待语义保持不变;Server 全量测试、Server race、Go vet、lint、注释和全量门禁通过并合并为一个可回滚提交 | 阶段 8:账号登录应用服务 repository 边界 | diff --git a/internal/server/item_publish_batch_handlers.go b/internal/server/item_publish_batch_handlers.go index b19bdbf..50ad3fd 100644 --- a/internal/server/item_publish_batch_handlers.go +++ b/internal/server/item_publish_batch_handlers.go @@ -326,7 +326,7 @@ func (s *Server) previewItemPublishBatch(w http.ResponseWriter, r *http.Request) writeErr(w, http.StatusBadRequest, "表格中没有有效数据行") return } - if err := s.Store.PublishBatches.Create(r.Context(), &db.ItemPublishBatch{ + if err := s.itemPublishRepositoryForServer().CreateBatch(r.Context(), &db.ItemPublishBatch{ ID: batchID, UserID: sess.UserID, DefaultCookieID: defaultCookieID, @@ -339,7 +339,7 @@ func (s *Server) previewItemPublishBatch(w http.ResponseWriter, r *http.Request) return } keepUpload = true - _ = s.Store.PublishBatches.Recount(r.Context(), batchID) + _ = s.itemPublishRepositoryForServer().RecountBatch(r.Context(), batchID) // 预检响应保留逐行错误,客户端据此决定是否允许启动发布。 // preview_id 是后续启动、轮询和放弃预检的稳定标识。 // total、valid 和 invalid 继续使用旧统计口径。 @@ -512,30 +512,30 @@ func (s *Server) StartPublishBatchRecovery(ctx context.Context) { } func (s *Server) recoverPublishBatchesOnce(ctx context.Context) { - batches, err := s.Store.PublishBatches.Recoverable(ctx, time.Now().UTC().Unix(), 20) + batches, err := s.itemPublishRepositoryForServer().RecoverableBatches(ctx, time.Now().UTC().Unix(), 20) if err != nil { s.Logger.Warn("扫描可恢复批量发布任务失败", "err", err) return } for _, batch := range batches { if batch.Status == "canceling" { - _, _ = s.Store.PublishBatches.FinalizeExpiredCancellation(ctx, batch.ID, time.Now().UTC().Unix()) + _, _ = s.itemPublishRepositoryForServer().FinalizeExpiredCancellation(ctx, batch.ID, time.Now().UTC().Unix()) continue } workerToken := randomHex(16) - claimed, claimErr := s.Store.PublishBatches.ClaimBatch(ctx, batch.ID, workerToken, time.Now().UTC().Add(publishBatchLease).Unix()) + claimed, claimErr := s.itemPublishRepositoryForServer().ClaimBatch(ctx, batch.ID, workerToken, time.Now().UTC().Add(publishBatchLease).Unix()) if claimErr != nil || !claimed { continue } - if err := s.Store.PublishBatches.ResetInterrupted(ctx, batch.ID); err != nil { + if err := s.itemPublishRepositoryForServer().ResetInterrupted(ctx, batch.ID); err != nil { s.failClaimedPublishBatch(batch.ID, workerToken) continue } - _ = s.Store.PublishBatches.Recount(ctx, batch.ID) - pending, pendingErr := s.Store.PublishBatches.PendingRows(ctx, batch.ID, false) + _ = s.itemPublishRepositoryForServer().RecountBatch(ctx, batch.ID) + pending, pendingErr := s.itemPublishRepositoryForServer().PendingRows(ctx, batch.ID, false) if pendingErr != nil || len(pending) == 0 { if pendingErr == nil { - _, _, _ = s.Store.PublishBatches.FinalizeBatch(ctx, batch.ID, workerToken) + _, _, _ = s.itemPublishRepositoryForServer().FinalizeBatch(ctx, batch.ID, workerToken) } else { s.failClaimedPublishBatch(batch.ID, workerToken) } @@ -548,7 +548,7 @@ func (s *Server) recoverPublishBatchesOnce(ctx context.Context) { func (s *Server) failClaimedPublishBatch(batchID, workerToken string) { ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() - if released, err := s.Store.PublishBatches.FailClaimedBatch(ctx, batchID, workerToken); err != nil { + if released, err := s.itemPublishRepositoryForServer().FailClaimedBatch(ctx, batchID, workerToken); err != nil { s.Logger.Warn("释放异常批量发布任务失败", "batch", batchID, "err", err) } else if !released { s.Logger.Debug("批量发布任务租约已转移,无需释放", "batch", batchID) @@ -558,12 +558,12 @@ func (s *Server) failClaimedPublishBatch(batchID, workerToken string) { func (s *Server) downloadItemPublishBatchResult(w http.ResponseWriter, r *http.Request) { sess := auth.SessionFromContext(r.Context()) batchID := chi.URLParam(r, "batch_id") - batch, err := s.Store.PublishBatches.Get(r.Context(), sess.UserID, batchID) + batch, err := s.itemPublishRepositoryForServer().GetBatch(r.Context(), sess.UserID, batchID) if err != nil { writeErr(w, http.StatusNotFound, "批量任务不存在") return } - rows, err := s.Store.PublishBatches.Rows(r.Context(), batch.ID) + rows, err := s.itemPublishRepositoryForServer().ListBatchRows(r.Context(), batch.ID) if err != nil { writeErr(w, http.StatusInternalServerError, "读取任务明细失败") return @@ -604,7 +604,7 @@ func safeCSVCell(value string) string { } func (s *Server) runItemPublishBatch(ctx context.Context, userID int64, batchID, workerToken string, failedOnly bool) { - rows, err := s.Store.PublishBatches.PendingRows(ctx, batchID, failedOnly) + rows, err := s.itemPublishRepositoryForServer().PendingRows(ctx, batchID, failedOnly) if err != nil { if s.Logger != nil { s.Logger.Warn("读取批量发布行失败", "batch", batchID, "err", err) @@ -621,21 +621,21 @@ func (s *Server) runItemPublishBatch(ctx context.Context, userID int64, batchID, return } wctx, cancel := publishStatusContext(ctx) - renewed, renewErr := s.Store.PublishBatches.RenewBatchLease(wctx, batchID, workerToken, time.Now().UTC().Add(publishBatchLease).Unix()) + renewed, renewErr := s.itemPublishRepositoryForServer().RenewBatchLease(wctx, batchID, workerToken, time.Now().UTC().Add(publishBatchLease).Unix()) cancel() if renewErr != nil || !renewed { s.finishInterruptedPublishBatch(ctx, userID, batchID, workerToken) return } wctx, cancel = publishStatusContext(ctx) - batch, err := s.Store.PublishBatches.Get(wctx, userID, batchID) + batch, err := s.itemPublishRepositoryForServer().GetBatch(wctx, userID, batchID) cancel() if err != nil || batch.Status != "running" || batch.WorkerToken != workerToken { s.finishInterruptedPublishBatch(ctx, userID, batchID, workerToken) return } wctx, cancel = publishStatusContext(ctx) - claimed, claimErr := s.Store.PublishBatches.ClaimRow(wctx, row.ID, workerToken) + claimed, claimErr := s.itemPublishRepositoryForServer().ClaimRow(wctx, row.ID, workerToken) cancel() if claimErr != nil { s.finishInterruptedPublishBatch(ctx, userID, batchID, workerToken) @@ -647,11 +647,11 @@ func (s *Server) runItemPublishBatch(ctx context.Context, userID int64, batchID, if rowErr := s.publishBatchRow(ctx, userID, client, row, workerToken); rowErr != nil { sessionExpired := mtop.IsSessionExpiredErr(rowErr) statusCtx, statusCancel := context.WithTimeout(context.Background(), 5*time.Second) - status, _ := s.Store.PublishBatches.BatchStatus(statusCtx, batchID) + status, _ := s.itemPublishRepositoryForServer().BatchStatus(statusCtx, batchID) statusCancel() message, failureKind := publishBatchFailure(rowErr, status) wctx, cancel := publishStatusContext(ctx) - marked, markErr := s.Store.PublishBatches.MarkClaimedRowFailed(wctx, row.ID, workerToken, message, failureKind) + marked, markErr := s.itemPublishRepositoryForServer().MarkClaimedRowFailed(wctx, row.ID, workerToken, message, failureKind) cancel() if markErr != nil || !marked { if s.Logger != nil { @@ -665,7 +665,7 @@ func (s *Server) runItemPublishBatch(ctx context.Context, userID int64, batchID, } } wctx, cancel = publishStatusContext(ctx) - if err := s.Store.PublishBatches.Recount(wctx, batchID); err != nil && s.Logger != nil { + if err := s.itemPublishRepositoryForServer().RecountBatch(wctx, batchID); err != nil && s.Logger != nil { s.Logger.Warn("重算批量发布进度失败", "batch", batchID, "err", err) } cancel() @@ -746,7 +746,7 @@ func publishStatusContext(parent context.Context) (context.Context, context.Canc func (s *Server) finishInterruptedPublishBatch(ctx context.Context, userID int64, batchID, workerToken string) { wctx, cancel := publishStatusContext(ctx) defer cancel() - batch, err := s.Store.PublishBatches.Get(wctx, userID, batchID) + batch, err := s.itemPublishRepositoryForServer().GetBatch(wctx, userID, batchID) if err != nil { return } @@ -755,12 +755,12 @@ func (s *Server) finishInterruptedPublishBatch(ctx context.Context, userID int64 if batch.WorkerToken != workerToken { return } - _, _ = s.Store.PublishBatches.FinalizeCanceled(wctx, batchID, workerToken) + _, _ = s.itemPublishRepositoryForServer().FinalizeCanceled(wctx, batchID, workerToken) } // 取消产生的 interrupted 失败行允许用户稍后重试,图片由统一的过期清理保留 7 天。 return } - _, _, finalizeErr := s.Store.PublishBatches.FinalizeInterrupted(wctx, batchID, workerToken, "任务超时或已中断") + _, _, finalizeErr := s.itemPublishRepositoryForServer().FinalizeInterrupted(wctx, batchID, workerToken, "任务超时或已中断") if finalizeErr != nil && s.Logger != nil { s.Logger.Warn("结束中断的批量发布任务失败,等待租约恢复", "batch", batchID, "err", finalizeErr) } @@ -769,16 +769,16 @@ func (s *Server) finishInterruptedPublishBatch(ctx context.Context, userID int64 func (s *Server) finishPublishBatch(ctx context.Context, userID int64, batchID, workerToken string) { wctx, cancel := publishStatusContext(ctx) defer cancel() - batch, err := s.Store.PublishBatches.Get(wctx, userID, batchID) + batch, err := s.itemPublishRepositoryForServer().GetBatch(wctx, userID, batchID) if err != nil || batch.Status == "canceled" || batch.WorkerToken != workerToken { return } if batch.Status == "canceling" { - _, _ = s.Store.PublishBatches.FinalizeCanceled(wctx, batchID, workerToken) + _, _ = s.itemPublishRepositoryForServer().FinalizeCanceled(wctx, batchID, workerToken) // 取消任务仍可“重试失败项”,不能在此删除重试所需的本地图片。 return } - finalStatus, finished, finishErr := s.Store.PublishBatches.FinalizeBatch(wctx, batchID, workerToken) + finalStatus, finished, finishErr := s.itemPublishRepositoryForServer().FinalizeBatch(wctx, batchID, workerToken) if finishErr != nil { if s.Logger != nil { s.Logger.Warn("结束批量发布任务失败,等待租约恢复", "batch", batchID, "err", finishErr) @@ -804,7 +804,7 @@ func finalPublishBatchStatus(batch *db.ItemPublishBatch) string { } func (s *Server) publishBatchRow(ctx context.Context, userID int64, client mtop.Client, row db.ItemPublishBatchRow, workerToken string) error { - batch, err := s.Store.PublishBatches.Get(ctx, userID, row.BatchID) + batch, err := s.itemPublishRepositoryForServer().GetBatch(ctx, userID, row.BatchID) if err != nil { return errors.New("批量任务不存在") } @@ -852,7 +852,7 @@ func (s *Server) publishBatchRow(ctx context.Context, userID int64, client mtop. return err } markCtx, markCancel := publishStatusContext(ctx) - remoteStarted, markErr := s.Store.PublishBatches.MarkClaimedRemoteStarted(markCtx, row.ID, workerToken) + remoteStarted, markErr := s.itemPublishRepositoryForServer().MarkClaimedRemoteStarted(markCtx, row.ID, workerToken) markCancel() if markErr != nil || !remoteStarted { return fmt.Errorf("保存远端发布前检查点失败: %w", firstError(markErr, errors.New("批次租约已失效"))) @@ -860,7 +860,7 @@ func (s *Server) publishBatchRow(ctx context.Context, userID int64, client mtop. runtimeCookie := "" runtimeCookieChanged := false res, err = func() (*mtop.PublishItemResult, error) { - credentialUnlock := s.Store.LockAccountCredentials(row.CookieID) + credentialUnlock := s.itemPublishRepositoryForServer().LockCredentials(row.CookieID) defer credentialUnlock() latest, latestErr := s.loadCookiePlatformDetail(ctx, row.CookieID) if latestErr != nil { @@ -902,7 +902,7 @@ func (s *Server) publishBatchRow(ctx context.Context, userID int64, client mtop. runtimeCookieChanged = true } } else if publishErr == nil && published != nil && published.UpdatedCookies != "" && published.UpdatedCookies != cookieValue { - if saveErr := s.Store.Cookies.UpdateValueOwned(ctx, row.CookieID, published.UpdatedCookies, userID); saveErr != nil { + if saveErr := s.itemPublishRepositoryForServer().UpdateCookieValueOwned(ctx, row.CookieID, published.UpdatedCookies, userID); saveErr != nil { responseCookieErr = fmt.Errorf("发布商品后保存响应 Cookie: %w", saveErr) } else { runtimeCookie = published.UpdatedCookies @@ -939,7 +939,7 @@ func (s *Server) publishBatchRow(ctx context.Context, userID int64, client mtop. } rawJSON, _ := json.Marshal(res.RawData) saveCtx, saveCancel := publishStatusContext(ctx) - saved, saveErr := s.Store.PublishBatches.SaveClaimedRemoteResult(saveCtx, row.ID, workerToken, res.ItemID, res.ItemURL, string(rawJSON)) + saved, saveErr := s.itemPublishRepositoryForServer().SaveClaimedRemoteResult(saveCtx, row.ID, workerToken, res.ItemID, res.ItemURL, string(rawJSON)) saveCancel() if saveErr != nil || !saved { return &uncertainRemotePublishError{err: fmt.Errorf("保存远端发布结果失败: %w", firstError(saveErr, errors.New("批次租约已失效")))} @@ -953,7 +953,7 @@ func (s *Server) publishBatchRow(ctx context.Context, userID int64, client mtop. if ctx.Err() != nil { return &postPublishError{err: ctx.Err()} } - currentBatch, err := s.Store.PublishBatches.Get(ctx, userID, row.BatchID) + currentBatch, err := s.itemPublishRepositoryForServer().GetBatch(ctx, userID, row.BatchID) if err != nil || currentBatch.Status == "canceled" || currentBatch.WorkerToken != workerToken { return &postPublishError{err: context.Canceled} } @@ -966,7 +966,7 @@ func (s *Server) publishBatchRow(ctx context.Context, userID int64, client mtop. "publish_raw": res.RawData, } detailJSON, _ := json.Marshal(detail) - if err := s.Store.Items.Upsert(ctx, &db.ItemInfoRow{ + if err := s.itemPublishRepositoryForServer().UpsertItem(ctx, &db.ItemInfoRow{ CookieID: row.CookieID, ItemID: res.ItemID, ItemTitle: firstNonEmpty(res.Title, row.Title), @@ -983,7 +983,7 @@ func (s *Server) publishBatchRow(ctx context.Context, userID int64, client mtop. } } rawJSON, _ := json.Marshal(res.RawData) - marked, err := s.Store.PublishBatches.MarkClaimedRowSuccess(ctx, row.ID, workerToken, res.ItemID, res.ItemURL, string(rawJSON)) + marked, err := s.itemPublishRepositoryForServer().MarkClaimedRowSuccess(ctx, row.ID, workerToken, res.ItemID, res.ItemURL, string(rawJSON)) if err != nil { return err } @@ -1321,12 +1321,12 @@ func (s *Server) removePublishUploadDir(ctx context.Context, batch *db.ItemPubli return } _ = os.RemoveAll(batch.UploadDir) - _ = s.Store.PublishBatches.ClearUploadDir(ctx, batch.ID) + _ = s.itemPublishRepositoryForServer().ClearUploadDir(ctx, batch.ID) } func (s *Server) cleanupExpiredPublishUploads(ctx context.Context) { cutoff := time.Now().UTC().Add(-7 * 24 * time.Hour).Format("2006-01-02 15:04:05") - batches, err := s.Store.PublishBatches.ExpiredUploads(ctx, cutoff, 100) + batches, err := s.itemPublishRepositoryForServer().ExpiredUploads(ctx, cutoff, 100) if err != nil { return } @@ -1338,14 +1338,14 @@ func (s *Server) cleanupExpiredPublishUploads(ctx context.Context) { // cookieOwnedByUser 判断指定账号是否属于用户,不读取或解密 Cookie 明文。 func (s *Server) cookieOwnedByUser(ctx context.Context, userID int64, cookieID string) bool { // owned 表示数据库中是否存在匹配用户和账号 ID 的记录。 - owned, err := s.Store.Cookies.ExistsOwned(ctx, userID, cookieID) + owned, err := s.itemPublishRepositoryForServer().ExistsOwned(ctx, userID, cookieID) return err == nil && owned } // cookieValueForUser 读取指定用户拥有的单个账号 Cookie 明文。 func (s *Server) cookieValueForUser(ctx context.Context, userID int64, cookieID string) (string, error) { // value 是按 user_id 与账号 ID 联合过滤后解密的单个 Cookie 明文。 - value, err := s.Store.Cookies.GetValueOwned(ctx, userID, cookieID) + value, err := s.itemPublishRepositoryForServer().GetCookieValueOwned(ctx, userID, cookieID) if err != nil { if errors.Is(err, db.ErrNotFound) { return "", errors.New("账号不存在或 Cookie 为空") diff --git a/internal/server/item_publish_repository.go b/internal/server/item_publish_repository.go index c15bf92..b3e13a9 100644 --- a/internal/server/item_publish_repository.go +++ b/internal/server/item_publish_repository.go @@ -38,6 +38,40 @@ type itemPublishRepository interface { DeleteBatch(ctx context.Context, userID int64, batchID string) error // ResetFailed 重置批次失败明细。 ResetFailed(ctx context.Context, batchID string) error + // RecoverableBatches 查询可恢复的批次。 + RecoverableBatches(ctx context.Context, now int64, limit int) ([]db.ItemPublishBatch, error) + // FinalizeExpiredCancellation 收口已过期的取消请求。 + FinalizeExpiredCancellation(ctx context.Context, batchID string, now int64) (bool, error) + // ResetInterrupted 重置进程中断的批次。 + ResetInterrupted(ctx context.Context, batchID string) error + // FailClaimedBatch 标记当前 worker 持有的批次失败。 + FailClaimedBatch(ctx context.Context, batchID, workerToken string) (bool, error) + // RenewBatchLease 续租批次 worker。 + RenewBatchLease(ctx context.Context, batchID, workerToken string, leaseExpiresAt int64) (bool, error) + // ClaimRow 抢占批次明细行。 + ClaimRow(ctx context.Context, rowID int64, workerToken string) (bool, error) + // BatchStatus 查询批次状态。 + BatchStatus(ctx context.Context, batchID string) (string, error) + // MarkClaimedRowFailed 标记 worker 持有的明细行失败。 + MarkClaimedRowFailed(ctx context.Context, rowID int64, workerToken, message, kind string) (bool, error) + // FinalizeCanceled 收口已取消批次。 + FinalizeCanceled(ctx context.Context, batchID, workerToken string) (bool, error) + // FinalizeInterrupted 收口被中断批次。 + FinalizeInterrupted(ctx context.Context, batchID, workerToken, message string) (string, bool, error) + // MarkClaimedRemoteStarted 记录远端发布已开始。 + MarkClaimedRemoteStarted(ctx context.Context, rowID int64, workerToken string) (bool, error) + // SaveClaimedRemoteResult 保存远端发布结果。 + SaveClaimedRemoteResult(ctx context.Context, rowID int64, workerToken, itemID, itemURL, rawJSON string) (bool, error) + // MarkClaimedRowSuccess 标记 worker 持有的明细行成功。 + MarkClaimedRowSuccess(ctx context.Context, rowID int64, workerToken, itemID, itemURL, rawJSON string) (bool, error) + // ClearUploadDir 清理批次上传目录记录。 + ClearUploadDir(ctx context.Context, batchID string) error + // ExpiredUploads 查询过期上传目录。 + ExpiredUploads(ctx context.Context, cutoff string, limit int) ([]db.ItemPublishBatch, error) + // GetCookieValueOwned 读取用户拥有账号的 Cookie。 + GetCookieValueOwned(ctx context.Context, userID int64, cookieID string) (string, error) + // ExistsOwned 判断账号是否归属于用户。 + ExistsOwned(ctx context.Context, userID int64, cookieID string) (bool, error) } // storeItemPublishRepository 将完整 Store 适配为发布应用服务窄 repository。 @@ -126,6 +160,91 @@ func (r storeItemPublishRepository) ResetFailed(ctx context.Context, batchID str return r.store.PublishBatches.ResetFailed(ctx, batchID) } +// RecoverableBatches 委托可恢复批次查询。 +func (r storeItemPublishRepository) RecoverableBatches(ctx context.Context, now int64, limit int) ([]db.ItemPublishBatch, error) { + return r.store.PublishBatches.Recoverable(ctx, now, limit) +} + +// FinalizeExpiredCancellation 委托过期取消收口。 +func (r storeItemPublishRepository) FinalizeExpiredCancellation(ctx context.Context, batchID string, now int64) (bool, error) { + return r.store.PublishBatches.FinalizeExpiredCancellation(ctx, batchID, now) +} + +// ResetInterrupted 委托中断批次重置。 +func (r storeItemPublishRepository) ResetInterrupted(ctx context.Context, batchID string) error { + return r.store.PublishBatches.ResetInterrupted(ctx, batchID) +} + +// FailClaimedBatch 委托 worker 持有批次失败标记。 +func (r storeItemPublishRepository) FailClaimedBatch(ctx context.Context, batchID, workerToken string) (bool, error) { + return r.store.PublishBatches.FailClaimedBatch(ctx, batchID, workerToken) +} + +// RenewBatchLease 委托批次租约续期。 +func (r storeItemPublishRepository) RenewBatchLease(ctx context.Context, batchID, workerToken string, leaseExpiresAt int64) (bool, error) { + return r.store.PublishBatches.RenewBatchLease(ctx, batchID, workerToken, leaseExpiresAt) +} + +// ClaimRow 委托批次明细行抢占。 +func (r storeItemPublishRepository) ClaimRow(ctx context.Context, rowID int64, workerToken string) (bool, error) { + return r.store.PublishBatches.ClaimRow(ctx, rowID, workerToken) +} + +// BatchStatus 委托批次状态查询。 +func (r storeItemPublishRepository) BatchStatus(ctx context.Context, batchID string) (string, error) { + return r.store.PublishBatches.BatchStatus(ctx, batchID) +} + +// MarkClaimedRowFailed 委托 worker 持有明细行失败标记。 +func (r storeItemPublishRepository) MarkClaimedRowFailed(ctx context.Context, rowID int64, workerToken, message, kind string) (bool, error) { + return r.store.PublishBatches.MarkClaimedRowFailed(ctx, rowID, workerToken, message, kind) +} + +// FinalizeCanceled 委托已取消批次收口。 +func (r storeItemPublishRepository) FinalizeCanceled(ctx context.Context, batchID, workerToken string) (bool, error) { + return r.store.PublishBatches.FinalizeCanceled(ctx, batchID, workerToken) +} + +// FinalizeInterrupted 委托中断批次收口。 +func (r storeItemPublishRepository) FinalizeInterrupted(ctx context.Context, batchID, workerToken, message string) (string, bool, error) { + return r.store.PublishBatches.FinalizeInterrupted(ctx, batchID, workerToken, message) +} + +// MarkClaimedRemoteStarted 委托远端发布开始标记。 +func (r storeItemPublishRepository) MarkClaimedRemoteStarted(ctx context.Context, rowID int64, workerToken string) (bool, error) { + return r.store.PublishBatches.MarkClaimedRemoteStarted(ctx, rowID, workerToken) +} + +// SaveClaimedRemoteResult 委托远端发布结果保存。 +func (r storeItemPublishRepository) SaveClaimedRemoteResult(ctx context.Context, rowID int64, workerToken, itemID, itemURL, rawJSON string) (bool, error) { + return r.store.PublishBatches.SaveClaimedRemoteResult(ctx, rowID, workerToken, itemID, itemURL, rawJSON) +} + +// MarkClaimedRowSuccess 委托明细行成功标记。 +func (r storeItemPublishRepository) MarkClaimedRowSuccess(ctx context.Context, rowID int64, workerToken, itemID, itemURL, rawJSON string) (bool, error) { + return r.store.PublishBatches.MarkClaimedRowSuccess(ctx, rowID, workerToken, itemID, itemURL, rawJSON) +} + +// ClearUploadDir 委托批次上传目录清理。 +func (r storeItemPublishRepository) ClearUploadDir(ctx context.Context, batchID string) error { + return r.store.PublishBatches.ClearUploadDir(ctx, batchID) +} + +// ExpiredUploads 委托过期上传目录查询。 +func (r storeItemPublishRepository) ExpiredUploads(ctx context.Context, cutoff string, limit int) ([]db.ItemPublishBatch, error) { + return r.store.PublishBatches.ExpiredUploads(ctx, cutoff, limit) +} + +// GetCookieValueOwned 委托用户账号 Cookie 查询。 +func (r storeItemPublishRepository) GetCookieValueOwned(ctx context.Context, userID int64, cookieID string) (string, error) { + return r.store.Cookies.GetValueOwned(ctx, userID, cookieID) +} + +// ExistsOwned 委托账号归属查询。 +func (r storeItemPublishRepository) ExistsOwned(ctx context.Context, userID int64, cookieID string) (bool, error) { + return r.store.Cookies.ExistsOwned(ctx, userID, cookieID) +} + // newStoreItemPublishRepository 从完整 Store 构造发布应用服务窄 repository。 func newStoreItemPublishRepository(store *db.Store) itemPublishRepository { if store == nil || store.Cookies == nil || store.Items == nil || store.PublishBatches == nil { diff --git a/internal/server/item_publish_service.go b/internal/server/item_publish_service.go index 38460cb..77067c4 100644 --- a/internal/server/item_publish_service.go +++ b/internal/server/item_publish_service.go @@ -90,6 +90,11 @@ func (s *Server) itemPublishApplication() *itemPublishService { return s.applicationServiceSet().itemPublish } +// itemPublishRepositoryForServer 返回当前 Server 装配的发布持久化边界。 +func (s *Server) itemPublishRepositoryForServer() itemPublishRepository { + return s.itemPublishApplication().repository +} + // PublishSingle 执行单商品发布、响应 Cookie 持久化和本地商品落库。 func (svc *itemPublishService) PublishSingle(ctx context.Context, input itemPublishInput) (itemPublishOutcome, error) { // s 是当前商品发布应用服务依赖的 Server。