|
| 1 | +-- Modify "start_ready_steps" function |
| 2 | +CREATE OR REPLACE FUNCTION "pgflow"."start_ready_steps" ("run_id" uuid) RETURNS void LANGUAGE sql SET "search_path" = '' AS $$ |
| 3 | +-- First handle empty array map steps (initial_tasks = 0) - direct transition to completed |
| 4 | +WITH empty_map_steps AS ( |
| 5 | + SELECT step_state.* |
| 6 | + FROM pgflow.step_states AS step_state |
| 7 | + JOIN pgflow.steps AS step |
| 8 | + ON step.flow_slug = step_state.flow_slug |
| 9 | + AND step.step_slug = step_state.step_slug |
| 10 | + WHERE step_state.run_id = start_ready_steps.run_id |
| 11 | + AND step_state.status = 'created' |
| 12 | + AND step_state.remaining_deps = 0 |
| 13 | + AND step.step_type = 'map' |
| 14 | + AND step_state.initial_tasks = 0 |
| 15 | + ORDER BY step_state.step_slug |
| 16 | + FOR UPDATE OF step_state |
| 17 | +), |
| 18 | +completed_empty_steps AS ( |
| 19 | + UPDATE pgflow.step_states |
| 20 | + SET status = 'completed', |
| 21 | + started_at = now(), |
| 22 | + completed_at = now(), |
| 23 | + remaining_tasks = 0 |
| 24 | + FROM empty_map_steps |
| 25 | + WHERE pgflow.step_states.run_id = start_ready_steps.run_id |
| 26 | + AND pgflow.step_states.step_slug = empty_map_steps.step_slug |
| 27 | + RETURNING pgflow.step_states.* |
| 28 | +), |
| 29 | +broadcast_empty_completed AS ( |
| 30 | + SELECT |
| 31 | + realtime.send( |
| 32 | + jsonb_build_object( |
| 33 | + 'event_type', 'step:completed', |
| 34 | + 'run_id', completed_step.run_id, |
| 35 | + 'step_slug', completed_step.step_slug, |
| 36 | + 'status', 'completed', |
| 37 | + 'started_at', completed_step.started_at, |
| 38 | + 'completed_at', completed_step.completed_at, |
| 39 | + 'remaining_tasks', 0, |
| 40 | + 'remaining_deps', 0, |
| 41 | + 'output', '[]'::jsonb |
| 42 | + ), |
| 43 | + concat('step:', completed_step.step_slug, ':completed'), |
| 44 | + concat('pgflow:run:', completed_step.run_id), |
| 45 | + false |
| 46 | + ) |
| 47 | + FROM completed_empty_steps AS completed_step |
| 48 | +), |
| 49 | + |
| 50 | +-- Now handle non-empty steps (both single and map with initial_tasks > 0) |
| 51 | +ready_steps AS ( |
| 52 | + SELECT * |
| 53 | + FROM pgflow.step_states AS step_state |
| 54 | + WHERE step_state.run_id = start_ready_steps.run_id |
| 55 | + AND step_state.status = 'created' |
| 56 | + AND step_state.remaining_deps = 0 |
| 57 | + -- Exclude empty map steps already handled |
| 58 | + AND NOT EXISTS ( |
| 59 | + SELECT 1 FROM empty_map_steps |
| 60 | + WHERE empty_map_steps.run_id = step_state.run_id |
| 61 | + AND empty_map_steps.step_slug = step_state.step_slug |
| 62 | + ) |
| 63 | + ORDER BY step_state.step_slug |
| 64 | + FOR UPDATE |
| 65 | +), |
| 66 | +started_step_states AS ( |
| 67 | + UPDATE pgflow.step_states |
| 68 | + SET status = 'started', |
| 69 | + started_at = now(), |
| 70 | + remaining_tasks = ready_steps.initial_tasks -- Copy initial_tasks to remaining_tasks when starting |
| 71 | + FROM ready_steps |
| 72 | + WHERE pgflow.step_states.run_id = start_ready_steps.run_id |
| 73 | + AND pgflow.step_states.step_slug = ready_steps.step_slug |
| 74 | + RETURNING pgflow.step_states.* |
| 75 | +), |
| 76 | + |
| 77 | +-- Generate tasks based on initial_tasks count |
| 78 | +-- For single steps: initial_tasks = 1, so generate_series(0, 0) = single task with index 0 |
| 79 | +-- For map steps: initial_tasks = N, so generate_series(0, N-1) = N tasks with indices 0..N-1 |
| 80 | +-- Group messages by step for batch sending |
| 81 | +message_batches AS ( |
| 82 | + SELECT |
| 83 | + started_step.flow_slug, |
| 84 | + started_step.run_id, |
| 85 | + started_step.step_slug, |
| 86 | + COALESCE(step.opt_start_delay, 0) as delay, |
| 87 | + array_agg( |
| 88 | + jsonb_build_object( |
| 89 | + 'flow_slug', started_step.flow_slug, |
| 90 | + 'run_id', started_step.run_id, |
| 91 | + 'step_slug', started_step.step_slug, |
| 92 | + 'task_index', task_idx.task_index |
| 93 | + ) ORDER BY task_idx.task_index |
| 94 | + ) AS messages, |
| 95 | + array_agg(task_idx.task_index ORDER BY task_idx.task_index) AS task_indices |
| 96 | + FROM started_step_states AS started_step |
| 97 | + JOIN pgflow.steps AS step |
| 98 | + ON step.flow_slug = started_step.flow_slug |
| 99 | + AND step.step_slug = started_step.step_slug |
| 100 | + -- Generate task indices from 0 to initial_tasks-1 |
| 101 | + CROSS JOIN LATERAL generate_series(0, started_step.initial_tasks - 1) AS task_idx(task_index) |
| 102 | + GROUP BY started_step.flow_slug, started_step.run_id, started_step.step_slug, step.opt_start_delay |
| 103 | +), |
| 104 | +-- Send messages in batch for better performance with large arrays |
| 105 | +sent_messages AS ( |
| 106 | + SELECT |
| 107 | + mb.flow_slug, |
| 108 | + mb.run_id, |
| 109 | + mb.step_slug, |
| 110 | + task_indices.task_index, |
| 111 | + msg_ids.msg_id |
| 112 | + FROM message_batches mb |
| 113 | + CROSS JOIN LATERAL unnest(mb.task_indices) WITH ORDINALITY AS task_indices(task_index, idx_ord) |
| 114 | + CROSS JOIN LATERAL pgmq.send_batch(mb.flow_slug, mb.messages, mb.delay) WITH ORDINALITY AS msg_ids(msg_id, msg_ord) |
| 115 | + WHERE task_indices.idx_ord = msg_ids.msg_ord |
| 116 | +), |
| 117 | + |
| 118 | +broadcast_events AS ( |
| 119 | + SELECT |
| 120 | + realtime.send( |
| 121 | + jsonb_build_object( |
| 122 | + 'event_type', 'step:started', |
| 123 | + 'run_id', started_step.run_id, |
| 124 | + 'step_slug', started_step.step_slug, |
| 125 | + 'status', 'started', |
| 126 | + 'started_at', started_step.started_at, |
| 127 | + 'remaining_tasks', started_step.remaining_tasks, |
| 128 | + 'remaining_deps', started_step.remaining_deps |
| 129 | + ), |
| 130 | + concat('step:', started_step.step_slug, ':started'), |
| 131 | + concat('pgflow:run:', started_step.run_id), |
| 132 | + false |
| 133 | + ) |
| 134 | + FROM started_step_states AS started_step |
| 135 | +) |
| 136 | + |
| 137 | +-- Insert all generated tasks with their respective task_index values |
| 138 | +INSERT INTO pgflow.step_tasks (flow_slug, run_id, step_slug, task_index, message_id) |
| 139 | +SELECT |
| 140 | + sent_messages.flow_slug, |
| 141 | + sent_messages.run_id, |
| 142 | + sent_messages.step_slug, |
| 143 | + sent_messages.task_index, |
| 144 | + sent_messages.msg_id |
| 145 | +FROM sent_messages; |
| 146 | +$$; |
0 commit comments