fix counter bugs

This commit is contained in:
sadlil 2016-09-21 13:08:17 +06:00
parent 475f2aa0d0
commit f61772507f

View File

@ -277,7 +277,14 @@ func (worker *Worker) SetId(id string) {
// inner job executing // inner job executing
func (worker *Worker) exec(inpack *inPack) (err error) { func (worker *Worker) exec(inpack *inPack) (err error) {
jobRunned := false
defer func() { 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 { if worker.limit != nil {
<-worker.limit <-worker.limit
} }
@ -288,19 +295,17 @@ func (worker *Worker) exec(inpack *inPack) (err error) {
err = ErrUnknown err = ErrUnknown
} }
} }
if worker.runningJobs > 0 {
worker.Lock()
worker.runningJobs--
worker.Unlock()
}
}() }()
f, ok := worker.funcs[inpack.fn] f, ok := worker.funcs[inpack.fn]
if !ok { if !ok {
return fmt.Errorf("The function does not exist: %s", inpack.fn) 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.Lock()
worker.runningJobs++ worker.runningJobs++
worker.Unlock() worker.Unlock()
var r *result var r *result
if f.timeout == 0 { if f.timeout == 0 {
d, e := f.f(inpack) d, e := f.f(inpack)