fix(runs): cancel the runs a cancelled run called (#2086)
This commit is contained in:
1 file changed
+21
-5
@@ -295,7 +295,8 @@ type execer interface {
|
||||
// if it is waiting. Only an ending an operator or the caller must act on
|
||||
// counts: a run waiting for approval has not ended, and its caller keeps
|
||||
// waiting. The first ending wins, so a later retry of a parked child does not
|
||||
// change what the parent was told.
|
||||
// change what the parent was told. A parent that has already ended is not told
|
||||
// at all: nothing reads the signal, and a cancelled parent must stay cancelled.
|
||||
func signalParent(ctx context.Context, q execer, id uuid.UUID) error {
|
||||
if _, err := q.ExecContext(ctx, `
|
||||
WITH ended AS (
|
||||
@@ -307,7 +308,9 @@ func signalParent(ctx context.Context, q execer, id uuid.UUID) error {
|
||||
AND state IN ('succeeded', 'failed', 'cancelled', 'parked', 'declined')),
|
||||
sent AS (
|
||||
INSERT INTO runs.signals (run_id, key, payload)
|
||||
SELECT parent_run_id, parent_signal, payload FROM ended
|
||||
SELECT e.parent_run_id, e.parent_signal, e.payload FROM ended e
|
||||
JOIN runs.runs p ON p.id = e.parent_run_id
|
||||
WHERE p.state NOT IN ('succeeded', 'failed', 'cancelled', 'declined')
|
||||
ON CONFLICT (run_id, key) DO NOTHING)
|
||||
UPDATE runs.runs r SET state = 'queued', wake_at = NULL, updated_at = now()
|
||||
FROM ended e
|
||||
@@ -420,8 +423,15 @@ func (s *Store) Signalled(ctx context.Context, id uuid.UUID, key string) (jsonte
|
||||
return payload, true, nil
|
||||
}
|
||||
|
||||
// Cancel asks a run to stop. A run not currently held ends at once and tells
|
||||
// the run waiting on it; a running one sees the request at its next step.
|
||||
// cancelDepth bounds how far Cancel follows workflow.call children. It matches
|
||||
// the call depth limit a call chain is started under.
|
||||
const cancelDepth = 5
|
||||
|
||||
// Cancel asks a run, and every run it started through workflow.call, to stop.
|
||||
// A run not currently held ends at once; a held one sees the request at its
|
||||
// next step and its worker ends it. Runs that have already ended are left
|
||||
// alone. Only the named run tells its own caller: the descendants' callers are
|
||||
// being cancelled in the same transaction.
|
||||
func (s *Store) Cancel(ctx context.Context, id uuid.UUID) error {
|
||||
tx, err := s.db.BeginTxx(ctx, nil)
|
||||
if err != nil {
|
||||
@@ -429,11 +439,17 @@ func (s *Store) Cancel(ctx context.Context, id uuid.UUID) error {
|
||||
}
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
if _, err := tx.ExecContext(ctx, `
|
||||
WITH RECURSIVE tree(id, depth) AS (
|
||||
SELECT id, 0 FROM runs.runs WHERE id = $1
|
||||
UNION ALL
|
||||
SELECT c.id, t.depth + 1 FROM runs.runs c JOIN tree t ON c.parent_run_id = t.id
|
||||
WHERE t.depth < $2)
|
||||
UPDATE runs.runs
|
||||
SET cancel_requested = true, updated_at = now(),
|
||||
state = CASE WHEN state IN ('queued', 'waiting', 'parked', 'waiting_approval') THEN 'cancelled' ELSE state END,
|
||||
finished_at = CASE WHEN state IN ('queued', 'waiting', 'parked', 'waiting_approval') THEN now() ELSE finished_at END
|
||||
WHERE id = $1 AND state NOT IN ('succeeded', 'failed', 'cancelled', 'declined')`, id); err != nil {
|
||||
WHERE id IN (SELECT id FROM tree) AND state NOT IN ('succeeded', 'failed', 'cancelled', 'declined')`,
|
||||
id, cancelDepth); err != nil {
|
||||
return fmt.Errorf("cancel run: %w", err)
|
||||
}
|
||||
if err := signalParent(ctx, tx, id); err != nil {
|
||||
|
||||
Reference in new issue
Block a user