From db7692d864e1146598c84f7426cedeb6af16de54 Mon Sep 17 00:00:00 2001 From: Jonathan Novak Date: Mon, 25 Jul 2016 15:50:48 -0700 Subject: [PATCH] BUGFIX: handle errors in worker by not spinning If there was an error when fetching a job, we previously just keep trying super fast, which burns CPU and prevents wp.Stop() from working. This can happen if Redis is down. Instead of doing that, chill out and sleep for 10 milliseconds. This addresses #7. --- worker.go | 2 ++ worker_test.go | 17 +++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/worker.go b/worker.go index 6c55d854..8be1f1ca 100644 --- a/worker.go +++ b/worker.go @@ -108,6 +108,8 @@ func (w *worker) loop() { job, err := w.fetchJob() if err != nil { logError("worker.fetch", err) + gotJob = false + timer.Reset(10 * time.Millisecond) } else if job != nil { w.processJob(job) consequtiveNoJobs = 0 diff --git a/worker_test.go b/worker_test.go index 44f42028..170fba51 100644 --- a/worker_test.go +++ b/worker_test.go @@ -234,6 +234,23 @@ func TestWorkerDead(t *testing.T) { assert.True(t, (nowEpochSeconds()-job.FailedAt) <= 2) } +// Test that in the case of an unavailable Redis server, +// the worker loop exits in the case of a WorkerPool.Stop +func TestStop(t *testing.T) { + redisPool := &redis.Pool{ + Dial: func() (redis.Conn, error) { + c, err := redis.Dial("tcp", "notworking:6379") + if err != nil { + return nil, err + } + return c, nil + }, + } + wp := NewWorkerPool(TestContext{}, 10, "work", redisPool) + wp.Start() + wp.Stop() +} + func BenchmarkJobProcessing(b *testing.B) { pool := newTestPool(":6379") ns := "work"