Skip to content

Commit b7bb014

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. [1]: master/lib/Resque/Redis.php#L128
1 parent 78a8b4a commit b7bb014

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

README.md

+7
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,13 @@ 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+
To enable clustering support on the worker, you need to define
307+
`REDIS_CLUSTER_ENABLED` which will inform the `setBackend` call to expand the
308+
`REDIS_BACKEND` environment variable and trigger the instantiation of a
309+
`Credis_Cluster` instead of a `Credis_Client`.
310+
304311
### Logging
305312

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

bin/resque

+14-5
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,23 @@ 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+
if (!empty($REDIS_BACKEND)) {
45+
if (empty($REDIS_BACKEND_DB)) {
46+
// Should we have clustering enabled, we accept a comma separated list of
47+
// servers to pass to the backend. This tells the underlying client we should
48+
// use the specific cluster client.
49+
if (filter_var($REDIS_CLUSTER_ENABLED, FILTER_VALIDATE_BOOLEAN)) {
50+
Resque::setBackend(explode(",", $REDIS_BACKEND));
51+
} else {
52+
Resque::setBackend($REDIS_BACKEND);
53+
}
54+
} else {
55+
Resque::setBackend($REDIS_BACKEND, $REDIS_BACKEND_DB);
56+
}
4857
}
4958

5059
$logLevel = false;

0 commit comments

Comments
 (0)