Skip to content

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

chore: use batch for patching task status to canceled (#3784)

Danny Xu0e7de2478aa2
diff --git a/server/runner/apprun/runner.go b/server/runner/apprun/runner.go
index 93153f28bd..f3af5eaf51 100644
--- a/server/runner/apprun/runner.go
+++ b/server/runner/apprun/runner.go
@@ -158,7 +158,7 @@ func (r *Runner) Run(ctx context.Context, wg *sync.WaitGroup) {
tasks = append(tasks, task)
}
}
- if err := r.store.BatchPatchTaskStatusToPending(ctx, externalApproval.ApproverID, taskIDList); err != nil {
+ if err := r.store.BatchPatchTaskStatus(ctx, taskIDList, api.TaskPending, externalApproval.ApproverID); err != nil {
return errors.Wrapf(err, "failed to update task status, task id list: %+v", taskIDList)
}
if err := r.activityManager.BatchCreateTaskStatusUpdateApprovalActivity(ctx, tasks, externalApproval.ApproverID, issue, stage); err != nil {
diff --git a/server/runner/taskrun/scheduler.go b/server/runner/taskrun/scheduler.go
index 00d14cdb05..e5987e7acb 100644
--- a/server/runner/taskrun/scheduler.go
+++ b/server/runner/taskrun/scheduler.go
@@ -342,38 +342,44 @@ func (s *Scheduler) Register(taskType api.TaskType, executorGetter Executor) {
// So we change their status to CANCELED before starting the scheduler.
func (s *Scheduler) ClearRunningTasks(ctx context.Context) error {
taskFind := &api.TaskFind{StatusList: &[]api.TaskStatus{api.TaskRunning}}
- runningTasks, err := s.store.FindTask(ctx, taskFind, false)
+ runningTasks, err := s.store.FindTask(ctx, taskFind, false /* returnOnErr */)
if err != nil {
return errors.Wrap(err, "failed to get running tasks")
}
+ if len(runningTasks) == 0 {
+ return nil
+ }
+
+ var taskIDs []int
+ for _, task := range runningTasks {
+ taskIDs = append(taskIDs, task.ID)
+ }
+ if err := s.store.BatchPatchTaskStatus(ctx, taskIDs, api.TaskCanceled, api.SystemBotID); err != nil {
+ return errors.Wrapf(err, "failed to change task %v's status to %s", taskIDs, api.TaskCanceled)
+ }
+
for _, task := range runningTasks {
- if _, err := s.store.PatchTaskStatus(ctx, &api.TaskStatusPatch{
- IDList: []int{task.ID},
+ // If it's a backup task, we also change the corresponding backup's status to FAILED, because the task is canceled just now.
+ if task.Type != api.TaskDatabaseBackup {
+ continue
+ }
+ // TODO(d): batch patching backup status.
+ var payload api.TaskDatabaseBackupPayload
+ if err := json.Unmarshal([]byte(task.Payload), &payload); err != nil {
+ return errors.Wrapf(err, "failed to parse the payload of backup task %d", task.ID)
+ }
+ statusFailed := string(api.BackupStatusFailed)
+ backup, err := s.store.PatchBackup(ctx, &api.BackupPatch{
+ ID: payload.BackupID,
UpdaterID: api.SystemBotID,
- Status: api.TaskCanceled,
- }); err != nil {
- return errors.Wrapf(err, "failed to change task %d's status to %s", task.ID, api.TaskFailed)
+ Status: &statusFailed,
+ })
+ if err != nil {
+ return errors.Wrapf(err, "failed to patch backup %d's status from %s to %s", payload.BackupID, api.BackupStatusPendingCreate, api.BackupStatusFailed)
}
- log.Debug(fmt.Sprintf("Changed task %d's status from RUNNING to %s", task.ID, api.TaskFailed))
- // If it's a backup task, we also change the corresponding backup's status to FAILED, because the task is canceled just now.
- if task.Type == api.TaskDatabaseBackup {
- var payload api.TaskDatabaseBackupPayload
- if err := json.Unmarshal([]byte(task.Payload), &payload); err != nil {
- return errors.Wrapf(err, "failed to parse the payload of backup task %d", task.ID)
- }
- statusFailed := string(api.BackupStatusFailed)
- backup, err := s.store.PatchBackup(ctx, &api.BackupPatch{
- ID: payload.BackupID,
- UpdaterID: api.SystemBotID,
- Status: &statusFailed,
- })
- if err != nil {
- return errors.Wrapf(err, "failed to patch backup %d's status from %s to %s", payload.BackupID, api.BackupStatusPendingCreate, api.BackupStatusFailed)
- }
- log.Debug(fmt.Sprintf("Changed backup %d's status from %s to %s", payload.BackupID, api.BackupStatusPendingCreate, api.BackupStatusFailed))
- if err := removeLocalBackupFile(s.profile.DataDir, backup); err != nil {
- log.Warn(err.Error())
- }
+ log.Debug(fmt.Sprintf("Changed backup %d's status from %s to %s", payload.BackupID, api.BackupStatusPendingCreate, api.BackupStatusFailed))
+ if err := removeLocalBackupFile(s.profile.DataDir, backup); err != nil {
+ log.Warn(err.Error())
}
}
return nil
@@ -819,12 +825,8 @@ func (s *Scheduler) cancelDependingTasks(ctx context.Context, task *api.Task) er
queue = append(queue, dag.ToTaskID)
}
}
- if _, err := s.store.PatchTaskStatus(ctx, &api.TaskStatusPatch{
- IDList: idList,
- UpdaterID: api.SystemBotID,
- Status: api.TaskCanceled,
- }); err != nil {
- return err
+ if err := s.store.BatchPatchTaskStatus(ctx, idList, api.TaskCanceled, api.SystemBotID); err != nil {
+ return errors.Wrapf(err, "failed to change task %v's status to %s", idList, api.TaskCanceled)
}
return nil
}
diff --git a/server/stage.go b/server/stage.go
index 2f0f162728..7a6810d778 100644
--- a/server/stage.go
+++ b/server/stage.go
@@ -67,7 +67,7 @@ func (s *Server) registerStageRoutes(g *echo.Group) {
for _, task := range tasks {
taskIDList = append(taskIDList, task.ID)
}
- if err := s.store.BatchPatchTaskStatusToPending(ctx, currentPrincipalID, taskIDList); err != nil {
+ if err := s.store.BatchPatchTaskStatus(ctx, taskIDList, api.TaskPending, currentPrincipalID); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to update task %q status", taskIDList)).SetInternal(err)
}
issue, err := s.store.GetIssueByPipelineID(ctx, tasks[0].PipelineID)
diff --git a/store/task.go b/store/task.go
index e033b6e303..32050be966 100644
--- a/store/task.go
+++ b/store/task.go
@@ -182,8 +182,8 @@ func (s *Store) PatchTaskStatus(ctx context.Context, patch *api.TaskStatusPatch)
return taskList, nil
}
-// BatchPatchTaskStatusToPending patches status to pending for a list of tasks.
-func (s *Store) BatchPatchTaskStatusToPending(ctx context.Context, updaterID int, taskIDs []int) error {
+// BatchPatchTaskStatus patches status for a list of tasks.
+func (s *Store) BatchPatchTaskStatus(ctx context.Context, taskIDs []int, status api.TaskStatus, updaterID int) error {
var ids []string
for _, id := range taskIDs {
ids = append(ids, fmt.Sprintf("%d", id))
@@ -193,7 +193,7 @@ func (s *Store) BatchPatchTaskStatusToPending(ctx context.Context, updaterID int
SET status = $1, updater_id = $2
WHERE id IN (%s);
`, strings.Join(ids, ","))
- if _, err := s.db.db.ExecContext(ctx, query, api.TaskPending, updaterID); err != nil {
+ if _, err := s.db.db.ExecContext(ctx, query, status, updaterID); err != nil {
return FormatError(err)
}
return nil