Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 23, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions agent/agentscripts/agentscripts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
Expand Down