fix(orchestrator): prevent concurrent double-run duplicating messages; reflect errors in status

This commit is contained in:
2026-07-01 19:32:04 +07:00
parent 1373aa0a77
commit 2429c786e4
6 changed files with 72 additions and 4 deletions
+16 -3
View File
@@ -13,6 +13,7 @@ import (
)
var ErrNotTested = errors.New("accounts not fully tested")
var ErrAlreadyRunning = errors.New("task already running")
type Orchestrator struct {
store *store.Store
@@ -99,15 +100,23 @@ func (o *Orchestrator) Run(ctx context.Context, taskID int64) (int64, error) {
if !gateOK(accs) {
return 0, ErrNotTested
}
acquired, err := o.store.TryMarkTaskRunning(ctx, taskID)
if err != nil {
return 0, err
}
if !acquired {
return 0, ErrAlreadyRunning
}
srcEP, dstEP, err := o.endpoints(ctx, task)
if err != nil {
_ = o.store.SetTaskStatus(ctx, taskID, "error")
return 0, err
}
runID, err := o.store.CreateRun(ctx, taskID)
if err != nil {
_ = o.store.SetTaskStatus(ctx, taskID, "error")
return 0, err
}
_ = o.store.SetTaskStatus(ctx, taskID, "running")
o.hub.Publish(wshub.Event{Type: "run_started", TaskID: taskID, Data: map[string]any{"run_id": runID}})
go o.runAll(context.WithoutCancel(ctx), task, runID, accs, srcEP, dstEP)
@@ -138,8 +147,12 @@ func (o *Orchestrator) runAll(ctx context.Context, task store.Task, runID int64,
}
wg.Wait()
_ = o.store.FinishRun(ctx, runID, "done", totCopied, totSkipped, totErr)
_ = o.store.SetTaskStatus(ctx, task.ID, "done")
status := "done"
if totErr > 0 {
status = "done_with_errors"
}
_ = o.store.FinishRun(ctx, runID, status, totCopied, totSkipped, totErr)
_ = o.store.SetTaskStatus(ctx, task.ID, status)
o.hub.Publish(wshub.Event{Type: "run_done", TaskID: task.ID,
Data: map[string]any{"run_id": runID, "copied": totCopied, "skipped": totSkipped, "errors": totErr}})
}