Skip to content

Commit 16e17e8

Browse files
committed
use provisioner job status for workspace searching
1 parent 7736647 commit 16e17e8

File tree

2 files changed

+34
-49
lines changed

2 files changed

+34
-49
lines changed

coderd/database/migrations/000160_provisioner_job_status.up.sql

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,14 @@ ALTER TABLE provisioner_jobs ADD COLUMN
1818
-- Not completed means it is in some "-ing" state
1919
ELSE
2020
CASE
21+
-- This should never happen because all errors set
22+
-- should also set a completed_at timestamp.
23+
-- But if there is an error, we should always return
24+
-- a failed state.
25+
WHEN error != '' THEN 'failed'::provisioner_job_status
2126
WHEN canceled_at IS NOT NULL THEN 'canceling'::provisioner_job_status
2227
-- Not done and not started means it is pending
2328
WHEN started_at IS NULL THEN 'pending'::provisioner_job_status
24-
-- Job is still running
2529
ELSE 'running'::provisioner_job_status
2630
END
2731
END

coderd/database/queries/workspaces.sql

Lines changed: 29 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ LEFT JOIN LATERAL (
9696
provisioner_jobs.updated_at,
9797
provisioner_jobs.canceled_at,
9898
provisioner_jobs.completed_at,
99-
provisioner_jobs.error
99+
provisioner_jobs.error,
100+
provisioner_jobs.job_status
100101
FROM
101102
workspace_builds
102103
LEFT JOIN
@@ -128,63 +129,43 @@ WHERE
128129
AND CASE
129130
WHEN @status :: text != '' THEN
130131
CASE
131-
WHEN @status = 'pending' THEN
132-
latest_build.started_at IS NULL
132+
-- Some workspace specific status refer to the transition
133+
-- type. By default, the standard provisioner job status
134+
-- search strings are supported.
135+
-- 'running' states
133136
WHEN @status = 'starting' THEN
134-
latest_build.started_at IS NOT NULL AND
135-
latest_build.canceled_at IS NULL AND
136-
latest_build.completed_at IS NULL AND
137+
latest_build.job_status = 'running' AND
138+
-- TODO: Should we drop the interval? Hung workspaces
139+
-- should still be their last state?
137140
latest_build.updated_at - INTERVAL '30 seconds' < NOW() AND
138141
latest_build.transition = 'start'::workspace_transition
139-
140-
WHEN @status = 'running' THEN
141-
latest_build.completed_at IS NOT NULL AND
142-
latest_build.canceled_at IS NULL AND
143-
latest_build.error IS NULL AND
144-
latest_build.transition = 'start'::workspace_transition
145-
146142
WHEN @status = 'stopping' THEN
147-
latest_build.started_at IS NOT NULL AND
148-
latest_build.canceled_at IS NULL AND
149-
latest_build.completed_at IS NULL AND
143+
latest_build.job_status = 'running' AND
144+
-- TODO: Should we drop the interval? Hung workspaces
145+
-- should still be their last state?
150146
latest_build.updated_at - INTERVAL '30 seconds' < NOW() AND
151147
latest_build.transition = 'stop'::workspace_transition
148+
WHEN @status = 'deleting' THEN
149+
latest_build.job_status = 'running' AND
150+
-- TODO: Should we drop the interval? Hung workspaces
151+
-- should still be their last state?
152+
latest_build.updated_at - INTERVAL '30 seconds' < NOW() AND
153+
latest_build.transition = 'delete'::workspace_transition
152154

155+
-- 'succeeded' states
156+
WHEN @status = 'deleted' THEN
157+
latest_build.job_status = 'succeeded' AND
158+
latest_build.transition = 'delete'::workspace_transition
153159
WHEN @status = 'stopped' THEN
154-
latest_build.completed_at IS NOT NULL AND
155-
latest_build.canceled_at IS NULL AND
156-
latest_build.error IS NULL AND
160+
latest_build.job_status = 'succeeded' AND
157161
latest_build.transition = 'stop'::workspace_transition
162+
WHEN @status = 'started' THEN
163+
latest_build.job_status = 'succeeded' AND
164+
latest_build.transition = 'start'::workspace_transition
158165

159-
WHEN @status = 'failed' THEN
160-
(latest_build.canceled_at IS NOT NULL AND
161-
latest_build.error IS NOT NULL) OR
162-
(latest_build.completed_at IS NOT NULL AND
163-
latest_build.error IS NOT NULL)
164-
165-
WHEN @status = 'canceling' THEN
166-
latest_build.canceled_at IS NOT NULL AND
167-
latest_build.completed_at IS NULL
168-
169-
WHEN @status = 'canceled' THEN
170-
latest_build.canceled_at IS NOT NULL AND
171-
latest_build.completed_at IS NOT NULL
172-
173-
WHEN @status = 'deleted' THEN
174-
latest_build.started_at IS NOT NULL AND
175-
latest_build.canceled_at IS NULL AND
176-
latest_build.completed_at IS NOT NULL AND
177-
latest_build.updated_at - INTERVAL '30 seconds' < NOW() AND
178-
latest_build.transition = 'delete'::workspace_transition AND
179-
-- If the error field is not null, the status is 'failed'
180-
latest_build.error IS NULL
181-
182-
WHEN @status = 'deleting' THEN
183-
latest_build.completed_at IS NULL AND
184-
latest_build.canceled_at IS NULL AND
185-
latest_build.error IS NULL AND
186-
latest_build.transition = 'delete'::workspace_transition
187-
166+
WHEN @status != '' THEN
167+
-- By default just match the job status exactly
168+
latest_build.job_status = @status
188169
ELSE
189170
true
190171
END

0 commit comments

Comments
 (0)