Skip to content

Commit

Permalink
BUGFIX: handle errors in worker by not spinning
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
cypriss committed Jul 25, 2016
1 parent adc78a0 commit db7692d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
2 changes: 2 additions & 0 deletions worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit db7692d

Please sign in to comment.