diff --git a/CHANGELOG.md b/CHANGELOG.md index 9e55a44b..b9ef05e7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 ----- diff --git a/UPGRADE.md b/UPGRADE.md index 1cd439a9..d904422a 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -1,6 +1,14 @@ 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. @@ -8,9 +16,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.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 diff --git a/docs/03-custom_tasks.md b/docs/03-custom_tasks.md index 72bca29a..7742d621 100644 --- a/docs/03-custom_tasks.md +++ b/docs/03-custom_tasks.md @@ -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 diff --git a/docs/05-good_practices.md b/docs/05-good_practices.md index ebe20410..b82f710b 100644 --- a/docs/05-good_practices.md +++ b/docs/05-good_practices.md @@ -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 diff --git a/src/DependencyInjection/Compiler/TaskLocatorCompilerPass.php b/src/DependencyInjection/Compiler/TaskLocatorCompilerPass.php index 3d6ba77a..1c26f5ee 100644 --- a/src/DependencyInjection/Compiler/TaskLocatorCompilerPass.php +++ b/src/DependencyInjection/Compiler/TaskLocatorCompilerPass.php @@ -60,8 +60,8 @@ 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> $taskCodesByService */ @@ -69,7 +69,14 @@ private function checkSharedServices(ContainerBuilder $container, string $proces { 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 + ); } } } diff --git a/tests/DependencyInjection/Compiler/TaskLocatorCompilerPassTest.php b/tests/DependencyInjection/Compiler/TaskLocatorCompilerPassTest.php index a5a10342..3eec2b83 100644 --- a/tests/DependencyInjection/Compiler/TaskLocatorCompilerPassTest.php +++ b/tests/DependencyInjection/Compiler/TaskLocatorCompilerPassTest.php @@ -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