Bugfixes, reconnection in client, readme
This commit is contained in:
@ -30,6 +30,8 @@ class CommandBusClient implements CommandBusInterface
|
||||
|
||||
private string $address;
|
||||
|
||||
private bool $autoReconnect = true;
|
||||
|
||||
public function __construct(?CommandRegistry $commandRegistry = null, ?ConnectorInterface $connector = null)
|
||||
{
|
||||
$this->commandRegistry = $commandRegistry;
|
||||
@ -48,6 +50,7 @@ class CommandBusClient implements CommandBusInterface
|
||||
|
||||
public function close(): void
|
||||
{
|
||||
$this->autoReconnect = false;
|
||||
$this->connection->close();
|
||||
$this->connection->removeAllListeners();
|
||||
$this->connection = null;
|
||||
@ -56,15 +59,26 @@ class CommandBusClient implements CommandBusInterface
|
||||
private function reconnect(): void
|
||||
{
|
||||
if ($this->connection) {
|
||||
$this->close();
|
||||
$this->connection->close();
|
||||
$this->connection->removeAllListeners();
|
||||
$this->connection = null;
|
||||
}
|
||||
|
||||
$this->connector->connect($this->address)->then(
|
||||
function (DuplexStreamInterface $connection) {
|
||||
$this->connection = $connection;
|
||||
$this->emit(self::EVENT_CONNECTED);
|
||||
$connection->on('error', function () {
|
||||
$this->emit(self::EVENT_DISCONNECTED);
|
||||
if ($this->autoReconnect) {
|
||||
Loop::addTimer(1, $this->reconnect(...));
|
||||
}
|
||||
});
|
||||
$connection->on('close', function () {
|
||||
$this->emit(self::EVENT_DISCONNECTED);
|
||||
if ($this->autoReconnect) {
|
||||
Loop::addTimer(1, $this->reconnect(...));
|
||||
}
|
||||
});
|
||||
$connection->on('data', function ($data) use ($connection) {
|
||||
try {
|
||||
@ -128,6 +142,12 @@ class CommandBusClient implements CommandBusInterface
|
||||
*/
|
||||
public function execute(string $command, array $context): PromiseInterface
|
||||
{
|
||||
if (!$this->connection) {
|
||||
return new Promise(function (callable $resolve) {
|
||||
throw new \RuntimeException("Not connected to command bus.");
|
||||
});
|
||||
}
|
||||
|
||||
$deferred = new Deferred();
|
||||
|
||||
$message = new Message(Message::MSGTYPE_EXECUTE, [
|
||||
|
Reference in New Issue
Block a user