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 @@ -11,6 +11,7 @@ Latest
* [#370](https://github.com/cleverage/process-bundle/issues/370) `RulesTransformer` (`rules`): the result of a `condition` is cast to `bool`, so a condition returning an `int` (`preg_match()`), a string, `null` or any other non-boolean value (a single variable) no longer throws a `TypeError`: the rule matches if the result is truthy. Update documentation, add tests.
* [#372](https://github.com/cleverage/process-bundle/issues/372) `ProcessConfigurationRegistry`: an unknown task in the `outputs` or `error_outputs` of a task, or in the `entry_point` or `end_point` of a process, throws an `InvalidProcessConfigurationException` naming the process, the task and the key holding the reference, instead of a `MissingTaskConfigurationException` (`No task configuration with code : <code>`). A process whose configuration is not valid is left out of `getProcessConfigurations()` (`cleverage:process:list`, lists of the UI) with an error log on the `cleverage_process` channel, instead of making every listing fail; getting or executing this process still throws. Update documentation, add tests.
* [#373](https://github.com/cleverage/process-bundle/issues/373) `ProcessManager`: the tasks are finalized (`FinalizableTaskInterface::finalize()`) when the process fails too, so that a long-running worker (Messenger, UI bundle) no longer keeps the resources of a failed process open (files of the CSV, JSON stream and XML tasks, cursor of `doctrine_database_reader` blocking the next queries with MySQL unbuffered queries). Only the initialized tasks are finalized, each one once; a finalization failure is logged as critical and the process fails with its original exception. `StatCounterTask` (`stat_counter`) logs its count when the process fails too. Update documentation, add tests.
* [#382](https://github.com/cleverage/process-bundle/issues/382) A shared service referenced by several tasks of the same process is deprecated instead of being rejected when the container is compiled (5.5, #339): this configuration worked before 5.5, all these tasks running with the same instance. The deprecation names the service, the tasks and the process; it will be rejected in 6.0. Declare the task services with `shared: false`. Update documentation, update tests.

v5.5
-----
Expand Down
14 changes: 10 additions & 4 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Upgrade Guide

## v5.6

One deprecation, and some behaviours change: check the points below.
Two deprecations, and some behaviours change: check the points below.

### Recursive process execution

Expand All @@ -17,6 +17,12 @@ Iterate with an iterable task (e.g. a reader or `constant_iterable_output` follo
recursing, or use the `process_launcher` task, which runs the process in a separate PHP process. Executing the same
sub-process several times in a row is not concerned.

### Shared task services

A shared service referenced by several tasks of the same process is no longer rejected when the container is compiled
(5.5): it is deprecated since 5.6, and will be rejected in v6.0. The deprecation names the service, the tasks and the
process: declare the service with `shared: false` (see v5.5 below).

### Finalization of the tasks when the process fails

`FinalizableTaskInterface::finalize()` is now also called when the process fails, on every task that has been
Expand All @@ -33,9 +39,9 @@ No deprecation in this version. Some behaviours change: check the points below.
### Shared task services

A shared service referenced by several tasks of the same process is rejected when the container is compiled
(`InvalidArgumentException` naming the service, the tasks and the process): all these tasks ran with the same instance,
initialized with the options of each task in turn. Declare your task services with `shared: false`, as the
autoconfiguration of `TaskInterface` does since 5.4:
(`InvalidArgumentException` naming the service, the tasks and the process; deprecated instead since 5.6, see above):
all these tasks ran with the same instance, initialized with the options of each task in turn. Declare your task
services with `shared: false`, as the autoconfiguration of `TaskInterface` does since 5.4:

```yaml
# config/services.yaml
Expand Down
2 changes: 1 addition & 1 deletion docs/03-custom_tasks.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Most of the time you don't want a task declared in a process to share some data
same service, or even with another instance of your process. Since a task is a service, by default, Symfony will only
create one instance (keeping values in every attribute of your class). With `shared: false`, a new instance is created
each time a task configuration is initialized. A shared service referenced by several tasks of the same process is
rejected when the container is compiled.
deprecated (a deprecation is triggered when the container is compiled) and will be rejected in v6.0.

With autoconfiguration (`autoconfigure: true`, set in the `_defaults` of the Symfony skeleton), the bundle applies
these settings to every service implementing `TaskInterface`: `shared: false` and a `monolog.logger` tag on the
Expand Down
2 changes: 1 addition & 1 deletion docs/05-good_practices.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ in several places, declare it once as a [generic transformer](reference/03-gener

Task services should be `shared: false`: a task often keeps state in its properties (an
open file, a buffer, a counter...). A shared service would share this state between two tasks using the same service
in a process (this is rejected when the container is compiled), or between two executions of a process in the same PHP
in a process (deprecated, rejected in v6.0), or between two executions of a process in the same PHP
process (sub-processes, workers, tests). See [service declaration](03-custom_tasks.md#service-declaration).

## Split big workflows into small processes
Expand Down
13 changes: 10 additions & 3 deletions src/DependencyInjection/Compiler/TaskLocatorCompilerPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,23 @@ public function process(ContainerBuilder $container): void
}

/**
* A shared service referenced by several tasks of a process would run all of them with the same instance: the
* options of the last initialization would apply to every task, and their states would be mixed.
* A shared service referenced by several tasks of a process runs all of them with the same instance: the options of
* the last initialization apply to every task, and their states are mixed. Deprecated, rejected in v6.0.
*
* @param array<string, list<string>> $taskCodesByService
*/
private function checkSharedServices(ContainerBuilder $container, string $processCode, array $taskCodesByService): void
{
foreach ($taskCodesByService as $id => $taskCodes) {
if (\count($taskCodes) > 1 && $container->findDefinition($id)->isShared()) {
throw new InvalidArgumentException(\sprintf('The service "%s" is used by the tasks "%s" of the process "%s", but it is shared: all these tasks would run with the same instance (and the options of one of them). Declare the service with "shared: false".', $id, implode('", "', $taskCodes), $processCode));
trigger_deprecation(
'cleverage/process-bundle',
'5.6',
'The service "%s" is used by the tasks "%s" of the process "%s", but it is shared: all these tasks run with the same instance (and the options of one of them). Declare the service with "shared: false". Shared services used by several tasks of a process are deprecated, they will be rejected in v6.0.',
$id,
implode('", "', $taskCodes),
$processCode
);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,32 @@ public function testInvalidTaskCodeAttributeThrows(): void
(new TaskLocatorCompilerPass())->process($container);
}

public function testSharedServiceUsedBySeveralTasksOfAProcessThrows(): void
public function testSharedServiceUsedBySeveralTasksOfAProcessIsDeprecated(): void
{
$container = $this->createContainerWithTasks(['test.process' => ['first' => 'app_task', 'second' => 'app_task']]);
$container->setDefinition('app.task', (new Definition(DummyTask::class))->addTag('cleverage.task', ['code' => 'app_task']));

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('The service "app.task" is used by the tasks "first", "second" of the process "test.process", but it is shared: all these tasks would run with the same instance (and the options of one of them). Declare the service with "shared: false".');
$deprecations = [];
set_error_handler(static function (int $errno, string $errstr) use (&$deprecations): bool {
$deprecations[] = $errstr;

(new TaskLocatorCompilerPass())->process($container);
return true;
}, \E_USER_DEPRECATED);
try {
(new TaskLocatorCompilerPass())->process($container);
} finally {
restore_error_handler();
}

self::assertSame([
'Since cleverage/process-bundle 5.6: The service "app.task" is used by the tasks "first", "second" of the process "test.process", but it is shared: all these tasks run with the same instance (and the options of one of them). Declare the service with "shared: false". Shared services used by several tasks of a process are deprecated, they will be rejected in v6.0.',
], $deprecations);
// The service is still available to the tasks, as before 5.5
$locatorReference = $container->getDefinition('cleverage_process.registry.task')->getArgument(0);
self::assertInstanceOf(Reference::class, $locatorReference);
$services = $container->getDefinition((string) $locatorReference)->getArgument(0);
self::assertIsArray($services);
self::assertSame(['app.task'], array_keys($services));
}

public function testNonSharedServiceCanBeUsedBySeveralTasksOfAProcess(): void
Expand Down
Loading