mirror of
https://github.com/coder/coder.git
synced 2025-07-06 15:41:45 +00:00
Control loop now handles reconciliation of multiple prebuilds-configured template versions
Correctly calculates extraneous prebuilds and returns offending prebuild IDs Signed-off-by: Danny Kopping <danny@coder.com>
This commit is contained in:
@ -1,4 +1,4 @@
|
||||
-- name: GetTemplatePrebuildState :one
|
||||
-- name: GetTemplatePrebuildState :many
|
||||
WITH
|
||||
-- All prebuilds currently running
|
||||
running_prebuilds AS (SELECT p.template_id,
|
||||
@ -8,6 +8,7 @@ WITH
|
||||
FROM workspace_prebuilds p
|
||||
INNER JOIN workspace_latest_build b ON b.workspace_id = p.id
|
||||
INNER JOIN provisioner_jobs pj ON b.job_id = pj.id
|
||||
INNER JOIN templates t ON p.template_id = t.id
|
||||
WHERE (b.transition = 'start'::workspace_transition
|
||||
-- if a deletion job fails, the workspace will still be running
|
||||
OR pj.job_status IN ('failed'::provisioner_job_status, 'canceled'::provisioner_job_status,
|
||||
@ -28,37 +29,36 @@ WITH
|
||||
GROUP BY t.id, tv.id, tvpp.id),
|
||||
-- Jobs relating to prebuilds current in-flight
|
||||
prebuilds_in_progress AS (SELECT wpb.template_version_id, wpb.transition, COUNT(wpb.transition) AS count
|
||||
FROM workspace_prebuild_builds wpb
|
||||
INNER JOIN workspace_latest_build wlb ON wpb.workspace_id = wlb.workspace_id
|
||||
FROM workspace_latest_build wlb
|
||||
INNER JOIN provisioner_jobs pj ON wlb.job_id = pj.id
|
||||
INNER JOIN workspace_prebuild_builds wpb ON wpb.id = wlb.id
|
||||
WHERE pj.job_status NOT IN
|
||||
('succeeded'::provisioner_job_status, 'canceled'::provisioner_job_status,
|
||||
'failed'::provisioner_job_status)
|
||||
GROUP BY wpb.template_version_id, wpb.transition)
|
||||
SELECT t.template_id,
|
||||
t.template_version_id,
|
||||
t.using_active_version AS is_active,
|
||||
p.ids AS running_prebuild_ids,
|
||||
CAST(COALESCE(
|
||||
MAX(CASE WHEN t.using_active_version THEN p.count ELSE 0 END),
|
||||
0) AS INT) AS actual, -- running prebuilds for active version
|
||||
t.using_active_version AS is_active,
|
||||
CAST(COALESCE(MAX(CASE WHEN p.template_version_id = t.template_version_id THEN p.ids END),
|
||||
'') AS TEXT) AS running_prebuild_ids,
|
||||
CAST(COALESCE(MAX(CASE WHEN t.using_active_version THEN p.count ELSE 0 END),
|
||||
0) AS INT) AS actual, -- running prebuilds for active version
|
||||
CAST(COALESCE(MAX(CASE WHEN t.using_active_version THEN t.desired_instances ELSE 0 END),
|
||||
0) AS INT) AS desired, -- we only care about the active version's desired instances
|
||||
CAST(COALESCE(MAX(CASE WHEN t.using_active_version THEN 0 ELSE p.count END),
|
||||
0) AS INT) AS extraneous, -- running prebuilds for inactive version
|
||||
0) AS INT) AS desired, -- we only care about the active version's desired instances
|
||||
CAST(COALESCE(MAX(CASE
|
||||
WHEN pip.transition = 'start'::workspace_transition THEN pip.count
|
||||
ELSE 0 END), 0) AS INT) AS starting,
|
||||
CAST(COALESCE(MAX(CASE
|
||||
WHEN pip.transition = 'stop'::workspace_transition THEN pip.count
|
||||
ELSE 0 END),
|
||||
0) AS INT) AS stopping, -- not strictly needed, since prebuilds should never be left if a "stopped" state, but useful to know
|
||||
CAST(COALESCE(MAX(CASE
|
||||
WHEN pip.transition = 'delete'::workspace_transition THEN pip.count
|
||||
ELSE 0 END), 0) AS INT) AS deleting,
|
||||
t.deleted AS template_deleted,
|
||||
t.deprecated AS template_deprecated
|
||||
WHEN p.template_version_id = t.template_version_id AND t.using_active_version = false
|
||||
THEN p.count END),
|
||||
0) AS INT) AS extraneous, -- running prebuilds for inactive version
|
||||
CAST(COALESCE(MAX(CASE WHEN pip.transition = 'start'::workspace_transition THEN pip.count ELSE 0 END),
|
||||
0) AS INT) AS starting,
|
||||
CAST(COALESCE(MAX(CASE WHEN pip.transition = 'stop'::workspace_transition THEN pip.count ELSE 0 END),
|
||||
0) AS INT) AS stopping, -- not strictly needed, since prebuilds should never be left if a "stopped" state, but useful to know
|
||||
CAST(COALESCE(MAX(CASE WHEN pip.transition = 'delete'::workspace_transition THEN pip.count ELSE 0 END),
|
||||
0) AS INT) AS deleting,
|
||||
t.deleted AS template_deleted,
|
||||
t.deprecated AS template_deprecated
|
||||
FROM templates_with_prebuilds t
|
||||
LEFT JOIN running_prebuilds p ON p.template_id = t.template_id
|
||||
LEFT JOIN running_prebuilds p ON p.template_version_id = t.template_version_id
|
||||
LEFT JOIN prebuilds_in_progress pip ON pip.template_version_id = t.template_version_id
|
||||
GROUP BY t.using_active_version, t.template_id, t.template_version_id, p.count, p.ids, t.deleted, t.deprecated;
|
||||
GROUP BY t.using_active_version, t.template_id, t.template_version_id, p.count, p.ids,
|
||||
p.template_version_id, t.deleted, t.deprecated;
|
||||
|
Reference in New Issue
Block a user