Skip to content

Commit

Permalink
feat: reset logger in long running task
Browse files Browse the repository at this point in the history
  • Loading branch information
ckrack committed Feb 24, 2024
1 parent 80c9a24 commit 35afec2
Showing 1 changed file with 24 additions and 14 deletions.
38 changes: 24 additions & 14 deletions src/Console/TcpServerStartConsoleCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Console;

use App\Command\AddDataCommand;
use Monolog\Logger;
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
Expand Down Expand Up @@ -59,27 +60,14 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$jsonData = $this->handleClients($server);
if (is_array($jsonData)) {
$this->dispatchAddDataCommand($jsonData);
$this->resetLogger();
}
} catch (\Throwable $exception) {
$this->logger->error('Exception while handling clients: '.$exception->getMessage().$exception->getFile().$exception->getLine());
}
}
}

/**
* @param array<array-key, mixed> $jsonData
*
* @throws \JsonException
*/
private function dispatchAddDataCommand(array $jsonData): void
{
if (!array_key_exists('token', $jsonData)) {
throw new \InvalidArgumentException('No token in data');
}

$this->messageBus->dispatch(new AddDataCommand(new Ulid($jsonData['token']), json_encode($jsonData, JSON_THROW_ON_ERROR)));
}

/**
* @param false|resource $server
*
Expand Down Expand Up @@ -139,4 +127,26 @@ private function readJsonFromClient($client): string

return $jsonRaw;
}

/**
* @param array<array-key, mixed> $jsonData
*
* @throws \JsonException
*/
private function dispatchAddDataCommand(array $jsonData): void
{
if (!array_key_exists('token', $jsonData)) {
throw new \InvalidArgumentException('No token in data');
}

$this->messageBus->dispatch(new AddDataCommand(new Ulid($jsonData['token']), json_encode($jsonData, JSON_THROW_ON_ERROR)));
}

public function resetLogger(): void
{
if (!$this->logger instanceof Logger) {
return;
}
$this->logger->reset();
}
}

0 comments on commit 35afec2

Please sign in to comment.