@@ -296,47 +296,48 @@ func (a *agent) run(ctx context.Context) error {
296
296
}
297
297
298
298
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
308
316
}
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
- }
318
317
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 != "" {
330
331
execTime := time .Since (scriptStart )
331
332
if err != nil {
332
333
a .logger .Warn (ctx , "startup script failed" , slog .F ("execution_time" , execTime ), slog .Error (err ))
333
334
lifecycleState = codersdk .WorkspaceAgentLifecycleStartError
334
335
} else {
335
336
a .logger .Info (ctx , "startup script completed" , slog .F ("execution_time" , execTime ))
336
337
}
337
- }()
338
- }
339
- a . setLifecycle ( ctx , lifecycleState )
338
+ }
339
+ a . setLifecycle ( ctx , lifecycleState )
340
+ }( )
340
341
}
341
342
342
343
// This automatically closes when the context ends!
0 commit comments