Skip to content

Commit c0cdc3c

Browse files
author
Chris Boulton
committed
Merge pull request #24 from d11wtq/bugfix/broken-pipe
Fix a bug where the worker would spin out of control if the redis socket is interrupted
2 parents 700af83 + c90e21d commit c0cdc3c

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

lib/Redisent/Redisent.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ class Redisent {
4949
function __construct($host, $port = 6379) {
5050
$this->host = $host;
5151
$this->port = $port;
52+
$this->establishConnection();
53+
}
54+
55+
function establishConnection() {
5256
$this->__sock = fsockopen($this->host, $this->port, $errno, $errstr);
5357
if (!$this->__sock) {
5458
throw new Exception("{$errno} - {$errstr}");

lib/Resque/Worker.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,7 @@ private function registerSigHandlers()
358358
pcntl_signal(SIGUSR1, array($this, 'killChild'));
359359
pcntl_signal(SIGUSR2, array($this, 'pauseProcessing'));
360360
pcntl_signal(SIGCONT, array($this, 'unPauseProcessing'));
361+
pcntl_signal(SIGPIPE, array($this, 'reestablishRedisConnection'));
361362
$this->log('Registered signals', self::LOG_VERBOSE);
362363
}
363364

@@ -380,6 +381,15 @@ public function unPauseProcessing()
380381
$this->paused = false;
381382
}
382383

384+
/**
385+
* Signal handler for SIGPIPE, in the event the redis connection has gone away.
386+
* Attempts to reconnect to redis, or raises an Exception.
387+
*/
388+
public function reestablishRedisConnection() {
389+
$this->log('SIGPIPE received; attempting to reconnect');
390+
Resque::redis()->establishConnection();
391+
}
392+
383393
/**
384394
* Schedule a worker for shutdown. Will finish processing the current job
385395
* and when the timeout interval is reached, the worker will shut down.

0 commit comments

Comments
 (0)