fix(agent/agentscripts): track cron run and wait for cron stop (#10388)

Fixes #10289
This commit is contained in:
Mathias Fredriksson
2023-10-23 20:08:52 +03:00
committed by GitHub
parent 1a2aea3a6b
commit 09f7b8e88c
+10 -2
View File
@@ -97,7 +97,15 @@ func (r *Runner) Init(scripts []codersdk.WorkspaceAgentScript) error {
// StartCron starts the cron scheduler.
// This is done async to allow for the caller to execute scripts prior.
func (r *Runner) StartCron() {
r.cron.Start()
// cron.Start() and cron.Stop() does not guarantee that the cron goroutine
// has exited by the time the `cron.Stop()` context returns, so we need to
// track it manually.
err := r.trackCommandGoroutine(func() {
r.cron.Run()
})
if err != nil {
r.Logger.Warn(context.Background(), "start cron failed", slog.Error(err))
}
}
// Execute runs a set of scripts according to a filter.
@@ -254,7 +262,7 @@ func (r *Runner) Close() error {
}
close(r.closed)
r.cronCtxCancel()
r.cron.Stop()
<-r.cron.Stop().Done()
r.cmdCloseWait.Wait()
return nil
}