From a9435929c1387ecc45832a8eb81eb8fe2326b94f Mon Sep 17 00:00:00 2001 From: Arkadiusz Surma Date: Thu, 11 Jul 2019 12:10:00 +0100 Subject: [PATCH] Async commands extended --- docs/bundle/async_commands.md | 5 ++++- docs/bundle/config_reference.md | 2 ++ .../AsyncCommandExtension.php | 19 ++++++++++++++----- pkg/async-command/RunCommandProcessor.php | 10 ++++++++-- .../Tests/RunCommandProcessorTest.php | 7 +++++++ .../DependencyInjection/Configuration.php | 5 +++++ .../DependencyInjection/EnqueueExtension.php | 12 ++++++++---- .../Tests/Functional/App/config/config.yml | 7 +++++-- 8 files changed, 53 insertions(+), 14 deletions(-) diff --git a/docs/bundle/async_commands.md b/docs/bundle/async_commands.md index c82024110..b5e8265be 100644 --- a/docs/bundle/async_commands.md +++ b/docs/bundle/async_commands.md @@ -21,7 +21,10 @@ $ composer require enqueue/async-command:0.9.x-dev enqueue: default: - async_commands: true + async_commands: + enabled: true + timeout: 60 + prefix: ~ ``` ## Usage diff --git a/docs/bundle/config_reference.md b/docs/bundle/config_reference.md index edbd917f2..d277cdc5c 100644 --- a/docs/bundle/config_reference.md +++ b/docs/bundle/config_reference.md @@ -62,6 +62,8 @@ enqueue: storage_factory_class: ~ async_commands: enabled: false + timeout: 60 + prefix: ~ job: enabled: false async_events: diff --git a/pkg/async-command/DependencyInjection/AsyncCommandExtension.php b/pkg/async-command/DependencyInjection/AsyncCommandExtension.php index 2cfdf261c..51c66e4be 100644 --- a/pkg/async-command/DependencyInjection/AsyncCommandExtension.php +++ b/pkg/async-command/DependencyInjection/AsyncCommandExtension.php @@ -12,13 +12,22 @@ class AsyncCommandExtension extends Extension public function load(array $configs, ContainerBuilder $container) { foreach ($configs['clients'] as $client) { - $id = sprintf('enqueue.async_command.%s.run_command_processor', $client); + // BC compatibility + if (!is_array($client)) { + $client = [ + 'name' => $client, + 'prefix' => '', + 'timeout' => 60, + ]; + } + + $id = sprintf('enqueue.async_command.%s.run_command_processor', $client['name']); $container->register($id, RunCommandProcessor::class) - ->addArgument('%kernel.project_dir%') + ->addArgument('%kernel.project_dir%', $client['timeout']) ->addTag('enqueue.processor', [ - 'client' => $client, - 'command' => Commands::RUN_COMMAND, - 'queue' => Commands::RUN_COMMAND, + 'client' => $client['name'], + 'command' => $client['prefix'].Commands::RUN_COMMAND, + 'queue' => $client['prefix'].Commands::RUN_COMMAND, 'prefix_queue' => false, 'exclusive' => true, ]) diff --git a/pkg/async-command/RunCommandProcessor.php b/pkg/async-command/RunCommandProcessor.php index 7925d658c..f1495f399 100644 --- a/pkg/async-command/RunCommandProcessor.php +++ b/pkg/async-command/RunCommandProcessor.php @@ -11,14 +11,20 @@ final class RunCommandProcessor implements Processor { + /** + * @var int + */ + private $timeout; + /** * @var string */ private $projectDir; - public function __construct(string $projectDir) + public function __construct(string $projectDir, int $timeout = 60) { $this->projectDir = $projectDir; + $this->timeout = $timeout; } public function process(Message $message, Context $context): Result @@ -29,7 +35,7 @@ public function process(Message $message, Context $context): Result $consoleBin = file_exists($this->projectDir.'/bin/console') ? './bin/console' : './app/console'; $process = new Process($phpBin.' '.$consoleBin.' '.$this->getCommandLine($command), $this->projectDir); - + $process->setTimeout($this->timeout); $process->run(); if ($message->getReplyTo()) { diff --git a/pkg/async-command/Tests/RunCommandProcessorTest.php b/pkg/async-command/Tests/RunCommandProcessorTest.php index 83274e2c0..3065ec315 100644 --- a/pkg/async-command/Tests/RunCommandProcessorTest.php +++ b/pkg/async-command/Tests/RunCommandProcessorTest.php @@ -28,4 +28,11 @@ public function testCouldBeConstructedWithProjectDirAsFirstArgument() $this->assertAttributeSame('aProjectDir', 'projectDir', $processor); } + + public function testCouldBeConstructedWithTimeoutAsSecondArgument() + { + $processor = new RunCommandProcessor('aProjectDir', 60); + + $this->assertAttributeSame(60, 'timeout', $processor); + } } diff --git a/pkg/enqueue-bundle/DependencyInjection/Configuration.php b/pkg/enqueue-bundle/DependencyInjection/Configuration.php index 6257fa4f1..e2d290348 100644 --- a/pkg/enqueue-bundle/DependencyInjection/Configuration.php +++ b/pkg/enqueue-bundle/DependencyInjection/Configuration.php @@ -73,6 +73,11 @@ private function getAsyncCommandsConfiguration(): ArrayNodeDefinition } return (new ArrayNodeDefinition('async_commands')) + ->children() + ->booleanNode('enabled')->defaultFalse()->end() + ->integerNode('timeout')->min(0)->defaultValue(60)->end() + ->scalarNode('prefix')->defaultValue('')->end() + ->end() ->addDefaultsIfNotSet() ->canBeEnabled() ; diff --git a/pkg/enqueue-bundle/DependencyInjection/EnqueueExtension.php b/pkg/enqueue-bundle/DependencyInjection/EnqueueExtension.php index b5ae10ab9..48acc85dc 100644 --- a/pkg/enqueue-bundle/DependencyInjection/EnqueueExtension.php +++ b/pkg/enqueue-bundle/DependencyInjection/EnqueueExtension.php @@ -291,14 +291,18 @@ private function loadReplyExtension(array $config, ContainerBuilder $container): private function loadAsyncCommands(array $config, ContainerBuilder $container): void { - $configNames = []; + $configs = []; foreach ($config as $name => $modules) { if (false === empty($modules['async_commands']['enabled'])) { - $configNames[] = $name; + $configs[] = [ + 'name' => $name, + 'timeout' => $modules['async_commands']['timeout'], + 'prefix' => $modules['async_commands']['prefix'], + ]; } } - if (false == $configNames) { + if (false == $configs) { return; } @@ -307,7 +311,7 @@ private function loadAsyncCommands(array $config, ContainerBuilder $container): } $extension = new AsyncCommandExtension(); - $extension->load(['clients' => $configNames], $container); + $extension->load(['clients' => $configs], $container); } private function loadMessageQueueCollector(array $config, ContainerBuilder $container) diff --git a/pkg/enqueue-bundle/Tests/Functional/App/config/config.yml b/pkg/enqueue-bundle/Tests/Functional/App/config/config.yml index 1198e60d2..3e43b628e 100644 --- a/pkg/enqueue-bundle/Tests/Functional/App/config/config.yml +++ b/pkg/enqueue-bundle/Tests/Functional/App/config/config.yml @@ -28,7 +28,10 @@ enqueue: traceable_producer: true job: true async_events: true - async_commands: true + async_commands: + enabled: true + timeout: 60 + prefix: '' services: test_enqueue.client.default.traceable_producer: @@ -122,4 +125,4 @@ services: enqueue.events.async_listener: class: 'Enqueue\Bundle\Tests\Functional\App\AsyncListener' public: true - arguments: ['@enqueue.client.default.producer', '@enqueue.events.registry'] \ No newline at end of file + arguments: ['@enqueue.client.default.producer', '@enqueue.events.registry']