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

@@ -11,6 +11,11 @@ services:
ports: ports:
- "${PORT_80:-80}:80" - "${PORT_80:-80}:80"
messenger:
command: ["php", "-d", "memory_limit=512M", "bin/console", "messenger:consume", "async", "--time-limit=3600", "--memory-limit=256M", "-vv", "--no-debug"]
volumes:
- .:/app
database: database:
ports: ports:
- "0.0.0.0:5432:5432" - "0.0.0.0:5432:5432"

View File

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