Skip to content

Commit d7e86c6

Browse files
feat: replace timed_out column with status column
1 parent 7fe6d8c commit d7e86c6

14 files changed

+402
-215
lines changed

agent/agentscripts/agentscripts.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,7 @@ func (r *Runner) run(ctx context.Context, script codersdk.WorkspaceAgentScript,
340340

341341
// We want to check this outside of the goroutine to avoid a race condition
342342
timedOut := errors.Is(err, ErrTimeout)
343+
pipesLeftOpen := errors.Is(err, ErrOutputPipesOpen)
343344

344345
err = r.trackCommandGoroutine(func() {
345346
var stage proto.Timing_Stage
@@ -352,6 +353,18 @@ func (r *Runner) run(ctx context.Context, script codersdk.WorkspaceAgentScript,
352353
stage = proto.Timing_CRON
353354
}
354355

356+
var status proto.Timing_Status
357+
switch {
358+
case !timedOut && !pipesLeftOpen && exitCode == 0:
359+
status = proto.Timing_OK
360+
case !timedOut && !pipesLeftOpen && exitCode != 0:
361+
status = proto.Timing_EXIT_FAILURE
362+
case timedOut:
363+
status = proto.Timing_TIMED_OUT
364+
case pipesLeftOpen:
365+
status = proto.Timing_PIPES_LEFT_OPEN
366+
}
367+
355368
reportTimeout := 30 * time.Second
356369
reportCtx, cancel := context.WithTimeout(context.Background(), reportTimeout)
357370
defer cancel()
@@ -363,7 +376,7 @@ func (r *Runner) run(ctx context.Context, script codersdk.WorkspaceAgentScript,
363376
End: timestamppb.New(end),
364377
ExitCode: int32(exitCode),
365378
Stage: stage,
366-
TimedOut: timedOut,
379+
Status: status,
367380
},
368381
})
369382
if err != nil {

agent/proto/agent.pb.go

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

agent/proto/agent.proto

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,14 @@ message Timing {
285285
CRON = 2;
286286
}
287287
Stage stage = 5;
288-
bool timed_out = 6;
288+
289+
enum Status {
290+
OK = 0;
291+
EXIT_FAILURE = 1;
292+
TIMED_OUT = 2;
293+
PIPES_LEFT_OPEN = 3;
294+
}
295+
Status status = 6;
289296
}
290297

291298
service Agent {

cli/organizationsettings.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,13 @@ func (r *RootCmd) setOrganizationSettings(orgContext *OrganizationContext, setti
125125

126126
settingJSON, err := json.Marshal(output)
127127
if err != nil {
128-
return fmt.Errorf("failed to marshal organization setting %s: %w", inv.Args[0], err)
128+
return xerrors.Errorf("failed to marshal organization setting %s: %w", inv.Args[0], err)
129129
}
130130

131131
var dst bytes.Buffer
132132
err = json.Indent(&dst, settingJSON, "", "\t")
133133
if err != nil {
134-
return fmt.Errorf("failed to indent organization setting as json %s: %w", inv.Args[0], err)
134+
return xerrors.Errorf("failed to indent organization setting as json %s: %w", inv.Args[0], err)
135135
}
136136

137137
_, err = fmt.Fprintln(inv.Stdout, dst.String())
@@ -190,13 +190,13 @@ func (r *RootCmd) printOrganizationSetting(orgContext *OrganizationContext, sett
190190

191191
settingJSON, err := json.Marshal(output)
192192
if err != nil {
193-
return fmt.Errorf("failed to marshal organization setting %s: %w", inv.Args[0], err)
193+
return xerrors.Errorf("failed to marshal organization setting %s: %w", inv.Args[0], err)
194194
}
195195

196196
var dst bytes.Buffer
197197
err = json.Indent(&dst, settingJSON, "", "\t")
198198
if err != nil {
199-
return fmt.Errorf("failed to indent organization setting as json %s: %w", inv.Args[0], err)
199+
return xerrors.Errorf("failed to indent organization setting as json %s: %w", inv.Args[0], err)
200200
}
201201

202202
_, err = fmt.Fprintln(inv.Stdout, dst.String())

coderd/agentapi/scripts.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,27 @@ func (s *ScriptsAPI) ScriptCompleted(ctx context.Context, req *agentproto.Worksp
3333
stage = database.WorkspaceAgentScriptTimingStageCron
3434
}
3535

36+
var status database.WorkspaceAgentScriptTimingStatus
37+
switch req.Timing.Status {
38+
case agentproto.Timing_OK:
39+
status = database.WorkspaceAgentScriptTimingStatusOk
40+
case agentproto.Timing_EXIT_FAILURE:
41+
status = database.WorkspaceAgentScriptTimingStatusExitFailure
42+
case agentproto.Timing_TIMED_OUT:
43+
status = database.WorkspaceAgentScriptTimingStatusTimedOut
44+
case agentproto.Timing_PIPES_LEFT_OPEN:
45+
status = database.WorkspaceAgentScriptTimingStatusPipesLeftOpen
46+
}
47+
3648
//nolint:gocritic // We need permissions to write to the DB here and we are in the context of the agent.
3749
ctx = dbauthz.AsProvisionerd(ctx)
3850
err = s.Database.InsertWorkspaceAgentScriptTimings(ctx, database.InsertWorkspaceAgentScriptTimingsParams{
3951
ScriptID: scriptID,
4052
Stage: stage,
53+
Status: status,
4154
StartedAt: req.Timing.Start.AsTime(),
4255
EndedAt: req.Timing.End.AsTime(),
4356
ExitCode: req.Timing.ExitCode,
44-
TimedOut: req.Timing.TimedOut,
4557
})
4658
if err != nil {
4759
return nil, xerrors.Errorf("insert workspace agent script timings into database: %w", err)

coderd/agentapi/scripts_test.go

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func TestScriptCompleted(t *testing.T) {
2929
Stage: agentproto.Timing_START,
3030
Start: timestamppb.New(dbtime.Now()),
3131
End: timestamppb.New(dbtime.Now().Add(time.Second)),
32-
TimedOut: false,
32+
Status: agentproto.Timing_OK,
3333
ExitCode: 0,
3434
},
3535
},
@@ -39,7 +39,7 @@ func TestScriptCompleted(t *testing.T) {
3939
Stage: agentproto.Timing_STOP,
4040
Start: timestamppb.New(dbtime.Now()),
4141
End: timestamppb.New(dbtime.Now().Add(time.Second)),
42-
TimedOut: false,
42+
Status: agentproto.Timing_OK,
4343
ExitCode: 0,
4444
},
4545
},
@@ -49,7 +49,7 @@ func TestScriptCompleted(t *testing.T) {
4949
Stage: agentproto.Timing_CRON,
5050
Start: timestamppb.New(dbtime.Now()),
5151
End: timestamppb.New(dbtime.Now().Add(time.Second)),
52-
TimedOut: false,
52+
Status: agentproto.Timing_OK,
5353
ExitCode: 0,
5454
},
5555
},
@@ -59,7 +59,7 @@ func TestScriptCompleted(t *testing.T) {
5959
Stage: agentproto.Timing_START,
6060
Start: timestamppb.New(dbtime.Now()),
6161
End: timestamppb.New(dbtime.Now().Add(time.Second)),
62-
TimedOut: true,
62+
Status: agentproto.Timing_TIMED_OUT,
6363
ExitCode: 255,
6464
},
6565
},
@@ -69,7 +69,7 @@ func TestScriptCompleted(t *testing.T) {
6969
Stage: agentproto.Timing_START,
7070
Start: timestamppb.New(dbtime.Now()),
7171
End: timestamppb.New(dbtime.Now().Add(time.Second)),
72-
TimedOut: true,
72+
Status: agentproto.Timing_EXIT_FAILURE,
7373
ExitCode: 1,
7474
},
7575
},
@@ -83,9 +83,9 @@ func TestScriptCompleted(t *testing.T) {
8383
mDB.EXPECT().InsertWorkspaceAgentScriptTimings(gomock.Any(), database.InsertWorkspaceAgentScriptTimingsParams{
8484
ScriptID: tt.scriptID,
8585
Stage: protoScriptTimingStageToDatabase(tt.timing.Stage),
86+
Status: protoScriptTimingStatusToDatabase(tt.timing.Status),
8687
StartedAt: tt.timing.Start.AsTime(),
8788
EndedAt: tt.timing.End.AsTime(),
88-
TimedOut: tt.timing.TimedOut,
8989
ExitCode: tt.timing.ExitCode,
9090
})
9191

@@ -108,3 +108,18 @@ func protoScriptTimingStageToDatabase(stage agentproto.Timing_Stage) database.Wo
108108
}
109109
return dbStage
110110
}
111+
112+
func protoScriptTimingStatusToDatabase(stage agentproto.Timing_Status) database.WorkspaceAgentScriptTimingStatus {
113+
var dbStatus database.WorkspaceAgentScriptTimingStatus
114+
switch stage {
115+
case agentproto.Timing_OK:
116+
dbStatus = database.WorkspaceAgentScriptTimingStatusOk
117+
case agentproto.Timing_EXIT_FAILURE:
118+
dbStatus = database.WorkspaceAgentScriptTimingStatusExitFailure
119+
case agentproto.Timing_TIMED_OUT:
120+
dbStatus = database.WorkspaceAgentScriptTimingStatusTimedOut
121+
case agentproto.Timing_PIPES_LEFT_OPEN:
122+
dbStatus = database.WorkspaceAgentScriptTimingStatusPipesLeftOpen
123+
}
124+
return dbStatus
125+
}

