From 6b85df51c2bb3d0228f430eb066952ca890c3f81 Mon Sep 17 00:00:00 2001 From: Nicolas Joubert Date: Fri, 6 Nov 2026 14:15:33 +0100 Subject: [PATCH] #307 - Write the buffered logs at the end of the process of the execution only Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 3 ++ .../04-process_executions_and_logs.md | 3 +- .../ProcessEventSubscriber.php | 10 +++++- .../ProcessEventSubscriberTest.php | 32 +++++++++++++++++-- ...octrineProcessHandlerEntityManagerTest.php | 2 +- 5 files changed, 45 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 54808110..7555363f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,9 @@ Latest ------ +## Fixes +* [#307](https://github.com/cleverage/ui-process-bundle/issues/307) The buffered log records are written to the database at the end (or failure) of the process of the execution only, no longer at the end of each of its sub-processes (e.g. a `ProcessExecutorTask` in a loop: one transaction per iteration, 1000 iterations took 10.5 s instead of 2.9 s). Update documentation, add tests. + v3.6 ------ diff --git a/docs/reference/04-process_executions_and_logs.md b/docs/reference/04-process_executions_and_logs.md index f5cafc55..19f7418f 100644 --- a/docs/reference/04-process_executions_and_logs.md +++ b/docs/reference/04-process_executions_and_logs.md @@ -195,7 +195,8 @@ deeper values are replaced by `Over 9 levels deep, aborting normalization`): an aggregator) was stored whole, and over the maximum packet size of the database, it made the insertion of the log records written with it fail. The objects of the context are stored with their public properties, without limit. -Records are buffered and inserted every 500 records, and at the end (or failure) of the process. A record counted in +Records are buffered and inserted every 500 records, and at the end (or failure) of the process (of the process of +the execution, not of each of its sub-processes run in the same PHP process). A record counted in the report of the execution (from `report_increment_level`, warnings by default) is inserted right away with the buffered ones, unless changes of the process tasks are pending in the entity manager (they would be written with it, see below): it is then inserted with the next ones. After a fatal error (e.g. maximum execution time exceeded), neither the diff --git a/src/EventSubscriber/ProcessEventSubscriber.php b/src/EventSubscriber/ProcessEventSubscriber.php index 080bfb1f..18a7b450 100644 --- a/src/EventSubscriber/ProcessEventSubscriber.php +++ b/src/EventSubscriber/ProcessEventSubscriber.php @@ -119,8 +119,16 @@ private function dispatchEnded(ProcessEvent $event): void } } - public function flushDoctrineLogs(): void + /** + * Only at the end of the process of the current execution: the end of each of its sub-processes (e.g. a + * ProcessExecutorTask in a loop) wrote the buffered log records, in a transaction of its own. + */ + public function flushDoctrineLogs(ProcessEvent $event): void { + $currentProcessExecution = $this->processExecutionManager->getCurrentProcessExecution(); + if ($currentProcessExecution instanceof ProcessExecution && $event->getProcessCode() !== $currentProcessExecution->getCode()) { + return; + } $this->doctrineProcessHandler->flush(); } } diff --git a/tests/EventSubscriber/ProcessEventSubscriberTest.php b/tests/EventSubscriber/ProcessEventSubscriberTest.php index 0f6d5ee2..78719139 100644 --- a/tests/EventSubscriber/ProcessEventSubscriberTest.php +++ b/tests/EventSubscriber/ProcessEventSubscriberTest.php @@ -277,7 +277,10 @@ public function testFlushDoctrineLogs(): void $entityManager = $this->createMock(EntityManagerInterface::class); $entityManager->method('getUnitOfWork')->willReturn($unitOfWork); $entityManager->expects(self::once())->method('persist')->with(self::isInstanceOf(LogRecord::class)); - $entityManager->expects(self::atLeastOnce())->method('flush'); + $flushes = 0; + $entityManager->method('flush')->willReturnCallback(static function () use (&$flushes): void { + ++$flushes; + }); $doctrineProcessHandler = new DoctrineProcessHandler(); $doctrineProcessHandler->setEntityManager($entityManager); @@ -291,10 +294,35 @@ public function testFlushDoctrineLogs(): void $doctrineProcessHandler, $processExecutionManager ); - $subscriber->flushDoctrineLogs(); + // End of a sub-process (e.g. ProcessExecutorTask): written at the end of the process of the execution + $subscriber->flushDoctrineLogs(new ProcessEvent('sub.process')); + self::assertSame(0, $flushes); + + $subscriber->flushDoctrineLogs(new ProcessEvent('test.process')); + self::assertSame(1, $flushes); $doctrineProcessHandler->disable(); // no flush on destruction, during another test } + /** + * Without current process execution (none started), the pending log records and changes are still written. + */ + public function testFlushDoctrineLogsWithoutProcessExecution(): void + { + $entityManager = $this->createMock(EntityManagerInterface::class); + $entityManager->method('isOpen')->willReturn(true); + $entityManager->expects(self::once())->method('flush'); + $doctrineProcessHandler = new DoctrineProcessHandler(); + $doctrineProcessHandler->setEntityManager($entityManager); + $processExecutionManager = new ProcessExecutionManager($this->createRepositoryStub()); + + (new ProcessEventSubscriber( + new ProcessHandler('/var/log/process', $processExecutionManager), + $doctrineProcessHandler, + $processExecutionManager + ))->flushDoctrineLogs(new ProcessEvent('test.process')); + $doctrineProcessHandler->disable(); + } + private function createSubscriber( ProcessHandler $processHandler, ProcessExecutionManager $processExecutionManager, diff --git a/tests/Monolog/Handler/DoctrineProcessHandlerEntityManagerTest.php b/tests/Monolog/Handler/DoctrineProcessHandlerEntityManagerTest.php index c526a638..194f2ae6 100644 --- a/tests/Monolog/Handler/DoctrineProcessHandlerEntityManagerTest.php +++ b/tests/Monolog/Handler/DoctrineProcessHandlerEntityManagerTest.php @@ -184,7 +184,7 @@ public function testFlushFailureAtTheEndOfTheProcess(): void $connection->executeStatement('ALTER TABLE log_record RENAME TO log_record_renamed'); try { // ProcessEvent::EVENT_PROCESS_ENDED listeners - $subscriber->flushDoctrineLogs(); + $subscriber->flushDoctrineLogs($event); // Reset right away for the process tasks self::assertTrue($this->entityManager->isOpen()); $subscriber->success($event);