Skip to content

Commit e90df49

Browse files
committed
fixup! Fix startup/shutdown script log messaging
1 parent 5adc708 commit e90df49

File tree

1 file changed

+33
-32
lines changed

1 file changed

+33
-32
lines changed

agent/agent.go

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -296,47 +296,48 @@ func (a *agent) run(ctx context.Context) error {
296296
}
297297

298298
lifecycleState := codersdk.WorkspaceAgentLifecycleReady
299-
if metadata.StartupScript != "" {
300-
scriptDone := make(chan error, 1)
301-
scriptStart := time.Now()
302-
err := a.trackConnGoroutine(func() {
303-
defer close(scriptDone)
304-
scriptDone <- a.runStartupScript(ctx, metadata.StartupScript)
305-
})
306-
if err != nil {
307-
return xerrors.Errorf("track startup script: %w", err)
299+
scriptDone := make(chan error, 1)
300+
scriptStart := time.Now()
301+
err = a.trackConnGoroutine(func() {
302+
defer close(scriptDone)
303+
scriptDone <- a.runStartupScript(ctx, metadata.StartupScript)
304+
})
305+
if err != nil {
306+
return xerrors.Errorf("track startup script: %w", err)
307+
}
308+
go func() {
309+
var timeout <-chan time.Time
310+
// If timeout is zero, an older version of the coder
311+
// provider was used. Otherwise a timeout is always > 0.
312+
if metadata.StartupScriptTimeout > 0 {
313+
t := time.NewTimer(metadata.StartupScriptTimeout)
314+
defer t.Stop()
315+
timeout = t.C
308316
}
309-
go func() {
310-
var timeout <-chan time.Time
311-
// If timeout is zero, an older version of the coder
312-
// provider was used. Otherwise a timeout is always > 0.
313-
if metadata.StartupScriptTimeout > 0 {
314-
t := time.NewTimer(metadata.StartupScriptTimeout)
315-
defer t.Stop()
316-
timeout = t.C
317-
}
318317

319-
var err error
320-
select {
321-
case err = <-scriptDone:
322-
case <-timeout:
323-
a.logger.Warn(ctx, "startup script timed out")
324-
a.setLifecycle(ctx, codersdk.WorkspaceAgentLifecycleStartTimeout)
325-
err = <-scriptDone // The script can still complete after a timeout.
326-
}
327-
if errors.Is(err, context.Canceled) {
328-
return
329-
}
318+
var err error
319+
select {
320+
case err = <-scriptDone:
321+
case <-timeout:
322+
a.logger.Warn(ctx, "startup script timed out")
323+
a.setLifecycle(ctx, codersdk.WorkspaceAgentLifecycleStartTimeout)
324+
err = <-scriptDone // The script can still complete after a timeout.
325+
}
326+
if errors.Is(err, context.Canceled) {
327+
return
328+
}
329+
// Only log if there was a startup script.
330+
if metadata.StartupScript != "" {
330331
execTime := time.Since(scriptStart)
331332
if err != nil {
332333
a.logger.Warn(ctx, "startup script failed", slog.F("execution_time", execTime), slog.Error(err))
333334
lifecycleState = codersdk.WorkspaceAgentLifecycleStartError
334335
} else {
335336
a.logger.Info(ctx, "startup script completed", slog.F("execution_time", execTime))
336337
}
337-
}()
338-
}
339-
a.setLifecycle(ctx, lifecycleState)
338+
}
339+
a.setLifecycle(ctx, lifecycleState)
340+
}()
340341
}
341342

342343
// This automatically closes when the context ends!

0 commit comments

Comments
 (0)