From c6f482d4f868092a132d93b2edc3506eda9e2ff8 Mon Sep 17 00:00:00 2001 From: Ken Hibino Date: Sun, 17 Nov 2019 21:21:32 -0800 Subject: [PATCH] Make Run method stateful --- asynq.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/asynq.go b/asynq.go index 3a51e58..a2d6648 100644 --- a/asynq.go +++ b/asynq.go @@ -109,6 +109,9 @@ type Workers struct { // poolTokens is a counting semaphore to ensure the number of active workers // does not exceed the limit. poolTokens chan struct{} + + // running indicates whether the workes are currently running. + running bool } // NewWorkers creates and returns a new Workers. @@ -125,6 +128,11 @@ type TaskHandler func(*Task) error // Run starts the workers and scheduler with a given handler. func (w *Workers) Run(handler TaskHandler) { + if w.running { + return + } + w.running = true + go w.pollDeferred() for {