From f61772507fff58f673da8543e51961861c015899 Mon Sep 17 00:00:00 2001 From: sadlil Date: Wed, 21 Sep 2016 13:08:17 +0600 Subject: [PATCH] fix counter bugs --- worker/worker.go | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/worker/worker.go b/worker/worker.go index d07bfba..74881e6 100644 --- a/worker/worker.go +++ b/worker/worker.go @@ -277,7 +277,14 @@ func (worker *Worker) SetId(id string) { // inner job executing func (worker *Worker) exec(inpack *inPack) (err error) { + jobRunned := false defer func() { + // decrement job counter in completion of this job + worker.Lock() + if worker.runningJobs > 0 && jobRunned { + worker.runningJobs-- + } + worker.Unlock() if worker.limit != nil { <-worker.limit } @@ -288,19 +295,17 @@ func (worker *Worker) exec(inpack *inPack) (err error) { err = ErrUnknown } } - if worker.runningJobs > 0 { - worker.Lock() - worker.runningJobs-- - worker.Unlock() - } }() f, ok := worker.funcs[inpack.fn] if !ok { return fmt.Errorf("The function does not exist: %s", inpack.fn) } + jobRunned = true + // Job function found, function will be executing now, increment counter worker.Lock() worker.runningJobs++ worker.Unlock() + var r *result if f.timeout == 0 { d, e := f.f(inpack)