Skip to content

Commit c851597

Browse files
committed
bin/resque: add support for connecting with a cluster client
As `bin/resque` currently works, it pulls in the hostname to use from the environment using `getenv`. The problem with this is that you cannot pass in an array which is what the underlying Redis library uses[1] to determine whether it initialises a `Credis_Client` or a `Credit_Cluster` for the connection. This solves that by introducing a new environment variable (`REDIS_CLUSTER_ENABLED`) to determine when it should expand a comma separated list of hosts to define as a cluster. Additionally, passing in a comma delimitered list of hosts to `REDIS_BACKEND` will also trigger this behaviour. [1]: master/lib/Resque/Redis.php#L128
1 parent 78a8b4a commit c851597

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,18 @@ Alternately, you can always `include('bin/resque')` from your application and
301301
skip setting `APP_INCLUDE` altogether. Just be sure the various environment
302302
variables are set (`setenv`) before you do.
303303

304+
### Clustering support
305+
306+
There are two ways to use the built in clustering support.
307+
308+
- Explicitly set `REDIS_CLUSTER_ENABLED=1` environment variable; and
309+
- Include a comma delimitered list of hosts in the `REDIS_BACKEND` environment
310+
variable.
311+
312+
Either of these approaches will inform the `setBackend` call to expand the
313+
`REDIS_BACKEND` environment variable and trigger the instantiation of a
314+
`Credis_Cluster` instead of a `Credis_Client`.
315+
304316
### Logging
305317

306318
The port supports the same environment variables for logging to STDOUT. Setting

bin/resque

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,21 @@ if(empty($QUEUE)) {
3737
* Note: the 'user' part of the DSN URI is required but is not used.
3838
*/
3939
$REDIS_BACKEND = getenv('REDIS_BACKEND');
40+
$REDIS_CLUSTER_ENABLED = getenv('REDIS_CLUSTER_ENABLED');
4041

4142
// A redis database number
4243
$REDIS_BACKEND_DB = getenv('REDIS_BACKEND_DB');
43-
if(!empty($REDIS_BACKEND)) {
44-
if (empty($REDIS_BACKEND_DB))
45-
Resque::setBackend($REDIS_BACKEND);
46-
else
47-
Resque::setBackend($REDIS_BACKEND, $REDIS_BACKEND_DB);
44+
45+
if (!empty($REDIS_BACKEND)) {
46+
if (empty($REDIS_BACKEND_DB)) {
47+
if (filter_var($REDIS_CLUSTER_ENABLED, FILTER_VALIDATE_BOOLEAN) || stripos($REDIS_BACKEND, ',') !== false) {
48+
Resque::setBackend(explode(",", $REDIS_BACKEND));
49+
} else {
50+
Resque::setBackend($REDIS_BACKEND);
51+
}
52+
} else {
53+
Resque::setBackend($REDIS_BACKEND, $REDIS_BACKEND_DB);
54+
}
4855
}
4956

5057
$logLevel = false;

0 commit comments

Comments
 (0)