2
0
mirror of https://github.com/hibiken/asynq.git synced 2024-12-26 07:42:17 +08:00

Make Run method stateful

This commit is contained in:
Ken Hibino 2019-11-17 21:21:32 -08:00
parent 62db9863fb
commit c6f482d4f8

View File

@ -109,6 +109,9 @@ type Workers struct {
// poolTokens is a counting semaphore to ensure the number of active workers // poolTokens is a counting semaphore to ensure the number of active workers
// does not exceed the limit. // does not exceed the limit.
poolTokens chan struct{} poolTokens chan struct{}
// running indicates whether the workes are currently running.
running bool
} }
// NewWorkers creates and returns a new Workers. // 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. // Run starts the workers and scheduler with a given handler.
func (w *Workers) Run(handler TaskHandler) { func (w *Workers) Run(handler TaskHandler) {
if w.running {
return
}
w.running = true
go w.pollDeferred() go w.pollDeferred()
for { for {