Skip to content
Closed
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
Latest
------

## Fixes
* [#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: 11 additions & 3 deletions UPGRADE.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
Upgrade Guide
=============

## v5.5.1

### 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, and will be rejected in v6.0. The deprecation names the service, the tasks and the process:
declare the service with `shared: false`, as below.

## v5.5

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.5.1, 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.5.1',
'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
25 changes: 21 additions & 4 deletions tests/DependencyInjection/Compiler/TaskLocatorCompilerPassTest.php
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.5.1: 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