Skip to content

Commit

Permalink
[exchange] Implementing exchange factory (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
actions-user committed Mar 29, 2023
1 parent 2493b92 commit eeda9cf
Show file tree
Hide file tree
Showing 164 changed files with 351 additions and 163 deletions.
2 changes: 1 addition & 1 deletion config/example.neon
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# @author Adam Kadlec <adam.kadlec@fastybird.com>
# @package FastyBird:DevicesModule!
# @subpackage config
# @since 0.1.0
# @since 1.0.0
#
# @date 25.11.20

Expand Down
11 changes: 10 additions & 1 deletion src/Commands/Connector.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @author Adam Kadlec <adam.kadlec@fastybird.com>
* @package FastyBird:DevicesModule!
* @subpackage Commands
* @since 0.60.0
* @since 1.0.0
*
* @date 31.05.22
*/
Expand All @@ -17,6 +17,7 @@

use BadMethodCallException;
use FastyBird\DateTimeFactory;
use FastyBird\Library\Exchange\Exchange as ExchangeExchange;
use FastyBird\Library\Metadata\Exceptions as MetadataExceptions;
use FastyBird\Library\Metadata\Types as MetadataTypes;
use FastyBird\Module\Devices\Connectors;
Expand Down Expand Up @@ -62,6 +63,9 @@ class Connector extends Console\Command\Command

private Log\LoggerInterface $logger;

/**
* @param array<ExchangeExchange\Factory> $exchangeFactories
*/
public function __construct(
private readonly Models\Connectors\ConnectorsRepository $connectorsRepository,
private readonly Models\Devices\DevicesRepository $devicesRepository,
Expand All @@ -71,6 +75,7 @@ public function __construct(
private readonly Utilities\ChannelPropertiesStates $channelPropertiesStateManager,
private readonly DateTimeFactory\Factory $dateTimeFactory,
private readonly EventLoop\LoopInterface $eventLoop,
private readonly array $exchangeFactories = [],
private readonly PsrEventDispatcher\EventDispatcherInterface|null $dispatcher = null,
Log\LoggerInterface|null $logger = null,
string|null $name = null,
Expand Down Expand Up @@ -319,6 +324,10 @@ private function executeConnector(Style\SymfonyStyle $io, Input\InputInterface $
}

$this->dispatcher?->dispatch(new Events\AfterConnectorStart($connector));

foreach ($this->exchangeFactories as $exchangeFactory) {
$exchangeFactory->create();
}
});

$this->eventLoop->addSignal(SIGINT, function () use ($connector, $service): void {
Expand Down
138 changes: 138 additions & 0 deletions src/Commands/Exchange.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
<?php declare(strict_types = 1);

/**
* Exchange.php
*
* @license More in LICENSE.md
* @copyright https://www.fastybird.com
* @author Adam Kadlec <adam.kadlec@fastybird.com>
* @package FastyBird:DevicesModule!
* @subpackage Commands
* @since 1.0.0
*
* @date 31.05.22
*/

namespace FastyBird\Module\Devices\Commands;

use FastyBird\Library\Exchange\Consumers as ExchangeConsumers;
use FastyBird\Library\Exchange\Exchange as ExchangeExchange;
use FastyBird\Library\Metadata\Types as MetadataTypes;
use FastyBird\Module\Devices\Consumers;
use FastyBird\Module\Devices\Events;
use Nette;
use Psr\EventDispatcher;
use Psr\Log;
use React\EventLoop;
use Symfony\Component\Console;
use Symfony\Component\Console\Input;
use Symfony\Component\Console\Output;
use Symfony\Component\Console\Style;
use Throwable;

/**
* Module exchange command
*
* @package FastyBird:RabbitMqPlugin!
* @subpackage Commands
*
* @author Adam Kadlec <adam.kadlec@fastybird.com>
*/
final class Exchange extends Console\Command\Command
{

use Nette\SmartObject;

public const NAME = 'fb:devices-module:exchange';

private Log\LoggerInterface $logger;

/**
* @param array<ExchangeExchange\Factory> $exchangeFactories
*/
public function __construct(
private readonly EventLoop\LoopInterface $eventLoop,
private readonly ExchangeConsumers\Container $consumer,
private readonly array $exchangeFactories = [],
private readonly EventDispatcher\EventDispatcherInterface|null $dispatcher = null,
Log\LoggerInterface|null $logger = null,
string|null $name = null,
)
{
parent::__construct($name);

$this->logger = $logger ?? new Log\NullLogger();
}

/**
* @throws Console\Exception\InvalidArgumentException
*/
protected function configure(): void
{
$this
->setName(self::NAME)
->setDescription('Devices module exchange');
}

/**
* @throws Console\Exception\InvalidArgumentException
*/
protected function execute(
Input\InputInterface $input,
Output\OutputInterface $output,
): int
{
$io = new Style\SymfonyStyle($input, $output);

if ($input->getOption('quiet') === false) {
$io->title('Devices module - exchange');

$io->note('This action will run module exchange service');
}

if ($input->getOption('no-interaction') === false) {
$question = new Console\Question\ConfirmationQuestion(
'Would you like to continue?',
false,
);

$continue = (bool) $io->askQuestion($question);

if (!$continue) {
return Console\Command\Command::SUCCESS;
}
}

try {
$this->dispatcher?->dispatch(new Events\ExchangeStartup());

foreach ($this->exchangeFactories as $exchangeFactory) {
$exchangeFactory->create();
}

$this->consumer->enable(Consumers\State::class);

$this->eventLoop->run();

} catch (Throwable $ex) {
// Log caught exception
$this->logger->error('An unhandled error occurred', [
'source' => MetadataTypes\ModuleSource::SOURCE_MODULE_DEVICES,
'type' => 'command',
'exception' => [
'message' => $ex->getMessage(),
'code' => $ex->getCode(),
],
]);

if ($input->getOption('quiet') === false) {
$io->error('Something went wrong, service could not be finished. Error was logged.');
}

return Console\Command\Command::FAILURE;
}

return self::SUCCESS;
}

}
2 changes: 1 addition & 1 deletion src/Commands/Initialize.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @author Adam Kadlec <adam.kadlec@fastybird.com>
* @package FastyBird:DevicesModule!
* @subpackage Commands
* @since 0.1.0
* @since 1.0.0
*
* @date 08.08.20
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Connectors/Connector.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @author Adam Kadlec <adam.kadlec@fastybird.com>
* @package FastyBird:DevicesModule!
* @subpackage Connectors
* @since 0.60.0
* @since 1.0.0
*
* @date 31.05.22
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Connectors/ConnectorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @author Adam Kadlec <adam.kadlec@fastybird.com>
* @package FastyBird:DevicesModule!
* @subpackage Connectors
* @since 0.67.0
* @since 1.0.0
*
* @date 03.07.22
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @author Adam Kadlec <adam.kadlec@fastybird.com>
* @package FastyBird:DevicesModule!
* @subpackage common
* @since 0.1.0
* @since 1.0.0
*
* @date 18.03.20
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Consumers/State.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @author Adam Kadlec <adam.kadlec@fastybird.com>
* @package FastyBird:DevicesModule!
* @subpackage Consumers
* @since 0.1.0
* @since 1.0.0
*
* @date 22.10.22
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Controllers/BaseV1.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @author Adam Kadlec <adam.kadlec@fastybird.com>
* @package FastyBird:DevicesModule!
* @subpackage Controllers
* @since 0.1.0
* @since 1.0.0
*
* @date 13.04.19
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Controllers/ChannelControlsV1.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @author Adam Kadlec <adam.kadlec@fastybird.com>
* @package FastyBird:DevicesModule!
* @subpackage Controllers
* @since 0.1.0
* @since 1.0.0
*
* @date 04.06.19
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Controllers/ChannelPropertiesV1.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @author Adam Kadlec <adam.kadlec@fastybird.com>
* @package FastyBird:DevicesModule!
* @subpackage Controllers
* @since 0.1.0
* @since 1.0.0
*
* @date 04.06.19
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Controllers/ChannelPropertyChildrenV1.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @author Adam Kadlec <adam.kadlec@fastybird.com>
* @package FastyBird:DevicesModule!
* @subpackage Controllers
* @since 0.33.0
* @since 1.0.0
*
* @date 09.02.22
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Controllers/ChannelsV1.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @author Adam Kadlec <adam.kadlec@fastybird.com>
* @package FastyBird:DevicesModule!
* @subpackage Controllers
* @since 0.1.0
* @since 1.0.0
*
* @date 13.04.19
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Controllers/ConnectorControlsV1.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @author Adam Kadlec <adam.kadlec@fastybird.com>
* @package FastyBird:DevicesModule!
* @subpackage Controllers
* @since 0.4.0
* @since 1.0.0
*
* @date 29.09.21
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Controllers/ConnectorPropertiesV1.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @author Adam Kadlec <adam.kadlec@fastybird.com>
* @package FastyBird:DevicesModule!
* @subpackage Controllers
* @since 0.31.0
* @since 1.0.0
*
* @date 08.02.22
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Controllers/ConnectorsV1.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @author Adam Kadlec <adam.kadlec@fastybird.com>
* @package FastyBird:DevicesModule!
* @subpackage Controllers
* @since 0.1.0
* @since 1.0.0
*
* @date 17.01.21
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Controllers/DeviceChildrenV1.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @author Adam Kadlec <adam.kadlec@fastybird.com>
* @package FastyBird:DevicesModule!
* @subpackage Controllers
* @since 0.1.0
* @since 1.0.0
*
* @date 04.06.19
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Controllers/DeviceControlsV1.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @author Adam Kadlec <adam.kadlec@fastybird.com>
* @package FastyBird:DevicesModule!
* @subpackage Controllers
* @since 0.4.0
* @since 1.0.0
*
* @date 29.09.21
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Controllers/DeviceParentsV1.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @author Adam Kadlec <adam.kadlec@fastybird.com>
* @package FastyBird:DevicesModule!
* @subpackage Controllers
* @since 0.42.0
* @since 1.0.0
*
* @date 25.03.22
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Controllers/DevicePropertiesV1.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @author Adam Kadlec <adam.kadlec@fastybird.com>
* @package FastyBird:DevicesModule!
* @subpackage Controllers
* @since 0.1.0
* @since 1.0.0
*
* @date 04.06.19
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Controllers/DevicePropertyChildrenV1.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @author Adam Kadlec <adam.kadlec@fastybird.com>
* @package FastyBird:DevicesModule!
* @subpackage Controllers
* @since 0.33.0
* @since 1.0.0
*
* @date 09.02.22
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Controllers/DevicesV1.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @author Adam Kadlec <adam.kadlec@fastybird.com>
* @package FastyBird:DevicesModule!
* @subpackage Controllers
* @since 0.1.0
* @since 1.0.0
*
* @date 13.04.19
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Controllers/Finders/TChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @author Adam Kadlec <adam.kadlec@fastybird.com>
* @package FastyBird:DevicesModule!
* @subpackage Controllers
* @since 0.1.0
* @since 1.0.0
*
* @date 13.04.19
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Controllers/Finders/TChannelProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @author Adam Kadlec <adam.kadlec@fastybird.com>
* @package FastyBird:DevicesModule!
* @subpackage Controllers
* @since 0.33.0
* @since 1.0.0
*
* @date 09.02.22
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Controllers/Finders/TConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @author Adam Kadlec <adam.kadlec@fastybird.com>
* @package FastyBird:DevicesModule!
* @subpackage Controllers
* @since 0.4.0
* @since 1.0.0
*
* @date 29.09.21
*/
Expand Down
Loading

0 comments on commit eeda9cf

Please sign in to comment.