Log parser errors via standard logger service
This commit is contained in:
parent
9aec5ba2c7
commit
4720991c8a
|
@ -10,12 +10,17 @@ use App\Contract\Service\Parser\ParserSelectorInterface;
|
|||
use App\Doctrine\Entity\PageView;
|
||||
use App\DTO\ParserResult;
|
||||
use App\Service\Repository\PageViewRepository;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Throwable;
|
||||
|
||||
use function count;
|
||||
use function get_class;
|
||||
|
||||
final class PageViewProcessor
|
||||
{
|
||||
public function __construct(
|
||||
private ClockInterface $clock,
|
||||
private LoggerInterface $logger,
|
||||
private PageViewRepository $pageViewRepository,
|
||||
private ParserSelectorInterface $parserSelector,
|
||||
private ParserResultProcessorInterface $parserResultManager,
|
||||
|
@ -24,11 +29,27 @@ final class PageViewProcessor
|
|||
|
||||
public function process(int $batchSize): void
|
||||
{
|
||||
$startDateTime = $this->clock->getCurrentDateTime();
|
||||
$this->logger->debug(message: 'Processing started...', context: ['start' => $startDateTime]);
|
||||
|
||||
$records = $this->pageViewRepository->getUnparsed(batchSize: $batchSize);
|
||||
|
||||
$this->logger->debug(
|
||||
message: 'Finished fetching page views from database',
|
||||
context: ['count' => count(value: $records)],
|
||||
);
|
||||
|
||||
/** @var PageView $pageView */
|
||||
foreach ($records as $pageView) {
|
||||
$this->logger->debug(
|
||||
message: 'Started processing a page view',
|
||||
context: ['id' => $pageView->getId(), 'createdAt' => $pageView->getCreatedAt()],
|
||||
);
|
||||
$parser = $this->parserSelector->findParser($pageView);
|
||||
$this->logger->debug(
|
||||
message: 'Selected parser',
|
||||
context: ['id' => $pageView->getId(), 'class' => null !== $parser ? get_class(object: $parser) : null],
|
||||
);
|
||||
|
||||
if (null !== $parser) {
|
||||
try {
|
||||
|
@ -44,6 +65,11 @@ final class PageViewProcessor
|
|||
|
||||
if (false === $parserResult->hasErrors()) {
|
||||
$this->parserResultManager->persist(parserResult: $parserResult);
|
||||
} else {
|
||||
$this->logger->error(
|
||||
message: 'Parsing has failed',
|
||||
context: ['id' => $pageView->getId(), 'errors' => $parserResult->getErrors()],
|
||||
);
|
||||
}
|
||||
|
||||
$currentDateTime = $this->clock->getCurrentDateTime();
|
||||
|
@ -53,5 +79,11 @@ final class PageViewProcessor
|
|||
errors: $parserResult->getErrors(),
|
||||
);
|
||||
}
|
||||
|
||||
$endDateTime = $this->clock->getCurrentDateTime();
|
||||
$this->logger->debug(
|
||||
message: 'Processing complete',
|
||||
context: ['start' => $startDateTime, 'end' => $endDateTime],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
Reference in New Issue