Skip to content

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

feat: task scheduler support task dependency (#1168)

p0ny37a0cbfea6b6
diff --git a/server/task_scheduler.go b/server/task_scheduler.go
index 4e63b1fdfc..cedd0e2313 100644
--- a/server/task_scheduler.go
+++ b/server/task_scheduler.go
@@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
+ "strconv"
"sync"
"time"
@@ -132,6 +133,17 @@ func (s *TaskScheduler) Run(ctx context.Context, wg *sync.WaitGroup) {
)
continue
}
+ // Skip execution if has any dependency not finished.
+ isBlocked, err := s.isTaskBlocked(ctx, task)
+ if err != nil {
+ s.l.Error("failed to check if task is blocked",
+ zap.Int("id", task.ID),
+ zap.Error(err))
+ continue
+ }
+ if isBlocked {
+ continue
+ }
tasks.mu.Lock()
if _, ok := tasks.running[task.ID]; ok {
@@ -306,3 +318,23 @@ func (s *TaskScheduler) ScheduleIfNeeded(ctx context.Context, task *api.Task) (*
return updatedTask, nil
}
+
+func (s *TaskScheduler) isTaskBlocked(ctx context.Context, task *api.Task) (bool, error) {
+ for _, blockingTaskIDString := range task.BlockedBy {
+ blockingTaskID, err := strconv.Atoi(blockingTaskIDString)
+ if err != nil {
+ return true, fmt.Errorf("failed to convert id string to int, id string: %v, error: %w", blockingTaskIDString, err)
+ }
+ taskFind := api.TaskFind{
+ ID: &blockingTaskID,
+ }
+ blockingTask, err := s.server.TaskService.FindTask(ctx, &taskFind)
+ if err != nil {
+ return true, fmt.Errorf("failed to fetch the blocking task, id: %v, error: %w", blockingTaskID, err)
+ }
+ if blockingTask.Status != api.TaskDone {
+ return true, nil
+ }
+ }
+ return false, nil
+}