Files
guides/projects/priceservice/src/MessageHandler/TriggerPriceImportHandler.php
2026-06-03 22:05:20 +02:00

46 lines
1.5 KiB
PHP

<?php
namespace App\MessageHandler;
use App\Message\TriggerPriceImport;
use App\Service\Prices\RedisImportService;
use Psr\Log\LoggerInterface;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
use Symfony\Component\Stopwatch\Stopwatch;
use Throwable;
#[AsMessageHandler]
final readonly class TriggerPriceImportHandler
{
public function __construct(
private RedisImportService $importService,
private LoggerInterface $logger,
private readonly Stopwatch $stopwatch
) {}
public function __invoke(TriggerPriceImport $message): void
{
$ts = $message->timestamp ?? 'undefined';
$this->stopwatch->start('price_import_trigger');
print('[Handler] Start Import with client timestamp: '.$ts."\n");
try {
$this->importService->writePrices();
} catch (Throwable $exception) {
$this->logger->error('[Handler] Import error: ', [
'timestamp' => $ts,
'message' => $exception->getMessage(),
'exception' => $exception,
]);
print('[Handler] Import error: '.$exception->getMessage()."\n");
throw $exception;
}
$event = $this->stopwatch->stop('price_import_trigger');
$duration = number_format(($event?->getDuration() ?? 0) / 1000, 2);
print('[Handler] Import complete with client timestamp: '.$ts."\n");
print('[Handler] Duration: '.$duration." s\n\n");
}
}