Skip to content

Commit

Permalink
Merge pull request #11 from summerwind/fix-graceful-shutdown
Browse files Browse the repository at this point in the history
Stop worker gracefully
  • Loading branch information
achiku committed Aug 26, 2022
2 parents b41dc9c + f4228c8 commit 614ebe4
Showing 1 changed file with 22 additions and 10 deletions.
32 changes: 22 additions & 10 deletions worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,24 @@ func NewWorker(c *Client, m WorkMap) *Worker {
// Work pulls jobs off the Worker's Queue at its Interval. This function only
// returns after Shutdown() is called, so it should be run in its own goroutine.
func (w *Worker) Work() {
defer log.Println("worker done")
for {
select {
case <-w.ch:
log.Println("worker done")
return
case <-time.After(w.Interval):
for {
if didWork := w.WorkOne(); !didWork {
break // didn't do any work, go back to sleep
}
// Try to work a job
if w.WorkOne() {
// Since we just did work, non-blocking check whether we should exit
select {
case <-w.ch:
return
default:
// continue in loop
}
} else {
// No work found, block until exit or timer expires
select {
case <-w.ch:
return
case <-time.After(w.Interval):
// continue in loop
}
}
}
Expand Down Expand Up @@ -218,7 +226,11 @@ func (w *WorkerPool) Shutdown() {

for _, worker := range w.workers {
go func(worker *Worker) {
worker.Shutdown()
// If Shutdown is called before Start has been called,
// then these are nil, so don't try to close them
if worker != nil {
worker.Shutdown()
}
wg.Done()
}(worker)
}
Expand Down

0 comments on commit 614ebe4

Please sign in to comment.