StartCron starts the cron scheduler. This is done async to allow for the caller to execute scripts prior.
()
| 164 | // StartCron starts the cron scheduler. |
| 165 | // This is done async to allow for the caller to execute scripts prior. |
| 166 | func (r *Runner) StartCron() { |
| 167 | // cron.Start() and cron.Stop() does not guarantee that the cron goroutine |
| 168 | // has exited by the time the `cron.Stop()` context returns, so we need to |
| 169 | // track it manually. |
| 170 | err := r.trackCommandGoroutine(func() { |
| 171 | // Since this is run async, in quick unit tests, it is possible the |
| 172 | // Close() function gets called before we even start the cron. |
| 173 | // In these cases, the Run() will never end. |
| 174 | // So if we are closed, we just return, and skip the Run() entirely. |
| 175 | select { |
| 176 | case <-r.cronCtx.Done(): |
| 177 | // The cronCtx is canceled before cron.Close() happens. So if the ctx is |
| 178 | // canceled, then Close() will be called, or it is about to be called. |
| 179 | // So do nothing! |
| 180 | default: |
| 181 | r.cron.Run() |
| 182 | } |
| 183 | }) |
| 184 | if err != nil { |
| 185 | r.Logger.Warn(context.Background(), "start cron failed", slog.Error(err)) |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | // ExecuteOption describes what scripts we want to execute. |
| 190 | type ExecuteOption int |