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 @@ -15,6 +15,7 @@ Latest
* [#292](https://github.com/cleverage/process-bundle/issues/292) Apply the behaviours announced by the deprecations of v5: an exception thrown by the `initialize()` of a task makes the process fail before any task is executed, with a `ProcessFailedException` (#222); the `event_dispatcher` task no longer dispatches its event under the class name of the event too when `event_name` is set (#189); a target property of an array destination that is neither a simple property name nor an index notation (`address.city`) throws an `UnexpectedValueException` in the `mapping` transformer (#117); the `additional_parameters` option of the `callback` transformer is removed, use `right_parameters`; a negative `flush_every` of the `counter` task is rejected (#491) (see `UPGRADE.md`). Update documentation and tests.
* [#279](https://github.com/cleverage/process-bundle/issues/279) The 109 classes marked `@final since 5.4` are `final` (engine, registries, models, configurations, events, `CleverAgeProcessBundle`, concrete tasks and transformers): they can no longer be extended nor doubled by PHPUnit. The services of the bundle are typed on `ProcessManagerInterface`, `ProcessConfigurationRegistryInterface` and `TransformerRegistryInterface`, so that a decorator is injected everywhere; `ProcessManagerInterface` declares `getProcessHistory()` and `getTaskConfiguration()`, `ProcessConfigurationRegistryInterface` declares `getProcessCodes()`, and both extend `ResetInterface` (a decorator receives the `kernel.reset` tag and must delegate `reset()`). `PregFilterTransformer::transform()` returns `?string`, the `$transformerRegistry` property of `TransformerTrait` is typed on `TransformerRegistryInterface`, `SplFile::readLine()` no longer has its unused `$length` parameter (see `UPGRADE.md`). Update documentation and tests.
* [#505](https://github.com/cleverage/process-bundle/issues/505) Consistent names for the equivalent options of the tasks: `filename` is renamed `file_path` (`file_reader`, `file_writer`, `line_reader`) and `base_folder_path` is renamed `base_path` (`input_folder_browser`), without deprecation in 5.x; `input_folder_browser` adds a single `/` between a non-empty `base_path` and the input, as `input_csv_reader` does (see `UPGRADE.md`). Update documentation and tests.
* [#380](https://github.com/cleverage/process-bundle/issues/380) Executing a process while it is already running, by itself or through its sub-processes (`A → B → A` with `process_executor`), throws a `RecursiveProcessExecutionException` naming the chain of processes, before any event is dispatched for the rejected execution (deprecated since 5.6, it reset the states of the running process). The `error_strategy` of the `process_executor` task applies (see `UPGRADE.md`). Update documentation and tests.

## Changes
* [#428](https://github.com/cleverage/process-bundle/issues/428) Tests on PHPUnit 12.5 and 13 (PHPUnit 10 and 11 dropped), with the lowest dependencies on PHP 8.3 and 8.5.
Expand Down
9 changes: 8 additions & 1 deletion UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,14 @@ The former names of the codes with a dash, with underscores instead of dashes, a
- the `additional_parameters` option of the `callback` transformer is removed (deprecated since 2.0.4): rename it
`right_parameters`, an `UndefinedOptionsException` is thrown otherwise;
- a negative `flush_every` option of the `counter` task is rejected with an `InvalidOptionsException` (it worked as
its absolute value, deprecated since 5.10): use a positive integer.
its absolute value, deprecated since 5.10): use a positive integer;
- executing a process while it is already running, by itself or through its sub-processes (`A → B → A` with the
`process_executor` task), throws a `RecursiveProcessExecutionException` (`Executing the process "A" while it is
already running (A -> B -> A) is not allowed...`), before any event is dispatched for the rejected execution (it
reset the states of the running process, deprecated since 5.6). In a `process_executor` task, its `error_strategy`
applies: with `stop` (default), the parent processes fail. Iterate with an iterable task instead of recursing, or use
the `process_launcher` task, which runs the process in a separate PHP process (not detected: bound the recursion
yourself).

### Final classes

Expand Down
4 changes: 2 additions & 2 deletions docs/04-advanced_workflow.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,8 @@ values when executing the parent process.
This is a good way to split a big workflow into small, testable processes. Private processes (`public: false`) are
hidden from `cleverage:process:list` (unless `--all` is used) but can still be executed.

Executing a process while it is already running, by itself or through its sub-processes, is deprecated and will throw
an exception in 6.0 (see the [ProcessExecutorTask](reference/tasks/process_executor_task.md#notes)).
Executing a process while it is already running, by itself or through its sub-processes, throws a
`RecursiveProcessExecutionException` (see the [ProcessExecutorTask](reference/tasks/process_executor_task.md#notes)).

## Events

Expand Down
5 changes: 4 additions & 1 deletion docs/reference/01-process_definition.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@ when the container is compiled.
**exclusive**: optional boolean (default is false). An execution of an exclusive process fails with a
`ProcessAlreadyRunningException` while another execution of it is running, from the same PHP process or from another one
sharing the lock store (cron entries, user interface, schedulers, Messenger workers): it is not executed, and the
failure is dispatched as any process failure (`EVENT_PROCESS_FAILED`). It requires the
failure is dispatched as any process failure (`EVENT_PROCESS_FAILED`). Executing it by itself or through its
sub-processes is rejected before its lock is requested, as for any process, with a
`RecursiveProcessExecutionException` and no event (see the
[ProcessExecutorTask](tasks/process_executor_task.md#notes)). It requires the
[Lock component](https://symfony.com/doc/current/lock.html) (`composer require symfony/lock`, `lock.factory` service of
the FrameworkBundle): an exclusive process without it is rejected when the container is compiled.

Expand Down
16 changes: 9 additions & 7 deletions docs/reference/tasks/process_executor_task.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,12 @@ Notes
* The `process` option is checked when the task is initialized: with an unknown process, the initialization fails, the
error is logged and the parent process fails before any of its tasks is executed (see
[initializable tasks](../../02-task_types.md#initializable-tasks)).
* Executing a process while it is already running, by itself or through its sub-processes (`A → B → A`), is
deprecated since 5.6 and will throw an exception in 6.0: a process has only one set of task instances and states,
which the second execution resets while the first one is running (the running process then returns the output of the
deepest execution). A deprecation naming the chain of processes (`A -> B -> A`) is triggered, and the execution goes
on. Iterate with an iterable task instead of recursing, or use the [ProcessLauncherTask](process_launcher_task.md),
which runs the process in a separate PHP process. Executing the same sub-process several times in a row (e.g. for
each input) is not concerned.
* Executing a process while it is already running, by itself or through its sub-processes (`A → B → A`), throws a
`RecursiveProcessExecutionException` naming the chain of processes (`A -> B -> A`): a process has only one set of
task instances and states, which a second execution would reset while the first one is running. The exception is
thrown before any event is dispatched for the rejected execution (the running process is not ended for the listeners
of the events, e.g. a user interface); the `error_strategy` of this task applies: with `stop` (default), the parent
processes fail, with `skip`, the input is skipped. Iterate with an iterable task instead of recursing, or use the
[ProcessLauncherTask](process_launcher_task.md), which runs the process in a separate PHP process (a recursion is not
detected there: bound it yourself). Executing the same sub-process several times in a row (e.g. for each input) is
not concerned.
33 changes: 33 additions & 0 deletions src/Exception/RecursiveProcessExecutionException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

/*
* This file is part of the CleverAge/ProcessBundle package.
*
* Copyright (c) Clever-Age
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace CleverAge\ProcessBundle\Exception;

/**
* Thrown when a process is executed while it is already running, by itself or through its sub-processes: a process has
* only one set of task instances and states, which the second execution would reset.
*/
final class RecursiveProcessExecutionException extends LogicException
{
/**
* @param list<string> $runningProcessCodes codes of the processes being executed, a sub-process after its parent
*/
public static function create(string $processCode, array $runningProcessCodes): self
{
$chain = implode(' -> ', [...$runningProcessCodes, $processCode]);

return new self(
"Executing the process \"{$processCode}\" while it is already running ({$chain}) is not allowed: a process has only one set of task instances and states."
);
}
}
20 changes: 9 additions & 11 deletions src/Manager/ProcessManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use CleverAge\ProcessBundle\Exception\ProcessAlreadyRunningException;
use CleverAge\ProcessBundle\Exception\ProcessFailedException;
use CleverAge\ProcessBundle\Exception\ProcessStoppedException;
use CleverAge\ProcessBundle\Exception\RecursiveProcessExecutionException;
use CleverAge\ProcessBundle\Exception\RuntimeException;
use CleverAge\ProcessBundle\Exception\UnexpectedValueException;
use CleverAge\ProcessBundle\Logger\ProcessLogger;
Expand Down Expand Up @@ -74,7 +75,7 @@ final class ProcessManager implements ProcessManagerInterface

/**
* Codes of the processes being executed, a sub-process after its parent: a process has only one configuration (task
* instances and states), executing it again while it is running resets them.
* instances and states), executing it again while it is running is rejected.
*
* @var list<string>
*/
Expand Down Expand Up @@ -162,6 +163,13 @@ public function getTaskConfiguration(): ?TaskConfiguration
*/
public function execute(string $processCode, mixed $input = null, array $context = []): mixed
{
// Before any event: a failure event would end the execution of the running process for its listeners (e.g.
// the user interface). The codes of the running processes are the codes of their configurations, as the one
// given (an unknown process is never running: it fails as usual, with its events)
if (\in_array($processCode, $this->runningProcessCodes, true)) {
throw RecursiveProcessExecutionException::create($processCode, $this->runningProcessCodes);
}

$parentProcessHistory = $this->processHistory;
$parentTaskConfiguration = $this->taskConfiguration;
try {
Expand All @@ -172,16 +180,6 @@ public function execute(string $processCode, mixed $input = null, array $context
$this->processLogger->debug('Process start');

$code = $this->processConfigurationRegistry->getProcessConfiguration($processCode)->getCode();
// @deprecated since 5.6, in v6.0 an exception will be thrown
if (\in_array($code, $this->runningProcessCodes, true)) {
trigger_deprecation(
'cleverage/process-bundle',
'5.6',
'Executing the process "%s" while it is already running (%s) is deprecated: it resets the states and task instances of the running process. In v6.0, an exception will be thrown.',
$code,
implode(' -> ', [...$this->runningProcessCodes, $code]),
);
}
// After the start event: the failure of an exclusive process already running is dispatched as any failure
$lock = $this->acquireLock($code);
$this->runningProcessCodes[] = $code;
Expand Down
7 changes: 5 additions & 2 deletions src/Manager/ProcessManagerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use CleverAge\ProcessBundle\Configuration\TaskConfiguration;
use CleverAge\ProcessBundle\Exception\MissingProcessException;
use CleverAge\ProcessBundle\Exception\ProcessFailedException;
use CleverAge\ProcessBundle\Exception\RecursiveProcessExecutionException;
use CleverAge\ProcessBundle\Model\ProcessHistory;
use Symfony\Contracts\Service\ResetInterface;

Expand All @@ -30,8 +31,10 @@ interface ProcessManagerInterface extends ResetInterface
*
* @param array<string, mixed> $context
*
* @throws MissingProcessException when the process does not exist
* @throws ProcessFailedException when a task fails with the "stop" error strategy
* @throws MissingProcessException when the process does not exist
* @throws ProcessFailedException when a task fails with the "stop" error strategy
* @throws RecursiveProcessExecutionException when the process is already running (by itself or through its
* sub-processes), before any event is dispatched
*/
public function execute(string $processCode, mixed $input = null, array $context = []): mixed;

Expand Down
29 changes: 29 additions & 0 deletions tests/Manager/ProcessManagerExclusiveTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use CleverAge\ProcessBundle\Exception\LogicException;
use CleverAge\ProcessBundle\Exception\ProcessAlreadyRunningException;
use CleverAge\ProcessBundle\Exception\ProcessFailedException;
use CleverAge\ProcessBundle\Exception\RecursiveProcessExecutionException;
use CleverAge\ProcessBundle\Logger\AbstractLogger;
use CleverAge\ProcessBundle\Logger\ProcessLogger;
use CleverAge\ProcessBundle\Logger\TaskLogger;
Expand All @@ -45,6 +46,8 @@
#[CoversClass(ProcessManager::class)]
#[CoversClass(ProcessAlreadyRunningException::class)]
#[UsesClass(ProcessFailedException::class)]
#[UsesClass(RecursiveProcessExecutionException::class)]
#[UsesClass(LogicException::class)]
#[UsesClass(TaskRegistry::class)]
#[UsesClass(ProcessConfiguration::class)]
#[UsesClass(TaskConfiguration::class)]
Expand Down Expand Up @@ -105,6 +108,32 @@ public function testExclusiveProcessFailsWhileItIsRunning(): void
self::assertSame([1, 2, 3], $this->recorded, 'The running execution goes on');
}

/**
* An exclusive process executed by itself is rejected as any process already running, before its lock is
* requested: no failure is dispatched for the rejected execution, the running one goes on.
*/
public function testExclusiveProcessExecutedByItselfIsRejectedBeforeTheLock(): void
{
$processManager = $this->createProcessManager(new LockFactory(new InMemoryStore()));
$error = null;
$this->onRecord = static function (mixed $item) use ($processManager, &$error): void {
if (2 !== $item) {
return;
}
try {
$processManager->execute('test.exclusive');
} catch (\Throwable $exception) {
$error = $exception;
}
};

$processManager->execute('test.exclusive');

self::assertInstanceOf(RecursiveProcessExecutionException::class, $error);
self::assertSame([], $this->failedEventErrors);
self::assertSame([1, 2, 3], $this->recorded, 'The running execution goes on');
}

/**
* The lock is released at the end of the process, succeeded or failed.
*/
Expand Down
Loading