Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

feat: cancel every task depending on the canceled task (#3005)

p0ny4f9541484d67
diff --git a/api/task_dag.go b/api/task_dag.go
index ae3539cdf3..9fd42b0656 100644
--- a/api/task_dag.go
+++ b/api/task_dag.go
@@ -26,7 +26,8 @@ type TaskDAGCreate struct {
// TaskDAGFind is the API message to find TaskDAG.
type TaskDAGFind struct {
- ToTaskID int
+ FromTaskID *int
+ ToTaskID *int
}
// TaskIndexDAG describes task dependency relationship using array index to represent task.
diff --git a/server/task.go b/server/task.go
index f7d4fc851e..fb5776c5b3 100644
--- a/server/task.go
+++ b/server/task.go
@@ -769,6 +769,13 @@ func (s *Server) patchTaskStatus(ctx context.Context, task *api.Task, taskStatus
}
}
+ // Cancel every task depending on the canceled task.
+ if taskPatched.Status == api.TaskCanceled {
+ if err := s.cancelDependingTasks(ctx, taskPatched); err != nil {
+ return nil, errors.Wrapf(err, "failed to cancel depending tasks for task %d", taskPatched.ID)
+ }
+ }
+
// If every task in the pipeline completes, and the assignee is system bot:
// Case 1: If the task is associated with an issue, then we mark the issue (including the pipeline) as DONE.
// Case 2: If the task is NOT associated with an issue, then we mark the pipeline as DONE.
@@ -890,3 +897,32 @@ func areAllTasksDone(pipeline *api.Pipeline) bool {
}
return true
}
+
+func (s *Server) cancelDependingTasks(ctx context.Context, task *api.Task) error {
+ queue := []int{task.ID}
+ seen := map[int]bool{task.ID: true}
+ for len(queue) != 0 {
+ fromTaskID := queue[0]
+ queue = queue[1:]
+ dagList, err := s.store.FindTaskDAGList(ctx, &api.TaskDAGFind{FromTaskID: &fromTaskID})
+ if err != nil {
+ return err
+ }
+ for _, dag := range dagList {
+ if seen[dag.ToTaskID] {
+ return errors.Errorf("found a cycle in task dag, visit task %v twice", dag.ToTaskID)
+ }
+ seen[dag.ToTaskID] = true
+
+ if _, err := s.store.PatchTaskStatus(ctx, &api.TaskStatusPatch{
+ ID: dag.ToTaskID,
+ UpdaterID: api.SystemBotID,
+ Status: api.TaskCanceled,
+ }); err != nil {
+ return err
+ }
+ queue = append(queue, dag.ToTaskID)
+ }
+ }
+ return nil
+}
diff --git a/store/task.go b/store/task.go
index 592d8007c1..4754c6457c 100644
--- a/store/task.go
+++ b/store/task.go
@@ -243,7 +243,7 @@ func (s *Store) composeTask(ctx context.Context, raw *taskRaw) (*api.Task, error
}
blockedBy := []string{}
- taskDAGList, err := s.FindTaskDAGList(ctx, &api.TaskDAGFind{ToTaskID: raw.ID})
+ taskDAGList, err := s.FindTaskDAGList(ctx, &api.TaskDAGFind{ToTaskID: &raw.ID})
if err != nil {
return nil, err
}
diff --git a/store/task_dag.go b/store/task_dag.go
index 21f9312fda..5da9f06c18 100644
--- a/store/task_dag.go
+++ b/store/task_dag.go
@@ -3,6 +3,8 @@ package store
import (
"context"
"database/sql"
+ "fmt"
+ "strings"
"github.com/pkg/errors"
@@ -59,7 +61,7 @@ func (s *Store) FindTaskDAGList(ctx context.Context, find *api.TaskDAGFind) ([]*
// GetTaskDAGByToTaskID gets a single TaskDAG by ToTaskID.
func (s *Store) GetTaskDAGByToTaskID(ctx context.Context, id int) (*api.TaskDAG, error) {
- taskDAGList, err := s.FindTaskDAGList(ctx, &api.TaskDAGFind{ToTaskID: id})
+ taskDAGList, err := s.FindTaskDAGList(ctx, &api.TaskDAGFind{ToTaskID: &id})
if err != nil {
return nil, err
}
@@ -132,6 +134,13 @@ func createTaskDAGImpl(ctx context.Context, tx *Tx, create *api.TaskDAGCreate) (
}
func findTaskDAGRawListImpl(ctx context.Context, tx *Tx, find *api.TaskDAGFind) ([]*taskDAGRaw, error) {
+ where, args := []string{"1 = 1"}, []interface{}{}
+ if v := find.FromTaskID; v != nil {
+ where, args = append(where, fmt.Sprintf("from_task_id = $%d", len(args)+1)), append(args, *v)
+ }
+ if v := find.ToTaskID; v != nil {
+ where, args = append(where, fmt.Sprintf("to_task_id = $%d", len(args)+1)), append(args, *v)
+ }
rows, err := tx.QueryContext(ctx, `
SELECT
id,
@@ -141,8 +150,9 @@ func findTaskDAGRawListImpl(ctx context.Context, tx *Tx, find *api.TaskDAGFind)
to_task_id,
payload
FROM task_dag
- WHERE to_task_id = $1
- `, find.ToTaskID)
+ WHERE `+strings.Join(where, " AND "),
+ args...,
+ )
if err != nil {
return nil, FormatError(err)
}