forked from longtao.wu/droboticflow
🐛 Bound nightly acceptance refresh per organization
This commit is contained in:
parent
b7f4924110
commit
3034a5a3cb
@ -48,4 +48,4 @@
|
||||
|
||||
管理员面板读取组织最近一次成功保存的汇总,按当前已保存配置的项目范围展示“待领取”和“归属待核对”,同时显示北京时间的统计完成时间。首次统计前返回 `status: pending`、两个计数为 null。配置变化可以直接用同一份分项目汇总计算对应范围。统计读取失败仍可重试。
|
||||
|
||||
API 实例在北京时间 01:00 竞争当日刷新资格,单实例串行处理组织,每 100 条候选任务批量解析来源。一个组织完整处理后原子替换其汇总。01:20 再检查一次,只补当天未完成的组织。每个窗口最多 15 分钟,窗口内重启可以参与未被领取的窗口。两个窗口都错过或未完成时保留最近成功值,次日再试。完成日志记录候选条数和用时。统计反映扫描期间看到的数据,业务领取和配置保存按原流程实时处理。
|
||||
API 实例在北京时间 01:00 竞争当日刷新资格,单实例串行处理组织,每 100 条候选任务批量解析来源。一个组织完整处理后原子替换其汇总。01:20 再检查一次,只补当天未完成的组织。每个窗口最多 15 分钟。每个组织最多使用剩余时间除以待检查的组织数,超时或读取失败后继续下一个组织,提前完成时,未用的时间留给后续组织。窗口内重启可以参与未被领取的窗口。两个窗口都错过或未完成时保留最近成功值,次日再试。完成日志记录候选条数和用时。统计反映扫描期间看到的数据,业务领取和配置保存按原流程实时处理。
|
||||
|
||||
@ -64,30 +64,45 @@ func refreshAcceptanceOrganizations(ctx context.Context, db store.Store, slot ti
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, org := range orgs {
|
||||
day := slot.In(acceptanceZone).Format(time.DateOnly)
|
||||
for i, org := range orgs {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
last, err := db.ReadAcceptanceSnapshot(ctx, org.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
// Reserve an equal share of the remaining window for each organization.
|
||||
// A fast or already-completed organization leaves its unused time to the rest.
|
||||
remaining := acceptanceWindow
|
||||
if deadline, ok := ctx.Deadline(); ok {
|
||||
remaining = time.Until(deadline)
|
||||
}
|
||||
if last != nil {
|
||||
completed, err := time.Parse(time.RFC3339Nano, last.CompletedAt)
|
||||
if err == nil && completed.In(acceptanceZone).Format(time.DateOnly) == slot.In(acceptanceZone).Format(time.DateOnly) {
|
||||
continue
|
||||
}
|
||||
}
|
||||
start := time.Now()
|
||||
n, err := refreshAcceptanceSnapshot(ctx, db, org.ID)
|
||||
log.Printf("acceptance statistics org=%q candidates=%d elapsed=%s completed=%t", org.ID, n, time.Since(start), err == nil)
|
||||
orgCtx, cancel := context.WithTimeout(ctx, remaining/time.Duration(len(orgs)-i))
|
||||
err := refreshAcceptanceOrganization(orgCtx, db, org.ID, day)
|
||||
cancel()
|
||||
if err != nil {
|
||||
log.Printf("acceptance statistics refresh failed: %v", err)
|
||||
log.Printf("acceptance statistics org=%q refresh failed: %v", org.ID, err)
|
||||
}
|
||||
}
|
||||
return ctx.Err()
|
||||
}
|
||||
|
||||
func refreshAcceptanceOrganization(ctx context.Context, db store.Store, org, day string) error {
|
||||
last, err := db.ReadAcceptanceSnapshot(ctx, org)
|
||||
if err != nil {
|
||||
return fmt.Errorf("read previous snapshot: %w", err)
|
||||
}
|
||||
if last != nil {
|
||||
completed, err := time.Parse(time.RFC3339Nano, last.CompletedAt)
|
||||
if err == nil && completed.In(acceptanceZone).Format(time.DateOnly) == day {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
start := time.Now()
|
||||
n, err := refreshAcceptanceSnapshot(ctx, db, org)
|
||||
log.Printf("acceptance statistics org=%q candidates=%d elapsed=%s completed=%t", org, n, time.Since(start), err == nil)
|
||||
return err
|
||||
}
|
||||
|
||||
func refreshAcceptanceSnapshot(ctx context.Context, db store.Store, org string) (int, error) {
|
||||
type key struct{ project, supplier string }
|
||||
counts := map[key]int{}
|
||||
|
||||
@ -188,3 +188,65 @@ func TestAcceptancePrioritySnapshotFirstRunAndFailure(t *testing.T) {
|
||||
t.Fatal(after, first)
|
||||
}
|
||||
}
|
||||
|
||||
type acceptanceRefreshTimeoutStore struct {
|
||||
store.Store
|
||||
attempts map[string]int
|
||||
}
|
||||
|
||||
func (s *acceptanceRefreshTimeoutStore) ListOrganizations(context.Context) ([]domain.OrganizationRecord, error) {
|
||||
return []domain.OrganizationRecord{{ID: "first"}, {ID: "second"}}, nil
|
||||
}
|
||||
|
||||
func (s *acceptanceRefreshTimeoutStore) VisitAcceptanceCandidates(ctx context.Context, org, stage string, visit func([]domain.HumanItemRecord) error) error {
|
||||
s.attempts[org]++
|
||||
if org == "first" && s.attempts[org] == 1 {
|
||||
<-ctx.Done()
|
||||
return ctx.Err()
|
||||
}
|
||||
return s.Store.VisitAcceptanceCandidates(ctx, org, stage, visit)
|
||||
}
|
||||
|
||||
func TestAcceptancePriorityRefreshTimeoutLetsLaterOrganizationFinish(t *testing.T) {
|
||||
db := &acceptanceRefreshTimeoutStore{Store: store.NewMemoryStore(), attempts: make(map[string]int)}
|
||||
old := domain.AcceptanceSnapshot{
|
||||
OrganizationID: "first",
|
||||
CompletedAt: time.Now().Add(-24 * time.Hour).UTC().Format(time.RFC3339Nano),
|
||||
Counts: []domain.AcceptanceCount{{ProjectID: "project", Count: 12}},
|
||||
}
|
||||
if err := db.SaveAcceptanceSnapshot(t.Context(), old); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
slot := time.Now()
|
||||
ctx, cancel := context.WithTimeout(t.Context(), time.Second)
|
||||
defer cancel()
|
||||
if err := refreshAcceptanceOrganizations(ctx, db, slot); err != nil {
|
||||
t.Fatalf("slow organization exhausted the entire window: %v", err)
|
||||
}
|
||||
first, err := db.ReadAcceptanceSnapshot(t.Context(), "first")
|
||||
if err != nil || first == nil || first.CompletedAt != old.CompletedAt || len(first.Counts) != 1 || first.Counts[0] != old.Counts[0] {
|
||||
t.Fatalf("timed-out refresh replaced the previous snapshot: %+v, %v", first, err)
|
||||
}
|
||||
second, err := db.ReadAcceptanceSnapshot(t.Context(), "second")
|
||||
if err != nil || second == nil {
|
||||
t.Fatalf("later organization did not publish its snapshot: %+v, %v", second, err)
|
||||
}
|
||||
|
||||
// Use the successful snapshot's day so this test also works around midnight.
|
||||
retryDay, err := time.Parse(time.RFC3339Nano, second.CompletedAt)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
retry, cancelRetry := context.WithTimeout(t.Context(), time.Second)
|
||||
defer cancelRetry()
|
||||
if err := refreshAcceptanceOrganizations(retry, db, retryDay); err != nil {
|
||||
t.Fatalf("retry failed: %v", err)
|
||||
}
|
||||
first, err = db.ReadAcceptanceSnapshot(t.Context(), "first")
|
||||
if err != nil || first == nil || first.CompletedAt == old.CompletedAt {
|
||||
t.Fatalf("retry did not publish a fresh snapshot: %+v, %v", first, err)
|
||||
}
|
||||
if db.attempts["first"] != 2 || db.attempts["second"] != 1 {
|
||||
t.Fatalf("retry must only scan unfinished organizations: %v", db.attempts)
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user