Skip to content

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

feat: batch create task runs (#17339)

p0nyd489575fb67a
diff --git a/backend/api/v1/rollout_service.go b/backend/api/v1/rollout_service.go
index 5af1495eef..7cb46afc71 100644
--- a/backend/api/v1/rollout_service.go
+++ b/backend/api/v1/rollout_service.go
@@ -704,8 +704,7 @@ func (s *RolloutService) BatchRunTasks(ctx context.Context, req *connect.Request
}
create := &store.TaskRunMessage{
- TaskUID: task.ID,
- CreatorID: user.ID,
+ TaskUID: task.ID,
}
if task.Payload.GetSheetId() != 0 {
sheetUID := int(task.Payload.GetSheetId())
@@ -721,7 +720,7 @@ func (s *RolloutService) BatchRunTasks(ctx context.Context, req *connect.Request
return a.TaskUID - b.TaskUID
})
- if err := s.store.CreatePendingTaskRuns(ctx, taskRunCreates...); err != nil {
+ if err := s.store.CreatePendingTaskRuns(ctx, user.ID, taskRunCreates...); err != nil {
return nil, connect.NewError(connect.CodeInternal, errors.Errorf("failed to create pending task runs, error %v", err))
}
diff --git a/backend/runner/taskrun/schedulerv2.go b/backend/runner/taskrun/schedulerv2.go
index cba507dbaa..1d76bd98b9 100644
--- a/backend/runner/taskrun/schedulerv2.go
+++ b/backend/runner/taskrun/schedulerv2.go
@@ -237,7 +237,7 @@ func (s *SchedulerV2) scheduleAutoRolloutTask(ctx context.Context, taskUID int)
create.SheetUID = &sheetUID
}
- if err := s.store.CreatePendingTaskRuns(ctx, create); err != nil {
+ if err := s.store.CreatePendingTaskRuns(ctx, common.SystemBotID, create); err != nil {
return errors.Wrapf(err, "failed to create pending task runs")
}
diff --git a/backend/store/task_run.go b/backend/store/task_run.go
index b0016d5b5d..cf04b6a4a5 100644
--- a/backend/store/task_run.go
+++ b/backend/store/task_run.go
@@ -249,7 +249,7 @@ func (s *Store) UpdateTaskRunStartAt(ctx context.Context, taskRunID int) error {
}
// CreatePendingTaskRuns creates pending task runs.
-func (s *Store) CreatePendingTaskRuns(ctx context.Context, creates ...*TaskRunMessage) error {
+func (s *Store) CreatePendingTaskRuns(ctx context.Context, creatorID int, creates ...*TaskRunMessage) error {
if len(creates) == 0 {
return nil
}
@@ -282,7 +282,7 @@ func (s *Store) CreatePendingTaskRuns(ctx context.Context, creates ...*TaskRunMe
return errors.Errorf("cannot create pending task runs because there are pending/running/done task runs")
}
- if err := s.createPendingTaskRunsTx(ctx, tx, attempts, creates); err != nil {
+ if err := s.createPendingTaskRunsTx(ctx, tx, creatorID, attempts, creates); err != nil {
return errors.Wrapf(err, "failed to create pending task runs")
}
@@ -324,16 +324,44 @@ func (*Store) getTaskNextAttempt(ctx context.Context, txn *sql.Tx, taskIDs []int
return attempts, nil
}
-func (s *Store) createPendingTaskRunsTx(ctx context.Context, txn *sql.Tx, attempts []int, creates []*TaskRunMessage) error {
+func (*Store) createPendingTaskRunsTx(ctx context.Context, txn *sql.Tx, creatorID int, attempts []int, creates []*TaskRunMessage) error {
if len(attempts) != len(creates) {
return errors.Errorf("length of attempts and creates are different")
}
+ var taskUIDs []int
+ var sheetUIDs []*int
+ var runAts []*time.Time
+ for _, create := range creates {
+ taskUIDs = append(taskUIDs, create.TaskUID)
+ sheetUIDs = append(sheetUIDs, create.SheetUID)
+ runAts = append(runAts, create.RunAt)
+ }
- // TODO(p0ny): batch create.
- for i, create := range creates {
- if err := s.createTaskRunImpl(ctx, txn, create, attempts[i], storepb.TaskRun_PENDING, create.CreatorID); err != nil {
- return err
- }
+ query := `
+ INSERT INTO task_run (
+ creator_id,
+ task_id,
+ sheet_id,
+ run_at,
+ attempt,
+ status
+ ) SELECT
+ $1,
+ unnest(CAST($2 AS INTEGER[])),
+ unnest(CAST($3 AS INTEGER[])),
+ unnest(CAST($4 AS TIMESTAMPTZ[])),
+ unnest(CAST($5 AS INTEGER[])),
+ $6
+ `
+ if _, err := txn.ExecContext(ctx, query,
+ creatorID,
+ taskUIDs,
+ sheetUIDs,
+ runAts,
+ attempts,
+ storepb.TaskRun_PENDING.String(),
+ ); err != nil {
+ return errors.Wrapf(err, "failed to create pending task runs")
}
return nil
}
@@ -358,31 +386,6 @@ func (*Store) checkTaskRunsExist(ctx context.Context, txn *sql.Tx, taskIDs []int
return exist, nil
}
-// createTaskRunImpl creates a new taskRun.
-func (*Store) createTaskRunImpl(ctx context.Context, txn *sql.Tx, create *TaskRunMessage, attempt int, status storepb.TaskRun_Status, creatorID int) error {
- query := `
- INSERT INTO task_run (
- creator_id,
- task_id,
- sheet_id,
- run_at,
- attempt,
- status
- ) VALUES ($1, $2, $3, $4, $5, $6)
- `
- if _, err := txn.ExecContext(ctx, query,
- creatorID,
- create.TaskUID,
- create.SheetUID,
- create.RunAt,
- attempt,
- status.String(),
- ); err != nil {
- return err
- }
- return nil
-}
-
// patchTaskRunStatusImpl updates a taskRun status. Returns the new state of the taskRun after update.
func (*Store) patchTaskRunStatusImpl(ctx context.Context, txn *sql.Tx, patch *TaskRunStatusPatch) (*TaskRunMessage, error) {
set, args := []string{"updated_at = $1", "status = $2"}, []any{time.Now(), patch.Status.String()}