Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
4 changes: 4 additions & 0 deletions docs/04-advanced_workflow.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 13 additions & 10 deletions src/Manager/ProcessManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);

Expand Down
117 changes: 110 additions & 7 deletions tests/Manager/ProcessManagerStopStrategyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -49,6 +50,16 @@
#[\PHPUnit\Framework\Attributes\UsesClass(ConstantOutputTask::class)]
class ProcessManagerStopStrategyTest extends TestCase
{
/**
* @var list<array{string, string, array<string, mixed>}>
*/
private array $records = [];

/**
* @var list<mixed>
*/
private array $errorBranchInputs = [];

public function testStopStrategyThrowsAProcessFailedExceptionWithTheOriginalException(): void
{
$originalException = new \LogicException('Something went wrong', 42);
Expand All @@ -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<string> $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,
Expand All @@ -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(
[
Expand All @@ -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'),
],
],
],
Expand All @@ -112,18 +213,19 @@ 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),
);
}

/**
* @param array<string, mixed> $options
* @param list<string> $outputs
* @param list<string> $errorOutputs
*
* @return array<string, mixed>
*/
Expand All @@ -132,6 +234,7 @@ private function createTaskConfiguration(
array $options = [],
array $outputs = [],
?string $errorStrategy = null,
array $errorOutputs = [],
): array {
return [
'service' => $service,
Expand All @@ -140,7 +243,7 @@ private function createTaskConfiguration(
'help' => '',
'outputs' => $outputs,
'errors' => [],
'error_outputs' => [],
'error_outputs' => $errorOutputs,
'error_strategy' => $errorStrategy,
'log_level' => null,
];
Expand Down
Loading