fix(cron): wait for job goroutines on stop

This commit is contained in:
Goon committed 2026-05-31 17:01:35 +07:00
1 parent 994acfe3a4
commit 6f676d1bd4
2 files changed
+12 -2

No files matched your search

+10 -2
View File
@@ -13,6 +13,7 @@ type Service struct {
onJob JobHandler
running bool
stopChan chan struct{}
wg sync.WaitGroup
mu sync.Mutex
runLog []RunLogEntry // in-memory run history (last 200 entries)
retryCfg RetryConfig // retry config for failed jobs
@@ -89,7 +90,11 @@ func (cs *Service) Start() error {
// after a previous Stop() returned but the runLoop goroutine hasn't yet
// executed its ticker construction.
tick := runLoopTickInterval
go cs.runLoop(cs.stopChan, tick)
cs.wg.Add(1)
go func() {
defer cs.wg.Done()
cs.runLoop(cs.stopChan, tick)
}()
slog.Info("cron service started", "jobs", len(cs.store.Jobs))
return nil
@@ -98,14 +103,17 @@ func (cs *Service) Start() error {
// Stop halts the scheduling loop.
func (cs *Service) Stop() {
cs.mu.Lock()
defer cs.mu.Unlock()
if !cs.running {
cs.mu.Unlock()
return
}
close(cs.stopChan)
cs.running = false
cs.mu.Unlock()
cs.wg.Wait()
slog.Info("cron service stopped")
}
+2
View File
@@ -216,7 +216,9 @@ func (cs *Service) checkJobs() {
// agent loop stuck), the entire cron scheduler would stop checking for new
// due jobs. Now each job runs independently with panic recovery.
for _, dj := range dueJobs {
cs.wg.Add(1)
go func(id string, scheduledAtMS int64) {
defer cs.wg.Done()
defer safego.Recover(nil, "job_id", id)
cs.executeJobByID(id, scheduledAtMS)
}(dj.id, dj.scheduledAtMS)