Skip to content

Commit 2d239a6

Browse files
fix: stop query returning dormant workspaces
1 parent 8b1b6d9 commit 2d239a6

File tree

6 files changed

+40
-19
lines changed

6 files changed

+40
-19
lines changed

coderd/autobuild/lifecycle_executor.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ func (e *Executor) runOnce(t time.Time) Stats {
205205
return xerrors.Errorf("get template scheduling options: %w", err)
206206
}
207207

208-
// If next start at is not valid we need to re-compute it.
208+
// If next start at is not valid we need to re-compute it
209209
if !ws.NextStartAt.Valid && ws.AutostartSchedule.Valid {
210210
next, err := schedule.NextAllowedAutostart(currentTick, ws.AutostartSchedule.String, templateSchedule)
211211
if err == nil {

coderd/database/dump.sql

+9-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/migrations/000278_workspace_next_start_at.down.sql

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
DROP VIEW workspaces_expanded;
22

3-
DROP TRIGGER IF EXISTS trigger_update_workspaces_schedule ON workspaces;
4-
DROP FUNCTION IF EXISTS nullify_workspace_next_start_at;
3+
DROP TRIGGER IF EXISTS trigger_nullify_next_start_at_on_template_autostart_modification ON templates;
4+
DROP FUNCTION IF EXISTS nullify_next_start_at_on_template_autostart_modification;
5+
6+
DROP TRIGGER IF EXISTS trigger_nullify_next_start_at_on_workspace_autostart_modification ON workspaces;
7+
DROP FUNCTION IF EXISTS nullify_next_start_at_on_workspace_autostart_modification;
58

69
DROP INDEX workspace_next_start_at_idx;
710

coderd/database/migrations/000278_workspace_next_start_at.up.sql

+9-4
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,17 @@ ALTER TABLE ONLY workspaces ADD COLUMN IF NOT EXISTS next_start_at TIMESTAMPTZ D
22

33
CREATE INDEX workspace_next_start_at_idx ON workspaces USING btree (next_start_at) WHERE (deleted=false);
44

5-
CREATE FUNCTION nullify_workspace_next_start_at() RETURNS trigger
5+
CREATE FUNCTION nullify_next_start_at_on_workspace_autostart_modification() RETURNS trigger
66
LANGUAGE plpgsql
77
AS $$
88
DECLARE
99
BEGIN
10-
IF (NEW.autostart_schedule <> OLD.autostart_schedule) AND (NEW.next_start_at = OLD.next_start_at) THEN
10+
-- A workspace's next_start_at might be invalidated by the following:
11+
-- * The autostart schedule has changed independent to next_start_at
12+
-- * The workspace has been marked as dormant
13+
IF (NEW.autostart_schedule <> OLD.autostart_schedule AND NEW.next_start_at = OLD.next_start_at)
14+
OR (NEW.dormant_at IS NOT NULL AND NEW.next_start_at IS NOT NULL)
15+
THEN
1116
UPDATE workspaces
1217
SET next_start_at = NULL
1318
WHERE id = NEW.id;
@@ -16,10 +21,10 @@ BEGIN
1621
END;
1722
$$;
1823

19-
CREATE TRIGGER trigger_update_workspaces_schedule
24+
CREATE TRIGGER trigger_nullify_next_start_at_on_workspace_autostart_modification
2025
AFTER UPDATE ON workspaces
2126
FOR EACH ROW
22-
EXECUTE PROCEDURE nullify_workspace_next_start_at();
27+
EXECUTE PROCEDURE nullify_next_start_at_on_workspace_autostart_modification();
2328

2429
-- Recreate view
2530
DROP VIEW workspaces_expanded;

coderd/database/queries.sql.go

+8-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries/workspaces.sql

+8-4
Original file line numberDiff line numberDiff line change
@@ -627,18 +627,22 @@ WHERE
627627
-- * The workspace's owner is active.
628628
-- * The provisioner job did not fail.
629629
-- * The workspace build was a stop transition.
630+
-- * The workspace is not dormant
630631
-- * The workspace has an autostart schedule.
631632
-- * It is after the workspace's next start time.
632633
(
633634
users.status = 'active'::user_status AND
634635
provisioner_jobs.job_status != 'failed'::provisioner_job_status AND
635636
workspace_builds.transition = 'stop'::workspace_transition AND
637+
workspaces.dormant_at IS NULL AND
636638
workspaces.autostart_schedule IS NOT NULL AND
637639
(
638-
-- next_start_at might be null when a coder instance has been updated
639-
-- and we haven't yet had an opportunity to set next_start_at. When
640-
-- this happens we leave it up to the Coder server to figure out if
641-
-- the workspace is ready to autostart.
640+
-- next_start_at might be null in these two scenarios:
641+
-- * A coder instance was updated and we haven't updated next_start_at yet.
642+
-- * A database trigger made it null because of an update to a related column.
643+
--
644+
-- When this occurs, we return the workspace so the Coder server can
645+
-- compute a valid next start at and update it.
642646
workspaces.next_start_at IS NULL OR
643647
workspaces.next_start_at <= @now :: timestamptz
644648
)

0 commit comments

Comments
 (0)