Skip to content

Commit

Permalink
FEATURE: Redis connection with AUTH password (#12)
Browse files Browse the repository at this point in the history
FEATURE: Redis connection with AUTH password
  • Loading branch information
Inchie authored and Bastian Waidelich committed Sep 12, 2018
1 parent b01d776 commit 664fc64
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 7 deletions.
15 changes: 13 additions & 2 deletions Classes/Queue/RedisQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
*/
class RedisQueue implements QueueInterface
{

/**
* @var string
*/
Expand Down Expand Up @@ -296,10 +295,22 @@ protected function connectClient()
{
$host = isset($this->clientOptions['host']) ? $this->clientOptions['host'] : '127.0.0.1';
$port = isset($this->clientOptions['port']) ? $this->clientOptions['port'] : 6379;
$password = isset($this->clientOptions['password']) ? $this->clientOptions['password'] : '';
$database = isset($this->clientOptions['database']) ? $this->clientOptions['database'] : 0;
// The connection read timeout should be higher than the timeout for blocking operations!
$timeout = isset($this->clientOptions['timeout']) ? $this->clientOptions['timeout'] : round($this->defaultTimeout * 1.5);
$connected = $this->client->connect($host, $port, $timeout) && $this->client->select($database);

$connected = $this->client->connect($host, $port, $timeout);
if ($connected) {
if ($password !== '') {
$authSuccess = $this->client->auth($password);
if ($authSuccess !== true) {
throw new JobQueueException('Redis authentication failed.', 1536735535);
}
}
$connected = $this->client->select($database);
}

// Break the cycle that could cause a high CPU load
if (!$connected) {
usleep($this->reconnectDelay * 1e6);
Expand Down
6 changes: 6 additions & 0 deletions Configuration/Settings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,9 @@ Flowpack:
host: 127.0.0.1
port: 6379
database: 15
# Set password in your redis config
passwordProtectedClient:
host: 127.0.0.1
port: 6379
password: 'My_Secret_Password'
database: 15
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ composer require flowpack/jobqueue-redis
```

If not already installed, that will fetch its requirements, namely `jobqueue-common`.

*NOTE:* This package needs a [redis](http://redis.io/) server and the [PHP redis extension](https://github.com/phpredis/phpredis) to be installed

Now the queue can be configured like this:
Expand All @@ -28,6 +29,7 @@ Flowpack:
host: 127.0.0.1
port: 6379
database: 15
password: 'some long secret'
defaultTimeout: 20
```
Expand All @@ -36,10 +38,10 @@ Flowpack:
The `RedisQueue` supports following options:

| Option | Type | Default | Description |
| ----------------------- |---------| ---------------------------------------------------------------------------------------------:| ---------------------------------------- |
| defaultTimeout | integer | 60 | Number of seconds new messages are waited for before a timeout occurs (This is overridden by a "timeout" argument in the `waitAndTake()` and `waitAndReserve()` methods |
| client | array | ['host' => '127.0.0.1', 'port' => 6379, 'database' => 0, 'timeout' => <defaultTimeout * 1.5>] | Redis connection settings |
| Option | Type | Default | Description |
|----------------|---------|----------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| defaultTimeout | integer | 60 | Number of seconds new messages are waited for before a timeout occurs (This is overridden by a "timeout" argument in the `waitAndTake()` and `waitAndReserve()` methods |
| client | array | ['host' => '127.0.0.1', 'port' => 6379, 'database' => 0, 'timeout' => <defaultTimeout * 1.5>, password => ''] | Redis connection settings |

### Submit options

Expand All @@ -55,4 +57,4 @@ This package is licensed under the MIT license

## Contributions

Pull-Requests are more than welcome. Make sure to read the [Code Of Conduct](CodeOfConduct.rst).
Pull-Requests are more than welcome. Make sure to read the [Code Of Conduct](CodeOfConduct.rst).
70 changes: 70 additions & 0 deletions Tests/Functional/Queue/RedisAuthenticationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php
namespace Flowpack\JobQueue\Redis\Tests\Functional\Queue;

/*
* This file is part of the Flowpack.JobQueue.Redis package.
*
* (c) Contributors to the package
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/

use Flowpack\JobQueue\Common\Queue\QueueInterface;
use Flowpack\JobQueue\Redis\Queue\RedisQueue;
use Neos\Flow\Configuration\ConfigurationManager;
use Neos\Flow\Tests\FunctionalTestCase;
use Neos\Utility\TypeHandling;

/**
* Functional test for RedisAuthenticationTest
*/
class RedisAuthenticationTest extends FunctionalTestCase
{
/**
* @var QueueInterface
*/
protected $queue;

/**
* @var array
*/
protected $queueSettings;

/**
* @var array
*/
protected $options = [];

/**
* Set up dependencies
*/
public function setUp()
{
parent::setUp();
$configurationManager = $this->objectManager->get(ConfigurationManager::class);
$packageKey = $this->objectManager->getPackageKeyByObjectName(
TypeHandling::getTypeForValue($this)
);
$packageSettings = $configurationManager->getConfiguration(
ConfigurationManager::CONFIGURATION_TYPE_SETTINGS, $packageKey
);
if (!isset($packageSettings['testing']['enabled']) || $packageSettings['testing']['enabled'] !== true) {
$this->markTestSkipped(sprintf('Queue is not configured (%s.testing.enabled != TRUE)', $packageKey));
}
$this->queueSettings = $packageSettings['testing'];
$this->options['client'] = $this->queueSettings['passwordProtectedClient'];
$this->queue = new RedisQueue('Test queue', $this->options);
$this->queue->flush();
}

/**
* @test
*/
public function testConnectionByAddingAMessage()
{
$this->queue->submit('First message');
$this->assertSame(1, $this->queue->countReady());
}
}

0 comments on commit 664fc64

Please sign in to comment.