Skip to content

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

chore: improve the query for stage blocked (#4492)

Danny Xu817b98308987
diff --git a/backend/legacyapi/task.go b/backend/legacyapi/task.go
index b6a8de5549..a31f815c42 100644
--- a/backend/legacyapi/task.go
+++ b/backend/legacyapi/task.go
@@ -241,8 +241,7 @@ type Task struct {
// We use string here to workaround jsonapi limitations. https://github.com/google/jsonapi/issues/209
BlockedBy []string `jsonapi:"attr,blockedBy"`
// Progress is loaded from the task scheduler in memory, NOT from the database
- Progress Progress `jsonapi:"attr,progress"`
- StageBlocked bool
+ Progress Progress `jsonapi:"attr,progress"`
}
// Progress is a generalized struct which can track the progress of a task.
@@ -303,7 +302,8 @@ type TaskFind struct {
TypeList *[]TaskType
// Payload contains JSONB expressions
// Ref: https://www.postgresql.org/docs/current/functions-json.html
- Payload string
+ Payload string
+ StageBlocked *bool
}
func (find *TaskFind) String() string {
diff --git a/backend/runner/taskrun/scheduler.go b/backend/runner/taskrun/scheduler.go
index 38180dda13..1cca215e71 100644
--- a/backend/runner/taskrun/scheduler.go
+++ b/backend/runner/taskrun/scheduler.go
@@ -743,18 +743,16 @@ func (s *Scheduler) isTaskBlocked(ctx context.Context, task *store.TaskMessage)
// scheduleAutoApprovedTasks schedules tasks that are approved automatically.
func (s *Scheduler) scheduleAutoApprovedTasks(ctx context.Context) error {
taskStatusList := []api.TaskStatus{api.TaskPendingApproval}
+ blocked := false
taskList, err := s.store.ListTasks(ctx, &api.TaskFind{
- StatusList: &taskStatusList,
+ StatusList: &taskStatusList,
+ StageBlocked: &blocked,
})
if err != nil {
return err
}
for _, task := range taskList {
- if task.StageBlocked {
- continue
- }
-
instance, err := s.store.GetInstanceV2(ctx, &store.FindInstanceMessage{UID: &task.InstanceID})
if err != nil {
return err
diff --git a/backend/store/task.go b/backend/store/task.go
index 330506dea8..0cf6dec89a 100644
--- a/backend/store/task.go
+++ b/backend/store/task.go
@@ -40,7 +40,6 @@ type TaskMessage struct {
Payload string
EarliestAllowedTs int64
BlockedBy []int
- StageBlocked bool
}
func (task *TaskMessage) toTask() *api.Task {
@@ -66,7 +65,6 @@ func (task *TaskMessage) toTask() *api.Task {
Type: task.Type,
Payload: task.Payload,
EarliestAllowedTs: task.EarliestAllowedTs,
- StageBlocked: task.StageBlocked,
}
for _, block := range task.BlockedBy {
composedTask.BlockedBy = append(composedTask.BlockedBy, fmt.Sprintf("%d", block))
@@ -378,6 +376,9 @@ func (s *Store) ListTasks(ctx context.Context, find *api.TaskFind) ([]*TaskMessa
if v := find.Payload; v != "" {
where = append(where, v)
}
+ if v := find.StageBlocked; v != nil {
+ where = append(where, "(SELECT COUNT(1) > 0 FROM task as other_task WHERE other_task.pipeline_id = task.pipeline_id AND other_task.stage_id < task.stage_id AND other_task.status != 'DONE') = FALSE")
+ }
tx, err := s.db.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
if err != nil {
@@ -401,7 +402,6 @@ func (s *Store) ListTasks(ctx context.Context, find *api.TaskFind) ([]*TaskMessa
task.type,
task.payload,
task.earliest_allowed_ts,
- (SELECT COUNT(1) FROM task as other_task WHERE other_task.pipeline_id = task.pipeline_id AND other_task.stage_id < task.stage_id AND other_task.status != 'DONE') as block_count,
ARRAY_AGG (task_dag.from_task_id) blocked_by
FROM task
LEFT JOIN task_dag ON task.id = task_dag.to_task_id
@@ -418,7 +418,6 @@ func (s *Store) ListTasks(ctx context.Context, find *api.TaskFind) ([]*TaskMessa
var tasks []*TaskMessage
for rows.Next() {
task := &TaskMessage{}
- var blockCount int
var blockedBy []sql.NullInt32
if err := rows.Scan(
&task.ID,
@@ -435,14 +434,10 @@ func (s *Store) ListTasks(ctx context.Context, find *api.TaskFind) ([]*TaskMessa
&task.Type,
&task.Payload,
&task.EarliestAllowedTs,
- &blockCount,
pq.Array(&blockedBy),
); err != nil {
return nil, err
}
- if blockCount > 0 {
- task.StageBlocked = true
- }
for _, v := range blockedBy {
if v.Valid {
task.BlockedBy = append(task.BlockedBy, int(v.Int32))