Skip to content

Commit 75388f7

Browse files
committed
Fix schema jank
1 parent 78f01d1 commit 75388f7

16 files changed

+82
-77
lines changed

agent/agent_test.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,9 +1209,9 @@ func TestAgent_Lifecycle(t *testing.T) {
12091209

12101210
_, client, _, _, _ := setupAgent(t, agentsdk.Manifest{
12111211
Scripts: []codersdk.WorkspaceAgentScript{{
1212-
Script: "sleep 3",
1213-
Timeout: time.Nanosecond,
1214-
RunOnStart: true,
1212+
Script: "sleep 3",
1213+
TimeoutSeconds: time.Nanosecond,
1214+
RunOnStart: true,
12151215
}},
12161216
}, 0)
12171217

@@ -1234,9 +1234,9 @@ func TestAgent_Lifecycle(t *testing.T) {
12341234

12351235
_, client, _, _, _ := setupAgent(t, agentsdk.Manifest{
12361236
Scripts: []codersdk.WorkspaceAgentScript{{
1237-
Script: "false",
1238-
Timeout: 30 * time.Second,
1239-
RunOnStart: true,
1237+
Script: "false",
1238+
TimeoutSeconds: 30 * time.Second,
1239+
RunOnStart: true,
12401240
}},
12411241
}, 0)
12421242

@@ -1259,9 +1259,9 @@ func TestAgent_Lifecycle(t *testing.T) {
12591259

12601260
_, client, _, _, _ := setupAgent(t, agentsdk.Manifest{
12611261
Scripts: []codersdk.WorkspaceAgentScript{{
1262-
Script: "true",
1263-
Timeout: 30 * time.Second,
1264-
RunOnStart: true,
1262+
Script: "true",
1263+
TimeoutSeconds: 30 * time.Second,
1264+
RunOnStart: true,
12651265
}},
12661266
}, 0)
12671267

@@ -1284,9 +1284,9 @@ func TestAgent_Lifecycle(t *testing.T) {
12841284

12851285
_, client, _, _, closer := setupAgent(t, agentsdk.Manifest{
12861286
Scripts: []codersdk.WorkspaceAgentScript{{
1287-
Script: "sleep 3",
1288-
Timeout: 30 * time.Second,
1289-
RunOnStop: true,
1287+
Script: "sleep 3",
1288+
TimeoutSeconds: 30 * time.Second,
1289+
RunOnStop: true,
12901290
}},
12911291
}, 0)
12921292

@@ -1325,9 +1325,9 @@ func TestAgent_Lifecycle(t *testing.T) {
13251325

13261326
_, client, _, _, closer := setupAgent(t, agentsdk.Manifest{
13271327
Scripts: []codersdk.WorkspaceAgentScript{{
1328-
Script: "sleep 3",
1329-
Timeout: time.Nanosecond,
1330-
RunOnStop: true,
1328+
Script: "sleep 3",
1329+
TimeoutSeconds: time.Nanosecond,
1330+
RunOnStop: true,
13311331
}},
13321332
}, 0)
13331333

@@ -1367,9 +1367,9 @@ func TestAgent_Lifecycle(t *testing.T) {
13671367

13681368
_, client, _, _, closer := setupAgent(t, agentsdk.Manifest{
13691369
Scripts: []codersdk.WorkspaceAgentScript{{
1370-
Script: "false",
1371-
Timeout: 30 * time.Second,
1372-
RunOnStop: true,
1370+
Script: "false",
1371+
TimeoutSeconds: 30 * time.Second,
1372+
RunOnStop: true,
13731373
}},
13741374
}, 0)
13751375

agent/agentscripts/agentscripts.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,10 @@ func (r *Runner) run(script codersdk.WorkspaceAgentScript) error {
141141
}()
142142

143143
var cmd *exec.Cmd
144-
if script.Timeout > 0 {
144+
if script.TimeoutSeconds > 0 {
145145
var cancel context.CancelFunc
146146
// Add a buffer to forcefully kill with the context.
147-
ctx, cancel = context.WithTimeout(ctx, script.Timeout+(3*time.Second))
147+
ctx, cancel = context.WithTimeout(ctx, script.TimeoutSeconds+(3*time.Second))
148148
defer cancel()
149149
}
150150

@@ -196,9 +196,9 @@ func (r *Runner) run(script codersdk.WorkspaceAgentScript) error {
196196

197197
// timeout stores whether the process timed out then was gracefully killed.
198198
var timeout chan struct{}
199-
if script.Timeout > 0 {
199+
if script.TimeoutSeconds > 0 {
200200
timeout = make(chan struct{})
201-
timer := time.AfterFunc(script.Timeout, func() {
201+
timer := time.AfterFunc(script.TimeoutSeconds, func() {
202202
close(timeout)
203203
err := cmd.Process.Signal(os.Interrupt)
204204
if err != nil {
@@ -232,6 +232,7 @@ func (r *Runner) Close() error {
232232
return nil
233233
}
234234
close(r.closed)
235+
r.cron.Stop()
235236
r.cmdCloseWait.Wait()
236237
return nil
237238
}

agent/agentscripts/agentscripts_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ func TestTimeout(t *testing.T) {
4646
runner := setup(t, nil)
4747
defer runner.Close()
4848
err := runner.Init([]codersdk.WorkspaceAgentScript{{
49-
Script: "sleep 3",
50-
Timeout: time.Nanosecond,
49+
Script: "sleep 3",
50+
TimeoutSeconds: time.Nanosecond,
5151
}})
5252
require.NoError(t, err)
5353
require.ErrorIs(t, runner.Execute(nil), agentscripts.ErrTimeout)

coderd/database/dbfake/dbfake.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4557,7 +4557,7 @@ func (q *FakeQuerier) InsertWorkspaceAgentLogSources(_ context.Context, arg data
45574557
logSource := database.WorkspaceAgentLogSource{
45584558
ID: source,
45594559
WorkspaceAgentID: arg.WorkspaceAgentID,
4560-
CreatedAt: arg.CreatedAt[index],
4560+
CreatedAt: arg.CreatedAt,
45614561
DisplayName: arg.DisplayName[index],
45624562
Icon: arg.Icon[index],
45634563
}
@@ -4586,7 +4586,7 @@ func (q *FakeQuerier) InsertWorkspaceAgentLogs(_ context.Context, arg database.I
45864586
logs = append(logs, database.WorkspaceAgentLog{
45874587
ID: id,
45884588
AgentID: arg.AgentID,
4589-
CreatedAt: arg.CreatedAt[index],
4589+
CreatedAt: arg.CreatedAt,
45904590
Level: arg.Level[index],
45914591
LogSourceID: arg.LogSourceID,
45924592
Output: output,
@@ -4650,8 +4650,8 @@ func (q *FakeQuerier) InsertWorkspaceAgentScripts(_ context.Context, arg databas
46504650
StartBlocksLogin: arg.StartBlocksLogin[index],
46514651
RunOnStart: arg.RunOnStart[index],
46524652
RunOnStop: arg.RunOnStop[index],
4653-
Timeout: arg.Timeout[index],
4654-
CreatedAt: arg.CreatedAt[index],
4653+
TimeoutSeconds: arg.TimeoutSeconds[index],
4654+
CreatedAt: arg.CreatedAt,
46554655
}
46564656
scripts = append(scripts, script)
46574657
}

coderd/database/dbgen/dbgen.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ func WorkspaceAgentLogSource(t testing.TB, db database.Store, orig database.Work
178178
sources, err := db.InsertWorkspaceAgentLogSources(genCtx, database.InsertWorkspaceAgentLogSourcesParams{
179179
WorkspaceAgentID: takeFirst(orig.WorkspaceAgentID, uuid.New()),
180180
ID: []uuid.UUID{takeFirst(orig.ID, uuid.New())},
181-
CreatedAt: []time.Time{takeFirst(orig.CreatedAt, dbtime.Now())},
181+
CreatedAt: takeFirst(orig.CreatedAt, dbtime.Now()),
182182
DisplayName: []string{takeFirst(orig.DisplayName, namesgenerator.GetRandomName(1))},
183183
Icon: []string{takeFirst(orig.Icon, namesgenerator.GetRandomName(1))},
184184
})

coderd/database/dump.sql

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

coderd/database/migrations/000155_workspace_agent_script.up.sql

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
BEGIN;
22
CREATE TABLE workspace_agent_log_sources (
33
workspace_agent_id uuid NOT NULL,
4-
id uuid NOT NULL,
4+
id uuid NOT NULL UNIQUE,
55
created_at timestamptz NOT NULL,
66
display_name varchar(127) NOT NULL,
77
icon text NOT NULL,
@@ -10,15 +10,15 @@ CREATE TABLE workspace_agent_log_sources (
1010

1111
CREATE TABLE workspace_agent_scripts (
1212
workspace_agent_id uuid NOT NULL,
13-
log_source_id uuid NOT NULL,
13+
log_source_id uuid NOT NULL REFERENCES workspace_agent_log_sources(id) ON DELETE CASCADE,
1414
log_path text NOT NULL,
1515
created_at timestamptz NOT NULL,
1616
script text NOT NULL,
1717
cron text NOT NULL,
1818
start_blocks_login boolean NOT NULL,
1919
run_on_start boolean NOT NULL,
2020
run_on_stop boolean NOT NULL,
21-
timeout integer NOT NULL
21+
timeout_seconds integer NOT NULL
2222
);
2323

2424
ALTER TABLE workspace_agent_logs ADD COLUMN log_source_id uuid NOT NULL;

coderd/database/models.go

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

coderd/database/queries.sql.go

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

coderd/database/queries/workspaceagents.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ INSERT INTO
154154
workspace_agent_logs (agent_id, created_at, output, level, log_source_id)
155155
SELECT
156156
@agent_id :: uuid AS agent_id,
157-
unnest(@created_at :: timestamptz [ ]) AS created_at,
157+
@created_at :: timestamptz AS created_at,
158158
unnest(@output :: VARCHAR(1024) [ ]) AS output,
159159
unnest(@level :: log_level [ ]) AS level,
160160
@log_source_id :: uuid AS log_source_id
@@ -165,8 +165,8 @@ INSERT INTO
165165
workspace_agent_log_sources (workspace_agent_id, id, created_at, display_name, icon)
166166
SELECT
167167
@workspace_agent_id :: uuid AS workspace_agent_id,
168+
@created_at :: timestamptz AS created_at,
168169
unnest(@id :: uuid [ ]) AS id,
169-
unnest(@created_at :: timestamptz [ ]) AS created_at,
170170
unnest(@display_name :: VARCHAR(127) [ ]) AS display_name,
171171
unnest(@icon :: text [ ]) AS icon
172172
RETURNING workspace_agent_log_sources.*;

0 commit comments

Comments
 (0)