Skip to content

Commit 292a878

Browse files
committed
Fix startup/shutdown script log messaging
1 parent bf42f4e commit 292a878

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
@@ -313,9 +313,10 @@ func (a *agent) run(ctx context.Context) error {
313313
}
314314
}
315315

316+
lifecycleState := codersdk.WorkspaceAgentLifecycleReady
316317
scriptDone := make(chan error, 1)
317318
scriptStart := time.Now()
318-
err := a.trackConnGoroutine(func() {
319+
err = a.trackConnGoroutine(func() {
319320
defer close(scriptDone)
320321
scriptDone <- a.runStartupScript(ctx, metadata.StartupScript)
321322
})
@@ -343,15 +344,16 @@ func (a *agent) run(ctx context.Context) error {
343344
if errors.Is(err, context.Canceled) {
344345
return
345346
}
346-
execTime := time.Since(scriptStart)
347-
lifecycleState := codersdk.WorkspaceAgentLifecycleReady
348-
if err != nil {
349-
a.logger.Warn(ctx, "startup script failed", slog.F("execution_time", execTime), slog.Error(err))
350-
lifecycleState = codersdk.WorkspaceAgentLifecycleStartError
351-
} else {
352-
a.logger.Info(ctx, "startup script completed", slog.F("execution_time", execTime))
347+
// Only log if there was a startup script.
348+
if metadata.StartupScript != "" {
349+
execTime := time.Since(scriptStart)
350+
if err != nil {
351+
a.logger.Warn(ctx, "startup script failed", slog.F("execution_time", execTime), slog.Error(err))
352+
lifecycleState = codersdk.WorkspaceAgentLifecycleStartError
353+
} else {
354+
a.logger.Info(ctx, "startup script completed", slog.F("execution_time", execTime))
355+
}
353356
}
354-
355357
a.setLifecycle(ctx, lifecycleState)
356358
}()
357359
}
@@ -1325,7 +1327,7 @@ func (a *agent) Close() error {
13251327
// TODO(mafredri): Only run shutdown script if the agent is 'ready'?
13261328

13271329
lifecycleState := codersdk.WorkspaceAgentLifecycleOff
1328-
if metadata, ok := a.metadata.Load().(agentsdk.Metadata); ok {
1330+
if metadata, ok := a.metadata.Load().(agentsdk.Metadata); ok && metadata.ShutdownScript != "" {
13291331
scriptDone := make(chan error, 1)
13301332
scriptStart := time.Now()
13311333
go func() {

0 commit comments

Comments
 (0)