feat: 增加批量铺货发布强制间隔

This commit is contained in:
Christ committed 2026-08-18 16:19:07 +08:00
1 parent a90c9422c1
commit be080e1500
39 files changed
+362 -75

No files matched your search

+2 -1
View File
@@ -45,7 +45,7 @@ func NewItemBatchPublishPort(store *db.Store, client func() mtop.Client, logger
}
// PublishRemoteRow 执行单行商品远端发布并保存远端结果检查点,不写入本地商品或自动化规则。
func (p *ItemBatchPublishPort) PublishRemoteRow(ctx context.Context, userID int64, row itemapp.BatchRow, workerToken string) (itemapp.BatchPublishOutcome, error) {
func (p *ItemBatchPublishPort) PublishRemoteRow(ctx context.Context, userID int64, row itemapp.BatchRow, workerToken string, beforePublish func(context.Context) error) (itemapp.BatchPublishOutcome, error) {
// validateErr 保存适配器依赖校验错误。
if validateErr := p.validate(); validateErr != nil {
return itemapp.BatchPublishOutcome{}, validateErr
@@ -116,6 +116,7 @@ func (p *ItemBatchPublishPort) PublishRemoteRow(ctx context.Context, userID int6
Title: row.Title, Description: firstBatchNonEmpty(row.Description, row.Title), PriceCents: priceCents,
OriginalPriceCents: originalPriceCents, Quantity: row.Quantity, PostageMode: row.PostageMode,
PostageCents: postageCents, Virtual: true, Location: location, PreferredCategory: preferredCategory, Images: images,
BeforePublish: beforePublish,
})
cancel()
// runtimeCookie、persistErr 保存会话写回后的运行时 Cookie 和错误。
+3 -3
View File
@@ -77,7 +77,7 @@ func TestItemBatchPublishPortPersistsRemoteCheckpoint(t *testing.T) {
},
func(context.Context, string) ([]byte, string, error) { return nil, "", nil })
// outcome、publishErr 保存远端端口结果及错误。
outcome, publishErr := port.PublishRemoteRow(ctx, admin.ID, batchRowApplicationModel(storedRows[0]), "worker-remote")
outcome, publishErr := port.PublishRemoteRow(ctx, admin.ID, batchRowApplicationModel(storedRows[0]), "worker-remote", nil)
if publishErr != nil || outcome.Result == nil || outcome.Result.ItemID != "remote-item" {
t.Fatalf("远端端口结果异常: outcome=%+v err=%v", outcome, publishErr)
}
@@ -95,7 +95,7 @@ func TestItemBatchPublishPortPersistsRemoteCheckpoint(t *testing.T) {
checkpointPort := NewItemBatchPublishPort(store, func() mtop.Client { return client }, nil, nil, nil, nil, nil)
// checkpointOutcome 保存从远端检查点恢复出的应用结果。
// checkpointErr 保存检查点恢复阶段的错误。
checkpointOutcome, checkpointErr := checkpointPort.PublishRemoteRow(ctx, admin.ID, batchRowApplicationModel(savedRows[0]), "worker-remote")
checkpointOutcome, checkpointErr := checkpointPort.PublishRemoteRow(ctx, admin.ID, batchRowApplicationModel(savedRows[0]), "worker-remote", nil)
if checkpointErr != nil || checkpointOutcome.Result == nil || checkpointOutcome.Result.ItemID != "remote-item" {
t.Fatalf("远端检查点重试异常: outcome=%+v err=%v", checkpointOutcome, checkpointErr)
}
@@ -109,7 +109,7 @@ func TestItemBatchPublishPortRejectsMissingDependencies(t *testing.T) {
// port 是未配置数据库的批量远端适配器。
port := NewItemBatchPublishPort(nil, nil, nil, nil, nil, func(string, string) ([]byte, string, string, error) { return nil, "", "", nil }, func(context.Context, string) ([]byte, string, error) { return nil, "", nil })
// _, publishErr 保存依赖校验错误。
_, publishErr := port.PublishRemoteRow(context.Background(), 1, itemapp.BatchRow{BatchID: "batch"}, "worker")
_, publishErr := port.PublishRemoteRow(context.Background(), 1, itemapp.BatchRow{BatchID: "batch"}, "worker", nil)
if publishErr == nil {
t.Fatal("缺少数据库时不应伪装批量远端发布成功")
}
+12 -2
View File
@@ -75,7 +75,7 @@ func (r *ItemBatchRepository) CreateBatch(ctx context.Context, batch itemapp.Bat
ErrorMessage: errorMessage, FailureKind: failureKind, RawJSON: string(rawJSON),
})
}
return r.store.PublishBatches.Create(ctx, &db.ItemPublishBatch{ID: batch.ID, UserID: batch.UserID, DefaultCookieID: batch.DefaultCookieID, Filename: batch.Filename, UploadDir: batch.UploadDir, LocationJSON: string(locationJSON), Status: status}, databaseRows)
return r.store.PublishBatches.Create(ctx, &db.ItemPublishBatch{ID: batch.ID, UserID: batch.UserID, DefaultCookieID: batch.DefaultCookieID, Filename: batch.Filename, UploadDir: batch.UploadDir, LocationJSON: string(locationJSON), PublishIntervalSeconds: batch.PublishIntervalSeconds, Status: status}, databaseRows)
}
// PendingRows 查询批次待处理明细并转换为应用模型。
@@ -107,6 +107,15 @@ func (r *ItemBatchRepository) RenewBatchLease(ctx context.Context, batchID, work
return r.store.PublishBatches.RenewBatchLease(ctx, batchID, workerToken, leaseExpiresAt)
}
// ReservePublishSlot 原子预留一次符合批次间隔的最终商品发布时刻。
func (r *ItemBatchRepository) ReservePublishSlot(ctx context.Context, batchID, workerToken string, minimumLastStartedAtMillis, startedAtMillis int64) (bool, error) {
// err 表示适配器依赖校验错误。
if err := r.validate(); err != nil {
return false, err
}
return r.store.PublishBatches.ReservePublishSlot(ctx, batchID, workerToken, minimumLastStartedAtMillis, startedAtMillis)
}
// GetBatch 查询批次并转换为应用状态模型。
func (r *ItemBatchRepository) GetBatch(ctx context.Context, userID int64, batchID string) (itemapp.BatchInfo, error) {
// err 表示适配器依赖校验错误。
@@ -398,7 +407,8 @@ func batchInfoApplicationModel(batch db.ItemPublishBatch) itemapp.BatchInfo {
return itemapp.BatchInfo{
ID: batch.ID, UserID: batch.UserID, Status: batch.Status, WorkerToken: batch.WorkerToken,
DefaultCookieID: batch.DefaultCookieID, Filename: batch.Filename, UploadDir: batch.UploadDir,
LocationJSON: batch.LocationJSON, TotalCount: batch.TotalCount, SuccessCount: batch.SuccessCount,
LocationJSON: batch.LocationJSON, PublishIntervalSeconds: batch.PublishIntervalSeconds, LastPublishStartedAtMillis: batch.LastPublishStartedAtMillis,
TotalCount: batch.TotalCount, SuccessCount: batch.SuccessCount,
FailedCount: batch.FailedCount, LeaseExpiresAt: batch.LeaseExpiresAt,
CreatedAt: batch.CreatedAt, UpdatedAt: batch.UpdatedAt,
}