Skip to content

Commit

Permalink
Small cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
akadlec committed Jun 19, 2022
1 parent c241f99 commit 95f5429
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/Commands/ConnectorCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ protected function execute(Input\InputInterface $input, Output\OutputInterface $
$this->connector->execute($connector);
});

$this->eventLoop->addSignal(SIGINT, function (int $signal) {
$this->eventLoop->addSignal(SIGINT, function (int $signal): void {
$this->connector->terminate();
});

Expand Down
44 changes: 39 additions & 5 deletions src/Connectors/Connector.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

namespace FastyBird\DevicesModule\Connectors;

use FastyBird\DateTimeFactory;
use FastyBird\DevicesModule;
use FastyBird\DevicesModule\Connectors;
use FastyBird\DevicesModule\Entities;
Expand Down Expand Up @@ -46,6 +47,8 @@ final class Connector

use Nette\SmartObject;

private const SHUTDOWN_WAITING_DELAY = 3;

/** @var bool */
private bool $stopped = true;

Expand Down Expand Up @@ -73,6 +76,9 @@ final class Connector
/** @var Models\States\ConnectorPropertiesManager */
private Models\States\ConnectorPropertiesManager $connectorPropertiesStateManager;

/** @var DateTimeFactory\DateTimeFactory */
private DateTimeFactory\DateTimeFactory $dateTimeFactory;

/** @var EventLoop\LoopInterface */
private EventLoop\LoopInterface $eventLoop;

Expand All @@ -84,6 +90,7 @@ public function __construct(
Models\Connectors\Properties\IPropertiesManager $connectorPropertiesManager,
Models\States\ConnectorPropertiesRepository $connectorPropertiesStateRepository,
Models\States\ConnectorPropertiesManager $connectorPropertiesStateManager,
DateTimeFactory\DateTimeFactory $dateTimeFactory,
EventLoop\LoopInterface $eventLoop,
?Log\LoggerInterface $logger = null
) {
Expand All @@ -92,6 +99,7 @@ public function __construct(
$this->connectorPropertiesStateRepository = $connectorPropertiesStateRepository;
$this->connectorPropertiesStateManager = $connectorPropertiesStateManager;

$this->dateTimeFactory = $dateTimeFactory;
$this->eventLoop = $eventLoop;

$this->logger = $logger ?? new Log\NullLogger();
Expand All @@ -115,7 +123,7 @@ public function execute(MetadataEntities\Modules\DevicesModule\IConnectorEntity

foreach ($this->connectors as $service) {
if ($connector->getType() === $service->getType()) {
$this->logger->debug('Preparing connector to start', [
$this->logger->debug('Starting connector...', [
'source' => 'devices-module',
'type' => 'connector',
]);
Expand All @@ -124,6 +132,7 @@ public function execute(MetadataEntities\Modules\DevicesModule\IConnectorEntity
$this->service = $service;
$this->service->initialize($connector);

// Start connector service
$this->service->execute();

$this->stopped = false;
Expand Down Expand Up @@ -156,11 +165,36 @@ public function terminate(): void
'type' => 'connector',
]);

if ($this->service !== null) {
$this->service->terminate();
}
try {
if ($this->service !== null) {
$this->service->terminate();

$this->setConnectorState(ConnectionStateType::get(ConnectionStateType::STATE_STOPPED));
$now = $this->dateTimeFactory->getNow();

$waitingForClosing = true;

# Wait until connector is fully terminated
while (
$waitingForClosing
&& ($this->dateTimeFactory->getNow()->getTimestamp() - $now->getTimestamp()) < self::SHUTDOWN_WAITING_DELAY
) {
if (!$this->service->hasUnfinishedTasks()) {
$waitingForClosing = false;
}
}
}

$this->setConnectorState(ConnectionStateType::get(ConnectionStateType::STATE_STOPPED));
} catch (Throwable $ex) {
$this->logger->error('Connector couldn\'t be stopped. An unexpected error occurred', [
'source' => 'devices-module',
'type' => 'connector',
'exception' => [
'message' => $ex->getMessage(),
'code' => $ex->getCode(),
],
]);
}
}

/**
Expand Down
5 changes: 5 additions & 0 deletions src/Connectors/IConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ public function execute(): void;
*/
public function terminate(): void;

/**
* @return bool
*/
public function hasUnfinishedTasks(): bool;

/**
* @param MetadataEntities\Modules\DevicesModule\IConnectorEntity $connector
*
Expand Down
11 changes: 11 additions & 0 deletions src/DataStorage/Reader.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,17 @@ public function read(): void
$dataConfiguration = $this->filesystem->read(DevicesModule\Constants::CONFIGURATION_FILE_FILENAME);
$dataConfiguration = Utils\Json::decode($dataConfiguration, Utils\Json::FORCE_ARRAY);

$this->connectorsRepository->reset();
$this->connectorPropertiesRepository->reset();
$this->connectorControlsRepository->reset();
$this->devicesRepository->reset();
$this->devicePropertiesRepository->reset();
$this->deviceControlsRepository->reset();
$this->deviceAttributesRepository->reset();
$this->channelsRepository->reset();
$this->channelPropertiesRepository->reset();
$this->channelControlsRepository->reset();

if (!is_array($dataConfiguration)) {
return;
}
Expand Down
9 changes: 9 additions & 0 deletions src/Translations/devices-module.en_US.neon
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,12 @@ base:
uniqueAttribute:
heading : "Attribute not unique"
message : "Provided attribute is not unique"

commands:
connector:
inputs:
connector:
title: "Connector uuid identifier"

validation:
identifierNotValid: "Provided connector uuid identifier is not valid uuid string"

0 comments on commit 95f5429

Please sign in to comment.