Skip to content

Commit 2132c53

Browse files
authored
feat(coderd/database): add schema for task pause/resume lifecycle (#21557)
Creates migration 000409 with the database foundation for pausing and resuming task workspaces. The task_snapshots table stores conversation history (AgentAPI messages) so users can view task logs even when the workspace is stopped. Each task gets one snapshot, overwritten on each pause. Three new build_reason values (task_auto_pause, task_manual_pause, task_resume) let us distinguish task lifecycle events in telemetry and audit logs from regular workspace operations. Uses a regular table rather than UNLOGGED for snapshots. While UNLOGGED would be faster, losing snapshots on database crash creates user confusion (logs disappear until next pause). We can switch to UNLOGGED post-GA if write performance becomes a problem. Closes coder/internal#1250
1 parent 59b71f2 commit 2132c53

7 files changed

Lines changed: 86 additions & 2 deletions

File tree

‎coderd/database/dump.sql‎

Lines changed: 24 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎coderd/database/foreign_key_constraint.go‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
DROP TABLE IF EXISTS task_snapshots;
2+
3+
-- Note: Cannot remove enum values in PostgreSQL.
4+
-- The build_reason enum values (task_auto_pause, task_manual_pause, task_resume)
5+
-- will remain but become unused.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
-- Create task_snapshots table for storing log snapshots when tasks are paused.
2+
-- This table holds the conversation history from AgentAPI, allowing users to view
3+
-- task logs even when the workspace is stopped.
4+
CREATE TABLE task_snapshots (
5+
task_id UUID NOT NULL PRIMARY KEY REFERENCES tasks (id) ON DELETE CASCADE,
6+
log_snapshot JSONB NOT NULL,
7+
log_snapshot_created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
8+
);
9+
10+
COMMENT ON TABLE task_snapshots IS 'Stores snapshots of task state when paused, currently limited to conversation history.';
11+
COMMENT ON COLUMN task_snapshots.task_id IS 'The task this snapshot belongs to.';
12+
COMMENT ON COLUMN task_snapshots.log_snapshot IS 'Task conversation history in JSON format, allowing users to view logs when the workspace is stopped.';
13+
COMMENT ON COLUMN task_snapshots.log_snapshot_created_at IS 'When this log snapshot was captured.';
14+
15+
-- Add build reasons for task lifecycle events.
16+
-- These distinguish task pause/resume operations from regular workspace lifecycle events.
17+
ALTER TYPE build_reason ADD VALUE IF NOT EXISTS 'task_auto_pause';
18+
ALTER TYPE build_reason ADD VALUE IF NOT EXISTS 'task_manual_pause';
19+
ALTER TYPE build_reason ADD VALUE IF NOT EXISTS 'task_resume';
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
INSERT INTO task_snapshots VALUES (
2+
'f5a1c3e4-8b2d-4f6a-9d7e-2a8b5c9e1f3d', -- task_id (references existing task from 000366)
3+
'{
4+
"format": "agentapi",
5+
"data": {
6+
"messages": [
7+
{"id": 0, "type": "output", "content": "Starting task execution...", "time": "2024-11-02T13:10:05Z"},
8+
{"id": 1, "type": "input", "content": "Create a React component for tasks", "time": "2024-11-02T13:10:06Z"},
9+
{"id": 2, "type": "output", "content": "Creating component structure...", "time": "2024-11-02T13:10:10Z"}
10+
],
11+
"truncated": false,
12+
"total_count": 3
13+
}
14+
}'::JSONB, -- log_snapshot
15+
'2024-11-02 13:15:00.000000+02' -- log_snapshot_at
16+
) ON CONFLICT DO NOTHING;

‎coderd/database/models.go‎

Lines changed: 20 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎coderd/database/unique_constraint.go‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)