@@ -295,47 +295,48 @@ func (a *agent) run(ctx context.Context) error {
295
295
}
296
296
}
297
297
298
- scriptDone := make (chan error , 1 )
299
- scriptStart := time .Now ()
300
- err := a .trackConnGoroutine (func () {
301
- defer close (scriptDone )
302
- scriptDone <- a .runStartupScript (ctx , metadata .StartupScript )
303
- })
304
- if err != nil {
305
- return xerrors .Errorf ("track startup script: %w" , err )
306
- }
307
- go func () {
308
- var timeout <- chan time.Time
309
- // If timeout is zero, an older version of the coder
310
- // provider was used. Otherwise a timeout is always > 0.
311
- if metadata .StartupScriptTimeout > 0 {
312
- t := time .NewTimer (metadata .StartupScriptTimeout )
313
- defer t .Stop ()
314
- timeout = t .C
315
- }
316
-
317
- var err error
318
- select {
319
- case err = <- scriptDone :
320
- case <- timeout :
321
- a .logger .Warn (ctx , "startup script timed out" )
322
- a .setLifecycle (ctx , codersdk .WorkspaceAgentLifecycleStartTimeout )
323
- err = <- scriptDone // The script can still complete after a timeout.
324
- }
325
- if errors .Is (err , context .Canceled ) {
326
- return
327
- }
328
- execTime := time .Since (scriptStart )
329
- lifecycleState := codersdk .WorkspaceAgentLifecycleReady
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
+ })
330
306
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 ))
307
+ return xerrors .Errorf ("track startup script: %w" , err )
335
308
}
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
+ }
336
318
337
- a .setLifecycle (ctx , lifecycleState )
338
- }()
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
+ }
330
+ execTime := time .Since (scriptStart )
331
+ if err != nil {
332
+ a .logger .Warn (ctx , "startup script failed" , slog .F ("execution_time" , execTime ), slog .Error (err ))
333
+ lifecycleState = codersdk .WorkspaceAgentLifecycleStartError
334
+ } else {
335
+ a .logger .Info (ctx , "startup script completed" , slog .F ("execution_time" , execTime ))
336
+ }
337
+ }()
338
+ }
339
+ a .setLifecycle (ctx , lifecycleState )
339
340
}
340
341
341
342
// This automatically closes when the context ends!
@@ -1230,7 +1231,7 @@ func (a *agent) Close() error {
1230
1231
// TODO(mafredri): Only run shutdown script if the agent is 'ready'?
1231
1232
1232
1233
lifecycleState := codersdk .WorkspaceAgentLifecycleOff
1233
- if metadata , ok := a .metadata .Load ().(agentsdk.Metadata ); ok {
1234
+ if metadata , ok := a .metadata .Load ().(agentsdk.Metadata ); ok && metadata . ShutdownScript != "" {
1234
1235
scriptDone := make (chan error , 1 )
1235
1236
scriptStart := time .Now ()
1236
1237
go func () {
0 commit comments