-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from Vinelab/improve/healthcheck
Revamp the health check mechanism
- Loading branch information
Showing
10 changed files
with
242 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,12 +2,13 @@ | |
|
||
namespace Vinelab\Bowler\Console\Commands; | ||
|
||
use Vinelab\Bowler\Consumer; | ||
use Vinelab\Bowler\Connection; | ||
use Illuminate\Console\Command; | ||
use Vinelab\Bowler\Facades\Registrator; | ||
use Vinelab\Bowler\Exceptions\UnregisteredQueueException; | ||
use Vinelab\Bowler\Connection; | ||
use Vinelab\Bowler\Consumer; | ||
use Vinelab\Bowler\Exceptions\Handler as BowlerExceptionHandler; | ||
use Vinelab\Bowler\Exceptions\UnregisteredQueueException; | ||
use Vinelab\Bowler\Facades\Registrator; | ||
use Vinelab\Bowler\RegisterQueues; | ||
|
||
/** | ||
* @author Ali Issa <[email protected]> | ||
|
@@ -17,9 +18,11 @@ class ConsumeCommand extends Command | |
{ | ||
protected $registerQueues; | ||
|
||
public function __construct() | ||
public function __construct(RegisterQueues $registrator) | ||
{ | ||
parent::__construct(); | ||
|
||
$this->registrator = $registrator; | ||
} | ||
|
||
/** | ||
|
@@ -70,8 +73,8 @@ public function handle() | |
$deadLetterRoutingKey = $this->option('deadLetterRoutingKey'); | ||
$messageTTL = ($ttl = $this->option('messageTTL')) ? (int) $ttl : null; | ||
|
||
require app_path().'/Messaging/queues.php'; | ||
$handlers = Registrator::getHandlers(); | ||
$this->loadQueuesDefinitions(); | ||
$handlers = $this->registrator->getHandlers(); | ||
|
||
foreach ($handlers as $handler) { | ||
if ($handler->queueName == $queueName) { | ||
|
@@ -83,17 +86,30 @@ public function handle() | |
} | ||
|
||
$bowlerConsumer = new Consumer(app(Connection::class), $handler->queueName, $exchangeName, $exchangeType, $bindingKeys, $passive, $durable, $autoDelete); | ||
|
||
if ($deadLetterQueueName) { | ||
|
||
// If configured as options and deadLetterExchangeName is not specified, default to deadLetterQueueName. | ||
$deadLetterExchangeName = isset($deadLetterExchangeName) ? $deadLetterExchangeName : $deadLetterQueueName; | ||
|
||
$bowlerConsumer->configureDeadLettering($deadLetterQueueName, $deadLetterExchangeName, $deadLetterExchangeType, $deadLetterRoutingKey, $messageTTL); | ||
} | ||
|
||
$bowlerConsumer->listenToQueue($handler->className, app(BowlerExceptionHandler::class)); | ||
} | ||
} | ||
|
||
throw new UnregisteredQueueException('No registered queue found with name '.$queueName.'.'); | ||
} | ||
|
||
public function loadQueuesDefinitions() | ||
{ | ||
$path = app_path().'/Messaging/queues.php'; | ||
|
||
if (!file_exists($path)) { | ||
return $this->error('Queues definitions file not found. Please create it at '.$path); | ||
} | ||
|
||
require $path; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,44 +3,46 @@ | |
namespace Vinelab\Bowler\Console\Commands; | ||
|
||
use ErrorException; | ||
use Vinelab\Bowler\Connection; | ||
use Illuminate\Console\Command; | ||
use PhpAmqpLib\Exception\AMQPProtocolChannelException; | ||
use Vinelab\Bowler\Connection; | ||
use Vinelab\Bowler\Traits\ConsumerTagTrait; | ||
|
||
/** | ||
* @author Abed Halawi <[email protected]> | ||
*/ | ||
class ConsumerHealthCheckCommand extends Command | ||
{ | ||
use ConsumerTagTrait; | ||
|
||
/** | ||
* The console command name. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'bowler:healthcheck:consumer | ||
{queueName : The queue name} | ||
{--c|consumers=1 : The expected number of consumers to be connected to the queue specified by queueName}'; | ||
{queueName : The queue name}'; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Check the health of connected consumers to a queue, with a minimum of 1 connection.'; | ||
protected $description = 'Check the health of connected consumers to a given queue.'; | ||
|
||
/** | ||
* Run the command. | ||
*/ | ||
public function handle() | ||
{ | ||
$queueName = $this->argument('queueName'); | ||
$expectedConsumers = (int) $this->option('consumers'); | ||
|
||
// may or may not be able to connect | ||
try { | ||
$connection = app(Connection::class); | ||
} catch (ErrorException $e) { | ||
$this->error('Unable to connect to RabbitMQ.'); | ||
|
||
return 1; | ||
} | ||
|
||
|
@@ -56,15 +58,31 @@ public function handle() | |
[] | ||
); | ||
|
||
// consumer count and minimum consumers connected should match | ||
if ($consumerCount !== $expectedConsumers) { | ||
$this->error('Health check failed. Minimum consumer count not met: expected '.$expectedConsumers.' got '.$consumerCount); | ||
$response = $connection->fetchQueueConsumers($queueName); | ||
|
||
if ($response && isset($response->consumer_details) && !empty($response->consumer_details)) { | ||
// read consumer tag | ||
$tag = $this->readConsumerTag(); | ||
|
||
// find consumer tag within the list of returned consumers | ||
foreach ($response->consumer_details as $consumer) { | ||
if (isset($consumer->consumer_tag) && $consumer->consumer_tag == $tag) { | ||
$this->info('Healthy consumer with tag '.$tag); | ||
|
||
return 0; | ||
} | ||
} | ||
|
||
$this->error('Health check failed! Could not find consumer with tag "'.$tag.'"'); | ||
|
||
return 1; | ||
} | ||
|
||
$this->info('Consumers healthy with '.$consumerCount.' live connections.'); | ||
$this->error('No consumers connected to queue "'.$queueName.'"'); | ||
|
||
return 1; | ||
} catch (AMQPProtocolChannelException $e) { | ||
switch($e->getCode()) { | ||
switch ($e->getCode()) { | ||
case 404: | ||
$this->error('Queue with name '.$queueName.' does not exist.'); | ||
break; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace Vinelab\Bowler\Traits; | ||
|
||
use Storage; | ||
|
||
/** | ||
* @author Abed Halawi <[email protected]> | ||
*/ | ||
trait ConsumerTagTrait | ||
{ | ||
public function getConsumerTagFilename() | ||
{ | ||
return 'rabbitmq-consumer.tag'; | ||
} | ||
|
||
private function writeConsumerTag($tag) | ||
{ | ||
Storage::disk('local')->put($this->getConsumerTagFilename(), $tag); | ||
} | ||
|
||
public function readConsumerTag() | ||
{ | ||
return Storage::disk('local')->get($this->getConsumerTagFilename()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
namespace Vinelab\Bowler\Tests; | ||
|
||
use Mockery as M; | ||
use Vinelab\Bowler\Connection; | ||
use Vinelab\Http\Client as HTTPClient; | ||
|
||
/** | ||
* @author Abed Halawi <[email protected]> | ||
*/ | ||
class ConnectionTest extends TestCase | ||
{ | ||
public function tearDown() | ||
{ | ||
M::close(); | ||
} | ||
|
||
public function test_fetching_consumers_default() | ||
{ | ||
$queueName = 'the-queue'; | ||
$mClient = M::mock(HTTPClient::class); | ||
$request = [ | ||
'url' => 'localhost:15672/api/queues/%2F/'.$queueName, | ||
'params' => ['columns' => 'consumer_details.consumer_tag'], | ||
'auth' => [ | ||
'username' => 'guest', | ||
'password' => 'guest', | ||
], | ||
]; | ||
|
||
$mClient->shouldReceive('get')->once()->with($request)->andReturn($mClient); | ||
$mClient->shouldReceive('json')->once()->withNoArgs()->andReturn('response'); | ||
|
||
$this->app->bind(HTTPClient::class, function () use ($mClient) { | ||
return $mClient; | ||
}); | ||
|
||
$mConnection = M::mock(Connection::class)->makePartial(); | ||
$this->app->bind(Connection::class, function () use ($mConnection) { | ||
return $mConnection; | ||
}); | ||
|
||
$connection = $this->app[Connection::class]; | ||
$response = $connection->fetchQueueConsumers($queueName); | ||
|
||
$this->assertEquals('response', $response); | ||
} | ||
} |
Oops, something went wrong.