Skip to content

Commit a0dc478

Browse files
committed
Fix startup/shutdown script log messaging
1 parent 26bea9a commit a0dc478

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

agent/agent.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -295,9 +295,10 @@ func (a *agent) run(ctx context.Context) error {
295295
}
296296
}
297297

298+
lifecycleState := codersdk.WorkspaceAgentLifecycleReady
298299
scriptDone := make(chan error, 1)
299300
scriptStart := time.Now()
300-
err := a.trackConnGoroutine(func() {
301+
err = a.trackConnGoroutine(func() {
301302
defer close(scriptDone)
302303
scriptDone <- a.runStartupScript(ctx, metadata.StartupScript)
303304
})
@@ -325,15 +326,16 @@ func (a *agent) run(ctx context.Context) error {
325326
if errors.Is(err, context.Canceled) {
326327
return
327328
}
328-
execTime := time.Since(scriptStart)
329-
lifecycleState := codersdk.WorkspaceAgentLifecycleReady
330-
if err != nil {
331-
a.logger.Warn(ctx, "startup script failed", slog.F("execution_time", execTime), slog.Error(err))
332-
lifecycleState = codersdk.WorkspaceAgentLifecycleStartError
333-
} else {
334-
a.logger.Info(ctx, "startup script completed", slog.F("execution_time", execTime))
329+
// Only log if there was a startup script.
330+
if metadata.StartupScript != "" {
331+
execTime := time.Since(scriptStart)
332+
if err != nil {
333+
a.logger.Warn(ctx, "startup script failed", slog.F("execution_time", execTime), slog.Error(err))
334+
lifecycleState = codersdk.WorkspaceAgentLifecycleStartError
335+
} else {
336+
a.logger.Info(ctx, "startup script completed", slog.F("execution_time", execTime))
337+
}
335338
}
336-
337339
a.setLifecycle(ctx, lifecycleState)
338340
}()
339341
}
@@ -1230,7 +1232,7 @@ func (a *agent) Close() error {
12301232
// TODO(mafredri): Only run shutdown script if the agent is 'ready'?
12311233

12321234
lifecycleState := codersdk.WorkspaceAgentLifecycleOff
1233-
if metadata, ok := a.metadata.Load().(agentsdk.Metadata); ok {
1235+
if metadata, ok := a.metadata.Load().(agentsdk.Metadata); ok && metadata.ShutdownScript != "" {
12341236
scriptDone := make(chan error, 1)
12351237
scriptStart := time.Now()
12361238
go func() {

0 commit comments

Comments
 (0)