修复库存监控提前停止并发布0.2.0

This commit is contained in:
yml2213
2026-08-09 19:37:30 +08:00
parent 02d38d9ede
commit 266f4a3da1
13 changed files with 239 additions and 31 deletions
+1 -1
View File
@@ -132,7 +132,7 @@ func (s *Scheduler) scheduleTimers(accountProducts map[string]string, targetTime
return
}
if s.clientSnapshot().Activity.IsXiaodian {
s.RunXiaodian(accountProducts, attempts, delayMs)
s.RunXiaodian(accountProducts, attempts, delayMs, "")
return
}
s.RunNow(accountProducts)
+2 -2
View File
@@ -616,7 +616,7 @@ func (s *Scheduler) RunFixed(accountProducts map[string]string, attempts, delayM
// RunXiaodian 使用和平小店专用策略:固定次数、固定间隔、每轮直接调用 livebuy。
// 小店补货不依赖旧活动的 75 秒阶段窗口,也不执行手册的预检链路。
func (s *Scheduler) RunXiaodian(accountProducts map[string]string, attempts, delayMs int) {
func (s *Scheduler) RunXiaodian(accountProducts map[string]string, attempts, delayMs int, reportSessionID string) {
defer s.finishRun()
s.ClearResults()
@@ -641,7 +641,7 @@ func (s *Scheduler) RunXiaodian(accountProducts map[string]string, attempts, del
logger.LogEvent("抢购", "warn", "和平小店抢购没有可执行账号")
return
}
s.startXiaodianReport(accounts, products)
s.startXiaodianReport(accounts, products, reportSessionID)
stoppedAccounts := make(map[string]string)
cooldownAccounts := make(map[string]time.Time)
+43 -4
View File
@@ -15,9 +15,51 @@ import (
const xiaodianReportFileName = "last_xiaodian_report.json"
func (s *Scheduler) startXiaodianReport(accounts []types.Account, products map[string]types.Product) {
func (s *Scheduler) startXiaodianReport(accounts []types.Account, products map[string]types.Product, sessionID string) {
s.reportMu.Lock()
defer s.reportMu.Unlock()
// 库存监控会把不同商品/账号拆成多次任务;同一会话追加账号,普通抢购则创建新报告。
if sessionID != "" && s.latestXiaodianReport != nil && s.latestXiaodianReport.SessionID == sessionID {
report := s.latestXiaodianReport
report.Stopped = false
report.FinishedAt = time.Time{}
existing := make(map[string]int, len(report.Accounts))
for index := range report.Accounts {
existing[report.Accounts[index].AccountName] = index
}
for _, account := range accounts {
product := products[account.Name]
if index, ok := existing[account.Name]; ok {
// 商品映射在会话内保持账号独立;仅补齐可能缺失的商品元数据。
item := &report.Accounts[index]
if item.ProductID == "" {
item.ProductID = product.CommodityID
}
if item.ProductName == "" {
item.ProductName = product.CommodityName
}
continue
}
report.Accounts = append(report.Accounts, types.XiaodianRunReportAccount{
AccountName: account.Name,
ProductID: product.CommodityID,
ProductName: product.CommodityName,
ProductPrice: product.Score,
PayType: product.PayType,
PayLabel: product.PayLabel,
Status: "待执行",
})
}
sort.Slice(report.Accounts, func(i, j int) bool {
return report.Accounts[i].AccountName < report.Accounts[j].AccountName
})
return
}
report := types.XiaodianRunReport{
ReportID: fmt.Sprintf("xiaodian_%s", time.Now().Format("20060102_150405.000")),
SessionID: sessionID,
StartedAt: time.Now(),
Accounts: make([]types.XiaodianRunReportAccount, 0, len(accounts)),
}
@@ -36,10 +78,7 @@ func (s *Scheduler) startXiaodianReport(accounts []types.Account, products map[s
sort.Slice(report.Accounts, func(i, j int) bool {
return report.Accounts[i].AccountName < report.Accounts[j].AccountName
})
s.reportMu.Lock()
s.latestXiaodianReport = &report
s.reportMu.Unlock()
}
func (s *Scheduler) updateXiaodianReport(results map[string]types.ExchangeResult) {
+47 -1
View File
@@ -1,6 +1,10 @@
package seckill
import "testing"
import (
"testing"
"douyu_go/internal/types"
)
func TestReportFailureReasonUsesXiaodianStopReason(t *testing.T) {
message := "失败|和平小店兑换失败: 对不起,Livelink登录态校验失败!"
@@ -8,3 +12,45 @@ func TestReportFailureReasonUsesXiaodianStopReason(t *testing.T) {
t.Fatalf("reportFailureReason() = %q", got)
}
}
func TestStartXiaodianReportAppendsWithinSession(t *testing.T) {
scheduler := &Scheduler{}
firstAccounts := []types.Account{{Name: "账号A"}}
firstProducts := map[string]types.Product{
"账号A": {CommodityID: "goods-a", CommodityName: "商品A"},
}
scheduler.startXiaodianReport(firstAccounts, firstProducts, "stock-session")
scheduler.updateXiaodianReport(map[string]types.ExchangeResult{
"账号A": {OK: true, Account: "账号A", Message: "成功|角色A"},
})
secondAccounts := []types.Account{{Name: "账号B"}}
secondProducts := map[string]types.Product{
"账号B": {CommodityID: "goods-b", CommodityName: "商品B"},
}
scheduler.startXiaodianReport(secondAccounts, secondProducts, "stock-session")
report, ok := scheduler.LatestXiaodianReport()
if !ok || len(report.Accounts) != 2 {
t.Fatalf("同一监控会话应保留两批账号,报告=%+v", report)
}
if report.Accounts[0].ProductID != "goods-a" || report.Accounts[1].ProductID != "goods-b" {
t.Fatalf("账号商品映射丢失: %+v", report.Accounts)
}
if report.Accounts[0].Status != "成功" || report.Accounts[0].Attempts != 1 {
t.Fatalf("前一批结果被覆盖: %+v", report.Accounts[0])
}
}
func TestStartXiaodianReportDifferentSessionReplacesReport(t *testing.T) {
scheduler := &Scheduler{}
scheduler.startXiaodianReport([]types.Account{{Name: "账号A"}}, map[string]types.Product{
"账号A": {CommodityID: "goods-a"},
}, "session-a")
scheduler.startXiaodianReport([]types.Account{{Name: "账号B"}}, map[string]types.Product{
"账号B": {CommodityID: "goods-b"},
}, "session-b")
report, ok := scheduler.LatestXiaodianReport()
if !ok || report.SessionID != "session-b" || len(report.Accounts) != 1 || report.Accounts[0].AccountName != "账号B" {
t.Fatalf("不同会话不应串报告: %+v", report)
}
}