|
| 1 | +-- Terminalize permanently stalled tasks through the step's when_exhausted |
| 2 | +-- policy (#689). requeue_stalled_tasks() archives the message and stamps |
| 3 | +-- permanently_stalled_at but leaves the task nonterminal; this function hands |
| 4 | +-- each marked task to fail_task(), which treats the marker as exhaustion and |
| 5 | +-- decides whether the run fails, the step skips, dependents start, and |
| 6 | +-- siblings are cancelled or archived. |
| 7 | +-- |
| 8 | +-- Lock order matches fail_task(): lock the run and step first, then the task, |
| 9 | +-- so recovery (which locks step_tasks first) and live workers cannot deadlock. |
| 10 | +-- The candidate loop is a deliberate exception to the declarative-first SQL |
| 11 | +-- style: each candidate needs its own lock-and-recheck sequence before |
| 12 | +-- fail_task() runs, which a set operation cannot interleave. |
| 13 | +-- Runs as its own cron job, in a separate transaction from recovery, so |
| 14 | +-- recovery's locks are released before escalation begins. Safe to repeat: |
| 15 | +-- candidates are rechecked under lock, and rows already terminalized by |
| 16 | +-- fail_task() lose their nonterminal status and stop matching. |
| 17 | +create or replace function pgflow.escalate_permanently_stalled() |
| 18 | +returns int |
| 19 | +language plpgsql |
| 20 | +security definer |
| 21 | +set search_path = '' |
| 22 | +as $$ |
| 23 | +declare |
| 24 | + v_candidate record; |
| 25 | + v_run_status text; |
| 26 | + v_step_status text; |
| 27 | + v_task_status text; |
| 28 | + v_stalled_at timestamptz; |
| 29 | + v_escalated int := 0; |
| 30 | +begin |
| 31 | + -- Pick up every marked nonterminal task, including marked queued leftovers |
| 32 | + -- requeued by older callbacks and rows stamped by earlier deployments |
| 33 | + for v_candidate in |
| 34 | + select |
| 35 | + st.run_id, |
| 36 | + st.step_slug, |
| 37 | + st.task_index |
| 38 | + from pgflow.step_tasks st |
| 39 | + where st.status in ('started', 'queued') |
| 40 | + and st.permanently_stalled_at is not null |
| 41 | + order by st.run_id, st.step_slug, st.task_index |
| 42 | + loop |
| 43 | + -- Lock and recheck the run and step: if either left 'started', that |
| 44 | + -- transition owns the task's terminal state and escalation must not |
| 45 | + -- fight it |
| 46 | + select r.status, step_state.status into v_run_status, v_step_status |
| 47 | + from pgflow.runs r |
| 48 | + join pgflow.step_states step_state on step_state.run_id = r.run_id |
| 49 | + where r.run_id = v_candidate.run_id |
| 50 | + and step_state.step_slug = v_candidate.step_slug |
| 51 | + for update of r, step_state; |
| 52 | + |
| 53 | + if v_run_status = 'started' and v_step_status = 'started' then |
| 54 | + -- Lock and recheck the task: recovery or a live callback may have |
| 55 | + -- transitioned it while this pass waited for the run and step locks |
| 56 | + select st.status, st.permanently_stalled_at |
| 57 | + into v_task_status, v_stalled_at |
| 58 | + from pgflow.step_tasks st |
| 59 | + where st.run_id = v_candidate.run_id |
| 60 | + and st.step_slug = v_candidate.step_slug |
| 61 | + and st.task_index = v_candidate.task_index |
| 62 | + for update; |
| 63 | + |
| 64 | + if v_task_status in ('started', 'queued') and v_stalled_at is not null then |
| 65 | + -- fail_task() treats the marker as exhaustion and applies |
| 66 | + -- when_exhausted; attempts_count stays untouched (#689) |
| 67 | + perform pgflow.fail_task( |
| 68 | + run_id => v_candidate.run_id, |
| 69 | + step_slug => v_candidate.step_slug, |
| 70 | + task_index => v_candidate.task_index, |
| 71 | + error_message => 'permanently stalled: worker did not report after 3 requeues' |
| 72 | + ); |
| 73 | + v_escalated := v_escalated + 1; |
| 74 | + end if; |
| 75 | + end if; |
| 76 | + end loop; |
| 77 | + |
| 78 | + return v_escalated; |
| 79 | +end; |
| 80 | +$$; |
0 commit comments