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 4bd9d44
Showing 1 changed file with 33 additions and 22 deletions.
55 changes: 33 additions & 22 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 All @@ -20,9 +21,10 @@
class TcpServerStartConsoleCommand extends Command
{
public function __construct(
private readonly LoggerInterface $logger,
private readonly LoggerInterface $logger,
private readonly MessageBusInterface $messageBus,
) {
)
{
parent::__construct();
}

Expand All @@ -34,7 +36,7 @@ protected function configure(): void
'p',
InputOption::VALUE_REQUIRED,
'Port to use',
(int) $_ENV['TCP_API_PORT']);
(int)$_ENV['TCP_API_PORT']);
}

protected function execute(InputInterface $input, OutputInterface $output): int
Expand All @@ -43,43 +45,30 @@ protected function execute(InputInterface $input, OutputInterface $output): int
set_time_limit(0);

// open TCP server
$server = stream_socket_server('tcp://0.0.0.0:'.$input->getOption('port'), $errno, $errorMessage);
$server = stream_socket_server('tcp://0.0.0.0:' . $input->getOption('port'), $errno, $errorMessage);

if (!$server) {
$this->logger->error('Could not bind to socket: '.$errorMessage);
$this->logger->error('Could not bind to socket: ' . $errorMessage);
throw new \RuntimeException(sprintf('Could not bind to socket: %s', $errorMessage));
}

$io = new SymfonyStyle($input, $output);
$io->success('Socket server open. Listening on: '.$input->getOption('port'));
$io->success('Socket server open. Listening on: ' . $input->getOption('port'));
$this->logger->info('socket server open', [$input->getOption('port')]);

while (true) { // @phpstan-ignore-line
try {
$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());
$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 @@ -127,7 +116,7 @@ private function readJsonFromClient($client): string
// read from input until blank line
$jsonRaw = '';
while ($tcpInput = fread($client, 1024)) {
$this->logger->info('Input: '.$tcpInput);
$this->logger->info('Input: ' . $tcpInput);
$jsonRaw .= $tcpInput;

// get input until json closing bracket
Expand All @@ -139,4 +128,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 4bd9d44

Please sign in to comment.