| 1 | -- SPDX-License-Identifier: AGPL-3.0-or-later |
| 2 | |
| 3 | -- name: InsertWorkflowJob :one |
| 4 | INSERT INTO workflow_jobs ( |
| 5 | run_id, job_index, job_key, job_name, |
| 6 | runs_on, needs_jobs, if_expr, timeout_minutes, |
| 7 | permissions, job_env |
| 8 | ) VALUES ( |
| 9 | $1, $2, $3, $4, $5, $6, $7, $8, $9, $10 |
| 10 | ) |
| 11 | RETURNING id, run_id, job_index, job_key, job_name, runs_on, |
| 12 | runner_id, needs_jobs, if_expr, timeout_minutes, permissions, |
| 13 | job_env, status, conclusion, cancel_requested, |
| 14 | started_at, completed_at, version, created_at, updated_at; |
| 15 | |
| 16 | -- name: GetWorkflowJobByID :one |
| 17 | SELECT id, run_id, job_index, job_key, job_name, runs_on, |
| 18 | runner_id, needs_jobs, if_expr, timeout_minutes, permissions, |
| 19 | job_env, status, conclusion, cancel_requested, |
| 20 | started_at, completed_at, version, created_at, updated_at |
| 21 | FROM workflow_jobs |
| 22 | WHERE id = $1; |
| 23 | |
| 24 | -- name: CountRunningJobsForRunner :one |
| 25 | SELECT COUNT(*)::integer |
| 26 | FROM workflow_jobs |
| 27 | WHERE runner_id = sqlc.arg(runner_id)::bigint AND status = 'running'; |
| 28 | |
| 29 | -- name: ClaimQueuedWorkflowJob :one |
| 30 | WITH candidate AS ( |
| 31 | SELECT j.id |
| 32 | FROM workflow_jobs j |
| 33 | WHERE j.status = 'queued' |
| 34 | AND j.runner_id IS NULL |
| 35 | AND (j.runs_on = '' OR j.runs_on = ANY(sqlc.arg(labels)::text[])) |
| 36 | AND NOT EXISTS ( |
| 37 | SELECT 1 |
| 38 | FROM workflow_jobs dep |
| 39 | WHERE dep.run_id = j.run_id |
| 40 | AND dep.job_key = ANY(j.needs_jobs) |
| 41 | AND (dep.status <> 'completed' OR dep.conclusion <> 'success') |
| 42 | ) |
| 43 | ORDER BY j.created_at ASC, j.id ASC |
| 44 | FOR UPDATE OF j SKIP LOCKED |
| 45 | LIMIT 1 |
| 46 | ), |
| 47 | claimed AS ( |
| 48 | UPDATE workflow_jobs j |
| 49 | SET runner_id = sqlc.arg(runner_id)::bigint, |
| 50 | status = 'running', |
| 51 | started_at = COALESCE(j.started_at, now()), |
| 52 | version = j.version + 1, |
| 53 | updated_at = now() |
| 54 | FROM candidate c |
| 55 | WHERE j.id = c.id |
| 56 | RETURNING j.id, j.run_id, j.job_index, j.job_key, j.job_name, j.runs_on, |
| 57 | j.runner_id, j.needs_jobs, j.if_expr, j.timeout_minutes, |
| 58 | j.permissions, j.job_env, j.status, j.conclusion, |
| 59 | j.cancel_requested, j.started_at, j.completed_at, j.version, |
| 60 | j.created_at, j.updated_at |
| 61 | ) |
| 62 | SELECT c.id, c.run_id, c.job_index, c.job_key, c.job_name, c.runs_on, |
| 63 | c.runner_id, c.needs_jobs, c.if_expr, c.timeout_minutes, |
| 64 | c.permissions, c.job_env, c.status, c.conclusion, |
| 65 | c.cancel_requested, c.started_at, c.completed_at, c.version, |
| 66 | c.created_at, c.updated_at, |
| 67 | r.repo_id, r.run_index, r.workflow_file, r.workflow_name, |
| 68 | r.head_sha, r.head_ref, r.event |
| 69 | FROM claimed c |
| 70 | JOIN workflow_runs r ON r.id = c.run_id; |
| 71 | |
| 72 | -- name: ListJobsForRun :many |
| 73 | SELECT id, run_id, job_index, job_key, job_name, runs_on, status, |
| 74 | conclusion, started_at, completed_at, created_at |
| 75 | FROM workflow_jobs |
| 76 | WHERE run_id = $1 |
| 77 | ORDER BY job_index ASC; |
| 78 |