Skip to content

Commit

Permalink
Merge pull request #40 from cerpusoddarne/master
Browse files Browse the repository at this point in the history
Make vhost configurable #39
  • Loading branch information
KinaneD authored Feb 4, 2019
2 parents 259e984 + f802a0e commit 90fed2a
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 8 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Tools like the Rabbitmq *[Management](https://www.rabbitmq.com/management.html)*

### Configuration

In order to configure rabbitmq host, port, username, password and configure the timeout values too, add the following inside the connections array in config/queue.php file:
In order to configure rabbitmq host, port, username, password and configure the timeout and vhost values too, add the following inside the connections array in config/queue.php file:

```php
'rabbitmq' => [
Expand All @@ -45,10 +45,11 @@ In order to configure rabbitmq host, port, username, password and configure the
'connection_timeout' => 30,
'read_write_timeout' => 30,
'heartbeat' => 15,
'vhost' => '/',
],
```

The default value for `connection_timeout` and `read_write_timeout` is set to 30 (seconds) and `heartbeat` is set to 15 (seconds).
The default value for `connection_timeout` and `read_write_timeout` is set to 30 (seconds) and `heartbeat` is set to 15 (seconds). The `vhost` value defaults to '/'.

And register the service provider by adding `Vinelab\Bowler\BowlerServiceProvider::class` to the providers array in `config/app`.

Expand Down
3 changes: 2 additions & 1 deletion src/BowlerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ public function register()
$rbmqConnectionTimeout = config('queue.connections.rabbitmq.connection_timeout') ? (int) config('queue.connections.rabbitmq.connection_timeout') : 30;
$rbmqReadWriteTimeout = config('queue.connections.rabbitmq.read_write_timeout') ? (int) config('queue.connections.rabbitmq.read_write_timeout') : 30;
$rbmqHeartbeat = config('queue.connections.rabbitmq.heartbeat') ? (int) config('queue.connections.rabbitmq.heartbeat') : 15;
$rbmqVhost = config('queue.connections.rabbitmq.vhost', '/');

return new Connection($rbmqHost, $rbmqPort, $rbmqUsername, $rbmqPassword, $rbmqConnectionTimeout, $rbmqReadWriteTimeout, $rbmqHeartbeat);
return new Connection($rbmqHost, $rbmqPort, $rbmqUsername, $rbmqPassword, $rbmqConnectionTimeout, $rbmqReadWriteTimeout, $rbmqHeartbeat, $rbmqVhost);
});

$this->app->bind(
Expand Down
14 changes: 10 additions & 4 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ class Connection
*/
private $heartbeat = 15;

/**
* RabbitMQ vhost.
* @var string
*/
private $vhost = '/';

/**
* @param string $host the ip of the rabbitmq server, default: localhost
* @param int $port. default: 5672
Expand All @@ -88,7 +94,7 @@ class Connection
* @param int $readWriteTimeout, default: 30
* @param int $heartbeat, default: 15
*/
public function __construct($host = 'localhost', $port = 5672, $username = 'guest', $password = 'guest', $connectionTimeout = 30, $readWriteTimeout = 30, $heartbeat = 15)
public function __construct($host = 'localhost', $port = 5672, $username = 'guest', $password = 'guest', $connectionTimeout = 30, $readWriteTimeout = 30, $heartbeat = 15, $vhost = '/')
{
$this->host = $host;
$this->port = $port;
Expand All @@ -97,13 +103,13 @@ public function __construct($host = 'localhost', $port = 5672, $username = 'gues
$this->connectionTimeout = $connectionTimeout;
$this->readWriteTimeout = $readWriteTimeout;
$this->heartbeat = $heartbeat;
$this->vhost = $vhost;

$this->initAMQPStreamConnection($host, $port, $username, $password, $connectionTimeout, $readWriteTimeout, $heartbeat);
$this->initAMQPStreamConnection($host, $port, $username, $password, $connectionTimeout, $readWriteTimeout, $heartbeat, $vhost);
}

protected function initAMQPStreamConnection($host, $port, $username, $password, $connectionTimeout, $readWriteTimeout, $heartbeat, $vhost = '/',$insist = false, $login_method = 'AMQPLAIN', $login_response = null, $locale = 'en_US', $context = null, $keepalive = false)
protected function initAMQPStreamConnection($host, $port, $username, $password, $connectionTimeout, $readWriteTimeout, $heartbeat, $vhost = '/', $insist = false, $login_method = 'AMQPLAIN', $login_response = null, $locale = 'en_US', $context = null, $keepalive = false)
{
$vhost = '/';
$insist = false;
$login_method = 'AMQPLAIN';
$login_response = null;
Expand Down
5 changes: 4 additions & 1 deletion tests/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public function test_set_default_configurations_values()
$this->assertAttributeEquals(15, 'heartbeat', $connection);
$this->assertAttributeEquals(30, 'readWriteTimeout', $connection);
$this->assertAttributeEquals(30, 'connectionTimeout', $connection);
$this->assertAttributeEquals('/', 'vhost', $connection);
}

public function test_set_altered_configurations_values()
Expand All @@ -75,6 +76,7 @@ public function test_set_altered_configurations_values()
Config::set('queue.connections.rabbitmq.read_write_timeout', 60);
Config::set('queue.connections.rabbitmq.connection_timeout', 60);
Config::set('queue.connections.rabbitmq.heartbeat', 30);
Config::set('queue.connections.rabbitmq.vhost', '/test-vhost');

$mAMQPStreamConnection = M::mock(AMQPStreamConnection::class);
$this->app->bind(AMQPStreamConnection::class, function () use ($mAMQPStreamConnection) {
Expand All @@ -85,11 +87,12 @@ public function test_set_altered_configurations_values()
$mAMQPStreamConnection->shouldReceive('channel')->once()->withNoArgs()->andReturn($mChannel);
$mAMQPStreamConnection->shouldReceive('close')->once()->withNoArgs();
$mChannel->shouldReceive('close')->once()->withNoArgs();

$connection = $this->app[Connection::class];

$this->assertAttributeEquals(30, 'heartbeat', $connection);
$this->assertAttributeEquals(60, 'readWriteTimeout', $connection);
$this->assertAttributeEquals(60, 'connectionTimeout', $connection);
$this->assertAttributeEquals('/test-vhost', 'vhost', $connection);
}
}

0 comments on commit 90fed2a

Please sign in to comment.