fix: resolve messenger worker OOM by clearing EntityManager and disabling debug

Clear Doctrine identity map after each film import to prevent memory
accumulation. Run messenger with --no-debug and higher PHP memory_limit
to avoid profiling overhead in dev.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
thibaud-leclere
2026-03-31 21:34:11 +02:00
parent 6a844542ad
commit a37ac1debd
3 changed files with 12 additions and 22 deletions

View File

@@ -5,9 +5,7 @@ declare(strict_types=1);
namespace App\MessageHandler;
use App\Entity\Import;
use App\Entity\Notification;
use App\Entity\UserMovie;
use App\Exception\GatewayException;
use App\Gateway\LtbxdGateway;
use App\Message\ImportFilmsBatchMessage;
use App\Repository\ImportRepository;
@@ -51,7 +49,8 @@ readonly class ImportFilmsBatchMessageHandler
}
$batch = array_slice($ltbxdMovies, $message->offset, $message->limit);
$user = $import->getUser();
$userId = $import->getUser()->getId();
$importId = $import->getId();
foreach ($batch as $ltbxdMovie) {
try {
@@ -63,6 +62,7 @@ readonly class ImportFilmsBatchMessageHandler
$this->actorSyncer->syncActorsForMovie($movie);
$user = $this->em->getReference(\App\Entity\User::class, $userId);
$existingLink = $this->em->getRepository(UserMovie::class)->findOneBy([
'user' => $user,
'movie' => $movie,
@@ -78,11 +78,14 @@ readonly class ImportFilmsBatchMessageHandler
} catch (\Throwable $e) {
$this->logger->warning('Failed to import film', [
'film' => $ltbxdMovie->getName(),
'importId' => $import->getId(),
'importId' => $importId,
'error' => $e->getMessage(),
]);
$this->importRepository->incrementFailedFilms($import);
}
$this->em->clear();
$import = $this->em->getRepository(Import::class)->find($importId);
}
$processedBatches = $this->importRepository->incrementProcessedBatches($import);
@@ -94,17 +97,6 @@ readonly class ImportFilmsBatchMessageHandler
$import->setStatus(Import::STATUS_COMPLETED);
$import->setCompletedAt(new \DateTimeImmutable());
$this->em->flush();
$imported = $import->getTotalFilms() - $import->getFailedFilms();
$notification = new Notification();
$notification->setUser($user);
$notification->setMessage(sprintf(
'Import terminé : %d/%d films importés.',
$imported,
$import->getTotalFilms()
));
$this->em->persist($notification);
$this->em->flush();
}
}
}

View File

@@ -5,7 +5,6 @@ declare(strict_types=1);
namespace App\MessageHandler;
use App\Entity\Import;
use App\Entity\Notification;
use App\Gateway\LtbxdGateway;
use App\Message\ImportFilmsBatchMessage;
use App\Message\ProcessImportMessage;
@@ -72,12 +71,6 @@ readonly class ProcessImportMessageHandler
$import->setStatus(Import::STATUS_FAILED);
$this->em->flush();
$notification = new Notification();
$notification->setUser($import->getUser());
$notification->setMessage('L\'import a échoué.');
$this->em->persist($notification);
$this->em->flush();
}
}
}