From 70ab2aac8851aac6fa72374d439224f3bae28e50 Mon Sep 17 00:00:00 2001 From: howardwu Date: Mon, 16 Mar 2020 18:29:23 +0800 Subject: [PATCH] Fix error cause by sync wait group --- cron.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/cron.go b/cron.go index 208ed3c..3df797f 100644 --- a/cron.go +++ b/cron.go @@ -58,7 +58,7 @@ type Cron struct { running bool add chan *Entry stop chan struct{} - wg *sync.WaitGroup + wg sync.WaitGroup } // New instantiates new Cron instant c. @@ -114,7 +114,7 @@ func (c *Cron) StopAfterJobDone() { return } c.running = false - c.wg.Done() + c.wg.Wait() c.stop <- struct{}{} } @@ -151,7 +151,8 @@ func (c *Cron) run() { } entry.Prev = now entry.Next = entry.Schedule.Next(now) - go entry.Job.Run(c.wg) + c.wg.Add(1) + go entry.Job.Run(&c.wg) } case e := <-c.add: e.Next = e.Schedule.Next(time.Now()) @@ -177,6 +178,5 @@ type JobFunc func() // Run calls j() func (j JobFunc) Run(wg *sync.WaitGroup) { j() - wg.Add(1) wg.Done() }