coderd/database/dbauthz/dbauthz_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2636,6 +2636,7 @@ func (s *MethodTestSuite) TestSystemFunctions() {
26362636
check.Args(database.InsertWorkspaceAgentScriptTimingsParams{
26372637
ScriptID: uuid.New(),
26382638
Stage: database.WorkspaceAgentScriptTimingStageStart,
2639+
Status: database.WorkspaceAgentScriptTimingStatusOk,
26392640
}).Asserts(rbac.ResourceSystem, policy.ActionCreate)
26402641
}))
26412642
s.Run("InsertWorkspaceAgentScripts", s.Subtest(func(db database.Store, check *expects) {

coderd/database/dbmem/dbmem.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7844,7 +7844,7 @@ func (q *FakeQuerier) InsertWorkspaceAgentScriptTimings(_ context.Context, arg d
78447844
EndedAt: arg.EndedAt,
78457845
ExitCode: arg.ExitCode,
78467846
Stage: arg.Stage,
7847-
TimedOut: arg.TimedOut,
7847+
Status: arg.Status,
78487848
},
78497849
)
78507850

coderd/database/dump.sql

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

coderd/database/migrations/000257_workspace_agent_script_timings.down.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
DROP TYPE IF EXISTS workspace_agent_script_timing_status CASCADE;
12
DROP TYPE IF EXISTS workspace_agent_script_timing_stage CASCADE;
23
DROP TABLE IF EXISTS workspace_agent_script_timings;
34

coderd/database/migrations/000257_workspace_agent_script_timings.up.sql

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,21 @@ CREATE TYPE workspace_agent_script_timing_stage AS ENUM (
66
'cron'
77
);
88

9+
CREATE TYPE workspace_agent_script_timing_status AS ENUM (
10+
'ok',
11+
'exit_failure',
12+
'timed_out',
13+
'pipes_left_open'
14+
);
15+
916
CREATE TABLE workspace_agent_script_timings
1017
(
11-
script_id uuid NOT NULL REFERENCES workspace_agent_scripts (id) ON DELETE CASCADE,
12-
started_at timestamp with time zone NOT NULL,
13-
ended_at timestamp with time zone NOT NULL,
14-
exit_code int NOT NULL,
15-
stage workspace_agent_script_timing_stage NOT NULL,
16-
timed_out bool NOT NULL
18+
script_id uuid NOT NULL REFERENCES workspace_agent_scripts (id) ON DELETE CASCADE,
19+
started_at timestamp with time zone NOT NULL,
20+
ended_at timestamp with time zone NOT NULL,
21+
exit_code int NOT NULL,
22+
stage workspace_agent_script_timing_stage NOT NULL,
23+
status workspace_agent_script_timing_status NOT NULL
1724
);
1825

1926
ALTER TABLE workspace_agent_script_timings ADD UNIQUE (script_id, started_at);

coderd/database/models.go

Lines changed: 70 additions & 6 deletions
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: 8 additions & 8 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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ INSERT INTO
296296
ended_at,
297297
exit_code,
298298
stage,
299-
timed_out
299+
status
300300
)
301301
VALUES
302302
($1, $2, $3, $4, $5, $6);

0 commit comments

Comments
 (0)