From 57396ee66c71faa74ee94b56d88f32cc88aa8d56 Mon Sep 17 00:00:00 2001 From: Rex Tsao Date: Thu, 9 Jul 2020 10:07:50 +0800 Subject: [PATCH] Fix: free s.Cron on proper opportunity to avoid unexpected crash. --- dkron/agent.go | 1 + dkron/scheduler.go | 11 ++++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/dkron/agent.go b/dkron/agent.go index 1176d5203..10eb440d9 100644 --- a/dkron/agent.go +++ b/dkron/agent.go @@ -240,6 +240,7 @@ func (a *Agent) Stop() error { if a.config.Server && a.sched.Started { a.sched.Stop() + a.sched.ClearCron() } if err := a.serf.Leave(); err != nil { diff --git a/dkron/scheduler.go b/dkron/scheduler.go index 428b0fcf1..fe8763139 100644 --- a/dkron/scheduler.go +++ b/dkron/scheduler.go @@ -60,7 +60,10 @@ func (s *Scheduler) Stop() { log.Debug("scheduler: Stopping scheduler") s.Cron.Stop() s.Started = false - s.Cron = nil + // Keep Cron exists and let the jobs which have been scheduled can continue to finish, + // even the node's leadership will be revoked. + // Ignore the running jobs and make s.Cron to nil may cause whole process crashed. + //s.Cron = nil // expvars cronInspect.Do(func(kv expvar.KeyValue) { @@ -73,9 +76,15 @@ func (s *Scheduler) Stop() { // Restart the scheduler func (s *Scheduler) Restart(jobs []*Job, agent *Agent) { s.Stop() + s.ClearCron() s.Start(jobs, agent) } +// Clear cron separately, this can only be called when agent will be stop. +func (s *Scheduler) ClearCron() { + s.Cron = nil +} + // GetEntry returns a scheduler entry from a snapshot in // the current time, and whether or not the entry was found. func (s *Scheduler) GetEntry(jobName string) (cron.Entry, bool) {