diff --git a/CHANGELOG.md b/CHANGELOG.md index 2ed37a4b..40dd5258 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ Latest * [#392](https://github.com/cleverage/process-bundle/issues/392) Documentation: reset of the process configurations between two messages of a worker (`kernel.reset`, #355), permissions of the JSON buffer of `process_launcher`, `ProcessManager` subclasses in `UPGRADE.md` (v5.5), broken link in the CHANGELOG. ## Fixes +* [#394](https://github.com/cleverage/process-bundle/issues/394) `ProcessManager::execute()`: an exception thrown by a listener of `cleverage_process.fail` (`ProcessEvent::EVENT_PROCESS_FAILED`) is logged as an error and no longer replaces the error of the process, which is rethrown to the command, the parent process or Messenger. The debug message "has error outputs but it's error strategy 'stop' implies they will never be reached" is removed: the error branch of a task with the `stop` strategy is executed before the process stops. Update documentation, add tests. * [#388](https://github.com/cleverage/process-bundle/issues/388) The temporary files of `file_splitter`, `csv_splitter` and of the JSON buffer of `process_launcher` (#343) are created with the `0077` umask: they are no longer readable by other users before their permissions are set to `0600`. Add tests. * [#390](https://github.com/cleverage/process-bundle/issues/390) `cleverage:process:execute` with `--output-format=json-stream` and STDOUT (#353): the invalid UTF-8 sequences of the output are replaced, and an output that still cannot be encoded as JSON (e.g. `NAN`) is reported on the error output instead of throwing a `JsonException` that made the command fail after the execution and skipped the next processes. `UPGRADE.md` (v5.5): the `dump` format without `symfony/var-dumper` and the unknown formats make the command fail before any execution. Update documentation, add tests. diff --git a/docs/04-advanced_workflow.md b/docs/04-advanced_workflow.md index ed5b9dde..9f78909d 100644 --- a/docs/04-advanced_workflow.md +++ b/docs/04-advanced_workflow.md @@ -222,6 +222,10 @@ to the process code, input, context and, depending on the event, output or error Those events are also dispatched for sub-processes executed with the `ProcessExecutorTask`. +An exception thrown by a listener of `cleverage_process.fail` is logged as an error (`A listener of the process +failure has failed`) and does not replace the error of the process, which is rethrown (the following listeners of the +event are not called). + Another event is dispatched once by the `cleverage:process:execute` command, before executing any process: `CleverAge\ProcessBundle\Event\ConsoleProcessEvent`. It is dispatched without a specific name, so listeners must use the class name as event name. It gives access to the console input/output objects, and to the process input and diff --git a/src/Manager/ProcessManager.php b/src/Manager/ProcessManager.php index cff91380..fde61a16 100644 --- a/src/Manager/ProcessManager.php +++ b/src/Manager/ProcessManager.php @@ -179,10 +179,19 @@ public function execute(string $processCode, mixed $input = null, array $context $this->processLogger->critical('Critical process failure', [ 'error' => $error->getMessage(), ]); - $this->eventDispatcher->dispatch( - new ProcessEvent($processCode, $input, $context, null, $error), - ProcessEvent::EVENT_PROCESS_FAILED - ); + try { + $this->eventDispatcher->dispatch( + new ProcessEvent($processCode, $input, $context, null, $error), + ProcessEvent::EVENT_PROCESS_FAILED + ); + } catch (\Throwable $listenerError) { + // The error of a listener must not replace the error of the process (command, parent process, + // Messenger): logged, the error of the process is thrown + $this->processLogger->error('A listener of the process failure has failed', [ + 'error' => $listenerError->getMessage(), + 'exception' => $listenerError, + ]); + } throw $error; } finally { @@ -335,12 +344,6 @@ protected function initialize(TaskConfiguration $taskConfiguration): void $previousTaskConfiguration = $this->taskConfiguration; $this->taskConfiguration = $taskConfiguration; - if (TaskConfiguration::STRATEGY_STOP === $taskConfiguration->getErrorStrategy() - && [] !== $taskConfiguration->getErrorOutputs()) { - $m = "Task configuration {$taskConfiguration->getCode()} has error outputs "; - $m .= "but it's error strategy 'stop' implies they will never be reached."; - $this->taskLogger->debug($m); - } $task = $this->taskRegistry->getTask($taskConfiguration); $taskConfiguration->setTask($task); diff --git a/tests/Manager/ProcessManagerStopStrategyTest.php b/tests/Manager/ProcessManagerStopStrategyTest.php index cc1ad21b..b415f290 100644 --- a/tests/Manager/ProcessManagerStopStrategyTest.php +++ b/tests/Manager/ProcessManagerStopStrategyTest.php @@ -30,7 +30,8 @@ use CleverAge\ProcessBundle\Registry\TaskRegistry; use CleverAge\ProcessBundle\Task\ConstantOutputTask; use PHPUnit\Framework\TestCase; -use Psr\Log\NullLogger; +use Psr\Log\AbstractLogger as PsrAbstractLogger; +use Psr\Log\LogLevel; use Symfony\Component\DependencyInjection\Container; use Symfony\Component\EventDispatcher\EventDispatcher; @@ -49,6 +50,16 @@ #[\PHPUnit\Framework\Attributes\UsesClass(ConstantOutputTask::class)] class ProcessManagerStopStrategyTest extends TestCase { + /** + * @var list}> + */ + private array $records = []; + + /** + * @var list + */ + private array $errorBranchInputs = []; + public function testStopStrategyThrowsAProcessFailedExceptionWithTheOriginalException(): void { $originalException = new \LogicException('Something went wrong', 42); @@ -74,8 +85,96 @@ public function testSkipStrategyDoesNotThrow(): void self::assertNull($processManager->execute('test.process')); } - private function createProcessManager(\Throwable $exception, string $errorStrategy = 'stop'): ProcessManager + /** + * The error branch of a task with the stop strategy is executed before the process stops (a debug message said it + * would never be reached). + */ + public function testStopStrategyExecutesTheErrorBranchBeforeStopping(): void { + $processManager = $this->createProcessManager(new \LogicException('Something went wrong'), errorOutputs: ['error_branch']); + + try { + $processManager->execute('test.process'); + self::fail('The process should have failed'); + } catch (ProcessFailedException) { + } + + self::assertSame(['value'], $this->errorBranchInputs); + foreach ($this->records as [, $message]) { + self::assertStringNotContainsString('never be reached', $message); + } + } + + /** + * An error thrown by a listener of the process failure is logged, the error of the process is still thrown (it was + * replaced by the error of the listener). + */ + public function testAFailureListenerErrorDoesNotReplaceTheErrorOfTheProcess(): void + { + $originalException = new \LogicException('Something went wrong'); + $eventDispatcher = new EventDispatcher(); + $failedEvents = []; + $eventDispatcher->addListener(ProcessEvent::EVENT_PROCESS_FAILED, static function () use (&$failedEvents): void { + $failedEvents[] = 'first'; + + throw new \RuntimeException('Listener error'); + }); + $processManager = $this->createProcessManager($originalException, eventDispatcher: $eventDispatcher); + + try { + $processManager->execute('test.process'); + self::fail('The process should have failed'); + } catch (\Throwable $exception) { + self::assertInstanceOf(ProcessFailedException::class, $exception); + self::assertSame($originalException, $exception->getPrevious()); + } + + self::assertSame(['first'], $failedEvents); + $errors = array_values(array_filter( + $this->records, + static fn (array $record): bool => LogLevel::ERROR === $record[0] + )); + self::assertCount(1, $errors); + self::assertSame('A listener of the process failure has failed', $errors[0][1]); + self::assertSame('Listener error', $errors[0][2]['error']); + } + + /** + * @param list $errorOutputs + */ + private function createProcessManager( + \Throwable $exception, + string $errorStrategy = 'stop', + array $errorOutputs = [], + EventDispatcher $eventDispatcher = new EventDispatcher(), + ): ProcessManager { + $errorBranchTask = new class(function (mixed $input): void { + $this->errorBranchInputs[] = $input; + }) implements TaskInterface { + public function __construct( + private readonly \Closure $onExecute, + ) { + } + + public function execute(ProcessState $state): void + { + ($this->onExecute)($state->getInput()); + } + }; + $logger = new class(function (string $level, string $message, array $context): void { + $this->records[] = [$level, $message, $context]; + }) extends PsrAbstractLogger { + public function __construct( + private readonly \Closure $onLog, + ) { + } + + public function log($level, string|\Stringable $message, array $context = []): void + { + ($this->onLog)((string) $level, (string) $message, $context); + } + }; + $failingTask = new class($exception) implements TaskInterface { public function __construct( private readonly \Throwable $exception, @@ -91,6 +190,7 @@ public function execute(ProcessState $state): void $container = new Container(); $container->set('test.constant', new ConstantOutputTask()); $container->set('test.failing', $failingTask); + $container->set('test.error_branch', $errorBranchTask); $registry = new ProcessConfigurationRegistry( [ @@ -103,7 +203,8 @@ public function execute(ProcessState $state): void 'public' => true, 'tasks' => [ 'entry' => $this->createTaskConfiguration('@test.constant', ['output' => 'value'], ['fail']), - 'fail' => $this->createTaskConfiguration('@test.failing', [], [], $errorStrategy), + 'fail' => $this->createTaskConfiguration('@test.failing', [], [], $errorStrategy, $errorOutputs), + 'error_branch' => $this->createTaskConfiguration('@test.error_branch'), ], ], ], @@ -112,11 +213,11 @@ public function execute(ProcessState $state): void return new ProcessManager( $container, - new ProcessLogger(new NullLogger()), - new TaskLogger(new NullLogger()), + new ProcessLogger($logger), + new TaskLogger($logger), $registry, new ContextualOptionResolver(), - new EventDispatcher(), + $eventDispatcher, taskRegistry: new TaskRegistry($container), ); } @@ -124,6 +225,7 @@ public function execute(ProcessState $state): void /** * @param array $options * @param list $outputs + * @param list $errorOutputs * * @return array */ @@ -132,6 +234,7 @@ private function createTaskConfiguration( array $options = [], array $outputs = [], ?string $errorStrategy = null, + array $errorOutputs = [], ): array { return [ 'service' => $service, @@ -140,7 +243,7 @@ private function createTaskConfiguration( 'help' => '', 'outputs' => $outputs, 'errors' => [], - 'error_outputs' => [], + 'error_outputs' => $errorOutputs, 'error_strategy' => $errorStrategy, 'log_level' => null, ];