Skip to content

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

Block running task if the most recent required check fails

天舟67bdce613ca3
diff --git a/api/task_check_run.go b/api/task_check_run.go
index e69dc69897..75c0296f24 100644
--- a/api/task_check_run.go
+++ b/api/task_check_run.go
@@ -129,6 +129,8 @@ type TaskCheckRunFind struct {
// Domain specific fields
StatusList *[]TaskCheckRunStatus
+ // If true, only returns the latest
+ Latest bool
}
func (find *TaskCheckRunFind) String() string {
diff --git a/server/pipeline.go b/server/pipeline.go
index 3f6169f58c..319ceaac91 100644
--- a/server/pipeline.go
+++ b/server/pipeline.go
@@ -62,7 +62,7 @@ func (s *Server) ScheduleNextTaskIfNeeded(ctx context.Context, pipeline *api.Pip
if _, err := s.TaskCheckScheduler.ScheduleCheckIfNeeded(ctx, task, api.SYSTEM_BOT_ID, skipIfAlreadyTerminated); err != nil {
return nil, err
}
- updatedTask, err := s.TaskScheduler.Schedule(context.Background(), task)
+ updatedTask, err := s.TaskScheduler.ScheduleIfNeeded(context.Background(), task)
if err != nil {
return nil, err
}
diff --git a/server/task.go b/server/task.go
index 8bc33b1055..dd65a82c3a 100644
--- a/server/task.go
+++ b/server/task.go
@@ -391,7 +391,7 @@ func (s *Server) ChangeTaskStatusWithPatch(ctx context.Context, task *api.Task,
return nil, fmt.Errorf("failed to schedule task check \"%v\" after approval", updatedTask.Name)
}
- updatedTask, err = s.TaskScheduler.Schedule(ctx, updatedTask)
+ updatedTask, err = s.TaskScheduler.ScheduleIfNeeded(ctx, updatedTask)
if err != nil {
return nil, fmt.Errorf("failed to schedule task \"%v\" after approval", updatedTask.Name)
}
diff --git a/server/task_scheduler.go b/server/task_scheduler.go
index 4ca9f4b3ab..2fff99083f 100644
--- a/server/task_scheduler.go
+++ b/server/task_scheduler.go
@@ -2,6 +2,7 @@ package server
import (
"context"
+ "encoding/json"
"fmt"
"time"
@@ -186,7 +187,26 @@ func (s *TaskScheduler) Register(taskType string, executor TaskExecutor) {
s.executors[taskType] = executor
}
-func (s *TaskScheduler) Schedule(ctx context.Context, task *api.Task) (*api.Task, error) {
+// We will schedule the task if its required check does not contain error in the latest run
+func (s *TaskScheduler) ScheduleIfNeeded(ctx context.Context, task *api.Task) (*api.Task, error) {
+ // For now, only schema update task has required task check
+ if task.Type == api.TaskDatabaseSchemaUpdate {
+ pass, err := passCheck(ctx, s.server, task, api.TaskCheckDatabaseConnect)
+ if err != nil {
+ return nil, err
+ }
+ if !pass {
+ return task, nil
+ }
+
+ pass, err = passCheck(ctx, s.server, task, api.TaskCheckDatabaseStatementSyntax)
+ if err != nil {
+ return nil, err
+ }
+ if !pass {
+ return task, nil
+ }
+ }
updatedTask, err := s.server.ChangeTaskStatus(ctx, task, api.TaskRunning, api.SYSTEM_BOT_ID)
if err != nil {
return nil, err
@@ -194,3 +214,46 @@ func (s *TaskScheduler) Schedule(ctx context.Context, task *api.Task) (*api.Task
return updatedTask, nil
}
+
+func passCheck(ctx context.Context, server *Server, task *api.Task, checkType api.TaskCheckType) (bool, error) {
+ statusList := []api.TaskCheckRunStatus{api.TaskCheckRunDone, api.TaskCheckRunFailed}
+ taskCheckRunFind := &api.TaskCheckRunFind{
+ TaskId: &task.ID,
+ Type: &checkType,
+ StatusList: &statusList,
+ Latest: true,
+ }
+
+ taskCheckRunList, err := server.TaskCheckRunService.FindTaskCheckRunList(ctx, taskCheckRunFind)
+ if err != nil {
+ return false, err
+ }
+
+ if len(taskCheckRunList) == 0 || taskCheckRunList[0].Status == api.TaskCheckRunFailed {
+ server.l.Debug("Task is waiting for check to pass",
+ zap.Int("task_id", task.ID),
+ zap.String("task_name", task.Name),
+ zap.String("task_type", string(task.Type)),
+ zap.String("task_check_type", string(api.TaskCheckDatabaseConnect)),
+ )
+ return false, nil
+ }
+
+ checkResult := &api.TaskCheckRunResultPayload{}
+ if err := json.Unmarshal([]byte(taskCheckRunList[0].Result), checkResult); err != nil {
+ return false, err
+ }
+ for _, result := range checkResult.ResultList {
+ if result.Status == api.TaskCheckStatusError {
+ server.l.Debug("Task is waiting for check to pass",
+ zap.Int("task_id", task.ID),
+ zap.String("task_name", task.Name),
+ zap.String("task_type", string(task.Type)),
+ zap.String("task_check_type", string(api.TaskCheckDatabaseConnect)),
+ )
+ return false, nil
+ }
+ }
+
+ return true, nil
+}
diff --git a/store/task_check_run.go b/store/task_check_run.go
index b6bd055455..e257e03653 100644
--- a/store/task_check_run.go
+++ b/store/task_check_run.go
@@ -269,6 +269,11 @@ func (s *TaskCheckRunService) findTaskCheckRunList(ctx context.Context, tx *sql.
where = append(where, fmt.Sprintf("`status` in (%s)", strings.Join(list, ",")))
}
+ orderAndLimit := ""
+ if find.Latest {
+ orderAndLimit = " ORDER BY updated_ts DESC LIMIT 1"
+ }
+
rows, err := tx.QueryContext(ctx, `
SELECT
id,
@@ -283,7 +288,7 @@ func (s *TaskCheckRunService) findTaskCheckRunList(ctx context.Context, tx *sql.
result,
payload
FROM task_check_run
- WHERE `+strings.Join(where, " AND "),
+ WHERE `+strings.Join(where, " AND ")+orderAndLimit,
args...,
)
if err != nil {