Give each workflow run a parent task with no agent
This commit is contained in:
@@ -160,7 +160,10 @@ func (s *Store) FreezeWorkflow(ctx context.Context, name string) (FrozenWorkflow
|
||||
func (s *Store) TaskWorkflow(ctx context.Context, taskID string) (FrozenWorkflow, error) {
|
||||
f := FrozenWorkflow{}
|
||||
defID := sql.NullString{}
|
||||
err := s.db.QueryRowContext(ctx, `SELECT workflow_name, workflow_definition_id, workflow_brief, workflow_title, audit FROM tasks WHERE id = ?`, taskID).Scan(&f.Name, &defID, &f.Brief, &f.Title, &f.Audit)
|
||||
err := s.db.QueryRowContext(ctx, `
|
||||
SELECT r.workflow_name, r.workflow_definition_id, r.workflow_brief, r.workflow_title, r.audit
|
||||
FROM tasks t JOIN tasks r ON r.id = CASE WHEN t.workflow_stage != '' THEN t.parent_task_id ELSE t.id END
|
||||
WHERE t.id = ?`, taskID).Scan(&f.Name, &defID, &f.Brief, &f.Title, &f.Audit)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return FrozenWorkflow{}, fmt.Errorf("%w: no task %s", ErrNotFound, taskID)
|
||||
}
|
||||
@@ -187,13 +190,13 @@ func (s *Store) LastAudit(ctx context.Context, projectID string) (string, bool,
|
||||
}
|
||||
|
||||
func (s *Store) RunTasks(ctx context.Context, run string) ([]api.Task, error) {
|
||||
return s.queryTasks(ctx, `SELECT `+taskColumns+` FROM tasks WHERE workflow_run = ? ORDER BY created_at`, run)
|
||||
return s.queryTasks(ctx, `SELECT `+taskColumns+` FROM tasks WHERE parent_task_id = ? AND workflow_stage != '' ORDER BY created_at`, run)
|
||||
}
|
||||
|
||||
func (s *Store) RunEvents(ctx context.Context, run string) ([]api.Event, error) {
|
||||
rows, err := s.db.QueryContext(ctx, `
|
||||
SELECT `+eventColumns+` FROM events
|
||||
WHERE task_id IN (SELECT id FROM tasks WHERE workflow_run = ?) AND kind IN ('task.workflow_stage', 'task.discarded', 'task.workflow_finished')
|
||||
WHERE task_id IN (SELECT id FROM tasks WHERE parent_task_id = ?1 AND workflow_stage != '') AND kind IN ('task.workflow_stage', 'task.discarded')
|
||||
ORDER BY seq
|
||||
`, run)
|
||||
if err != nil {
|
||||
@@ -283,7 +286,7 @@ func clearStage(ctx context.Context, tx *sql.Tx, sessionID string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *Store) FinishWorkflow(ctx context.Context, task api.Task, sessionID, workflowName, output string, kept []string) error {
|
||||
func (s *Store) FinishWorkflow(ctx context.Context, run api.Task, sessionID, workflowName, output string, kept []string) error {
|
||||
return s.write(ctx, func(tx *sql.Tx, emit emitFunc) error {
|
||||
err := clearStage(ctx, tx, sessionID)
|
||||
if err != nil {
|
||||
@@ -291,16 +294,16 @@ func (s *Store) FinishWorkflow(ctx context.Context, task api.Task, sessionID, wo
|
||||
}
|
||||
return emit(api.Event{
|
||||
Kind: "task.workflow_finished",
|
||||
ProjectId: &task.ProjectId,
|
||||
TaskId: &task.Id,
|
||||
ProjectId: &run.ProjectId,
|
||||
TaskId: &run.Id,
|
||||
Actor: "byre",
|
||||
Payload: map[string]any{"id": task.Id, "workflow": workflowName, "output": output, "kept": kept},
|
||||
Payload: map[string]any{"id": run.Id, "workflow": workflowName, "output": output, "kept": kept},
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func (s *Store) WorkflowFinished(ctx context.Context, run string) (bool, error) {
|
||||
finished := false
|
||||
err := s.db.QueryRowContext(ctx, `SELECT EXISTS (SELECT 1 FROM events e JOIN tasks t ON t.id = e.task_id WHERE t.workflow_run = ? AND e.kind = 'task.workflow_finished')`, run).Scan(&finished)
|
||||
err := s.db.QueryRowContext(ctx, `SELECT EXISTS (SELECT 1 FROM events WHERE task_id = ? AND kind = 'task.workflow_finished')`, run).Scan(&finished)
|
||||
return finished, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user