fix(orchestrator): prevent concurrent double-run duplicating messages; reflect errors in status
This commit is contained in:
@@ -55,3 +55,14 @@ func (s *Store) SetTaskStatus(ctx context.Context, id int64, status string) erro
|
||||
_, err := s.Pool.Exec(ctx, `UPDATE tasks SET status=$2 WHERE id=$1`, id, status)
|
||||
return err
|
||||
}
|
||||
|
||||
// TryMarkTaskRunning atomically sets status='running' only if the task is not already running.
|
||||
// Returns true if this call acquired the run (status was not 'running' before), false otherwise.
|
||||
func (s *Store) TryMarkTaskRunning(ctx context.Context, id int64) (bool, error) {
|
||||
ct, err := s.Pool.Exec(ctx,
|
||||
`UPDATE tasks SET status='running' WHERE id=$1 AND status<>'running'`, id)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return ct.RowsAffected() == 1, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user