Skip to content

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

fix(timing): task executes after earliest allowed time (#2196)

p0ny2218324acb6f
diff --git a/server/pipeline.go b/server/pipeline.go
index eef988fd6a..8eeeb47daa 100644
--- a/server/pipeline.go
+++ b/server/pipeline.go
@@ -29,11 +29,8 @@ func (s *Server) ScheduleNextTaskIfNeeded(ctx context.Context, pipeline *api.Pip
return nil, fmt.Errorf("failed to get approval policy for environment ID %d, error: %w", task.Instance.EnvironmentID, err)
}
if policy.Value == api.PipelineApprovalValueManualNever {
- // transit into Pending for ManualNever (auto-approval) tasks.
- // Requirements:
- // 1. has no blocking tasks.
- // 2. task check results are all SUCCESS.
- ok, err := s.TaskScheduler.canTransitTaskStatus(ctx, task, api.TaskCheckStatusSuccess)
+ // transit into Pending for ManualNever (auto-approval) tasks if all required task checks passed.
+ ok, err := s.TaskScheduler.canAutoApprove(ctx, task)
if err != nil {
return nil, err
}
diff --git a/server/task.go b/server/task.go
index f767573f79..030b27be93 100644
--- a/server/task.go
+++ b/server/task.go
@@ -40,12 +40,12 @@ func (s *Server) canUpdateTaskStatement(ctx context.Context, task *api.Task) *ec
// Allow frontend to change the SQL statement of
// 1. a PendingApproval task which hasn't started yet
// 2. a Failed task which can be retried
- // 3. a Pending task which can't be scheduled because of failed task checks
+ // 3. a Pending task which can't be scheduled because of failed task checks, task dependency or earliest allowed time
if task.Status != api.TaskPendingApproval && task.Status != api.TaskFailed && task.Status != api.TaskPending {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("can not update task in %q state", task.Status))
}
if task.Status == api.TaskPending {
- ok, err := s.TaskScheduler.canTransitTaskStatus(ctx, task, api.TaskCheckStatusWarn)
+ ok, err := s.TaskScheduler.canSchedule(ctx, task)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "failed to check whether the task can be scheduled").SetInternal(err)
}
diff --git a/server/task_check_executor_timing.go b/server/task_check_executor_timing.go
index 38fccbe8f5..0a3c055b26 100644
--- a/server/task_check_executor_timing.go
+++ b/server/task_check_executor_timing.go
@@ -35,7 +35,7 @@ func (*TaskCheckTimingExecutor) Run(_ context.Context, _ *Server, taskCheckRun *
Namespace: api.BBNamespace,
Code: common.Ok.Int(),
Title: "OK",
- Content: "Earliest allowed time unset",
+ Content: "Earliest allowed time is not set",
},
}, nil
}
@@ -44,7 +44,7 @@ func (*TaskCheckTimingExecutor) Run(_ context.Context, _ *Server, taskCheckRun *
if time.Now().UTC().Before(time.Unix(payload.EarliestAllowedTs, 0).UTC()) {
return []api.TaskCheckResult{
{
- Status: api.TaskCheckStatusError,
+ Status: api.TaskCheckStatusWarn,
Namespace: api.BBNamespace,
Code: common.TaskTimingNotAllowed.Int(),
Title: "Not ready to run",
diff --git a/server/task_check_scheduler.go b/server/task_check_scheduler.go
index c83e5c8775..0c254c9271 100644
--- a/server/task_check_scheduler.go
+++ b/server/task_check_scheduler.go
@@ -178,7 +178,7 @@ func (s *TaskCheckScheduler) Register(taskType api.TaskCheckType, executor TaskC
}
// Returns true if we meet either of the following conditions:
-// 1. Task has a non-default value and no task check has run before (so we are about to kick of the check the first time)
+// 1. Task has a non-default value and no task check has run before (so we are about to kick off the check for the first time)
// 2. The specified EarliestAllowedTs has elapsed, so we need to rerun the check to unblock the task.
// On the other hand, we would also rerun the check if user has modified EarliestAllowedTs. This is handled separately in the task patch handler.
func (s *TaskCheckScheduler) shouldScheduleTimingTaskCheck(ctx context.Context, task *api.Task, forceSchedule bool) (bool, error) {
diff --git a/server/task_scheduler.go b/server/task_scheduler.go
index 13f2d29c0e..1d97e0a5b9 100644
--- a/server/task_scheduler.go
+++ b/server/task_scheduler.go
@@ -231,25 +231,7 @@ func (s *TaskScheduler) Register(taskType api.TaskType, executorGetter func() Ta
s.executorGetters[taskType] = executorGetter
}
-func (s *TaskScheduler) canTransitTaskStatus(ctx context.Context, task *api.Task, allowedStatus api.TaskCheckStatus) (bool, error) {
- blocked, err := s.isTaskBlocked(ctx, task)
- if err != nil {
- return false, fmt.Errorf("failed to check if task is blocked, error: %w", err)
- }
- if blocked {
- return false, nil
- }
- // timing task check
- if task.EarliestAllowedTs != 0 {
- pass, err := s.server.passCheck(ctx, task, api.TaskCheckGeneralEarliestAllowedTime, allowedStatus)
- if err != nil {
- return false, err
- }
- if !pass {
- return false, nil
- }
- }
-
+func (s *TaskScheduler) passAllCheck(ctx context.Context, task *api.Task, allowedStatus api.TaskCheckStatus) (bool, error) {
// schema update, data update and gh-ost sync task have required task check.
if task.Type == api.TaskDatabaseSchemaUpdate || task.Type == api.TaskDatabaseDataUpdate || task.Type == api.TaskDatabaseSchemaUpdateGhostSync {
pass, err := s.server.passCheck(ctx, task, api.TaskCheckDatabaseConnect, allowedStatus)
@@ -320,11 +302,39 @@ func (s *TaskScheduler) canTransitTaskStatus(ctx context.Context, task *api.Task
return true, nil
}
+// auto transit PendingApproval to Pending if all required task checks pass.
+func (s *TaskScheduler) canAutoApprove(ctx context.Context, task *api.Task) (bool, error) {
+ return s.passAllCheck(ctx, task, api.TaskCheckStatusSuccess)
+}
+
+func (s *TaskScheduler) canSchedule(ctx context.Context, task *api.Task) (bool, error) {
+ blocked, err := s.isTaskBlocked(ctx, task)
+ if err != nil {
+ return false, fmt.Errorf("failed to check if task is blocked, error: %w", err)
+ }
+ if blocked {
+ return false, nil
+ }
+ // timing task check
+ if task.EarliestAllowedTs != 0 {
+ pass, err := s.server.passCheck(ctx, task, api.TaskCheckGeneralEarliestAllowedTime, api.TaskCheckStatusSuccess)
+ if err != nil {
+ return false, err
+ }
+ if !pass {
+ return false, nil
+ }
+ }
+
+ return s.passAllCheck(ctx, task, api.TaskCheckStatusWarn)
+}
+
// ScheduleIfNeeded schedules the task if
-// 1. its required check does not contain error in the latest run
-// 2. it has no blocking tasks.
+// 1. its required check does not contain error in the latest run.
+// 2. it has no blocking tasks.
+// 3. it has passed the earliest allowed time.
func (s *TaskScheduler) ScheduleIfNeeded(ctx context.Context, task *api.Task) (*api.Task, error) {
- schedule, err := s.canTransitTaskStatus(ctx, task, api.TaskCheckStatusWarn)
+ schedule, err := s.canSchedule(ctx, task)
if err != nil {
return nil, err
}