Skip to content

Commit ee19071

Browse files
authored
Merge pull request #914 from uro/feature/async-command-extensions
async_commands: extended configuration proposal
2 parents 690ba08 + 600ac06 commit ee19071

File tree

8 files changed

+59
-14
lines changed

8 files changed

+59
-14
lines changed

Diff for: docs/bundle/async_commands.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ $ composer require enqueue/async-command:0.9.x-dev
2121

2222
enqueue:
2323
default:
24-
async_commands: true
24+
async_commands:
25+
enabled: true
26+
timeout: 60
27+
command_name: ~
28+
queue_name: ~
2529
```
2630
2731
## Usage

Diff for: docs/bundle/config_reference.md

+3
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ enqueue:
6262
storage_factory_class: ~
6363
async_commands:
6464
enabled: false
65+
timeout: 60
66+
command_name: ~
67+
queue_name: ~
6568
job:
6669
enabled: false
6770
async_events:

Diff for: pkg/async-command/DependencyInjection/AsyncCommandExtension.php

+15-5
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,23 @@ class AsyncCommandExtension extends Extension
1212
public function load(array $configs, ContainerBuilder $container)
1313
{
1414
foreach ($configs['clients'] as $client) {
15-
$id = sprintf('enqueue.async_command.%s.run_command_processor', $client);
15+
// BC compatibility
16+
if (!is_array($client)) {
17+
$client = [
18+
'name' => $client,
19+
'command_name' => Commands::RUN_COMMAND,
20+
'queue_name' => Commands::RUN_COMMAND,
21+
'timeout' => 60,
22+
];
23+
}
24+
25+
$id = sprintf('enqueue.async_command.%s.run_command_processor', $client['name']);
1626
$container->register($id, RunCommandProcessor::class)
17-
->addArgument('%kernel.project_dir%')
27+
->addArgument('%kernel.project_dir%', $client['timeout'])
1828
->addTag('enqueue.processor', [
19-
'client' => $client,
20-
'command' => Commands::RUN_COMMAND,
21-
'queue' => Commands::RUN_COMMAND,
29+
'client' => $client['name'],
30+
'command' => $client['command_name'] ?? Commands::RUN_COMMAND,
31+
'queue' => $client['queue_name'] ?? Commands::RUN_COMMAND,
2232
'prefix_queue' => false,
2333
'exclusive' => true,
2434
])

Diff for: pkg/async-command/RunCommandProcessor.php

+8-2
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,20 @@
1111

1212
final class RunCommandProcessor implements Processor
1313
{
14+
/**
15+
* @var int
16+
*/
17+
private $timeout;
18+
1419
/**
1520
* @var string
1621
*/
1722
private $projectDir;
1823

19-
public function __construct(string $projectDir)
24+
public function __construct(string $projectDir, int $timeout = 60)
2025
{
2126
$this->projectDir = $projectDir;
27+
$this->timeout = $timeout;
2228
}
2329

2430
public function process(Message $message, Context $context): Result
@@ -29,7 +35,7 @@ public function process(Message $message, Context $context): Result
2935
$consoleBin = file_exists($this->projectDir.'/bin/console') ? './bin/console' : './app/console';
3036

3137
$process = new Process($phpBin.' '.$consoleBin.' '.$this->getCommandLine($command), $this->projectDir);
32-
38+
$process->setTimeout($this->timeout);
3339
$process->run();
3440

3541
if ($message->getReplyTo()) {

Diff for: pkg/async-command/Tests/RunCommandProcessorTest.php

+7
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,11 @@ public function testCouldBeConstructedWithProjectDirAsFirstArgument()
2828

2929
$this->assertAttributeSame('aProjectDir', 'projectDir', $processor);
3030
}
31+
32+
public function testCouldBeConstructedWithTimeoutAsSecondArgument()
33+
{
34+
$processor = new RunCommandProcessor('aProjectDir', 60);
35+
36+
$this->assertAttributeSame(60, 'timeout', $processor);
37+
}
3138
}

Diff for: pkg/enqueue-bundle/DependencyInjection/Configuration.php

+6
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,12 @@ private function getAsyncCommandsConfiguration(): ArrayNodeDefinition
7676
}
7777

7878
return (new ArrayNodeDefinition('async_commands'))
79+
->children()
80+
->booleanNode('enabled')->defaultFalse()->end()
81+
->integerNode('timeout')->min(0)->defaultValue(60)->end()
82+
->scalarNode('command_name')->defaultNull()->end()
83+
->scalarNode('queue_name')->defaultNull()->end()
84+
->end()
7985
->addDefaultsIfNotSet()
8086
->canBeEnabled()
8187
;

Diff for: pkg/enqueue-bundle/DependencyInjection/EnqueueExtension.php

+9-4
Original file line numberDiff line numberDiff line change
@@ -363,14 +363,19 @@ private function loadReplyExtension(array $config, ContainerBuilder $container):
363363

364364
private function loadAsyncCommands(array $config, ContainerBuilder $container): void
365365
{
366-
$configNames = [];
366+
$configs = [];
367367
foreach ($config as $name => $modules) {
368368
if (false === empty($modules['async_commands']['enabled'])) {
369-
$configNames[] = $name;
369+
$configs[] = [
370+
'name' => $name,
371+
'timeout' => $modules['async_commands']['timeout'],
372+
'command_name' => $modules['async_commands']['command_name'],
373+
'queue_name' => $modules['async_commands']['queue_name'],
374+
];
370375
}
371376
}
372377

373-
if (false == $configNames) {
378+
if (false == $configs) {
374379
return;
375380
}
376381

@@ -379,7 +384,7 @@ private function loadAsyncCommands(array $config, ContainerBuilder $container):
379384
}
380385

381386
$extension = new AsyncCommandExtension();
382-
$extension->load(['clients' => $configNames], $container);
387+
$extension->load(['clients' => $configs], $container);
383388
}
384389

385390
private function loadMessageQueueCollector(array $config, ContainerBuilder $container)

Diff for: pkg/enqueue-bundle/Tests/Functional/App/config/config.yml

+6-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@ enqueue:
2828
traceable_producer: true
2929
job: true
3030
async_events: true
31-
async_commands: true
31+
async_commands:
32+
enabled: true
33+
timeout: 60
34+
command_name: ~
35+
queue_name: ~
3236

3337
services:
3438
test_enqueue.client.default.traceable_producer:
@@ -122,4 +126,4 @@ services:
122126
enqueue.events.async_listener:
123127
class: 'Enqueue\Bundle\Tests\Functional\App\AsyncListener'
124128
public: true
125-
arguments: ['@enqueue.client.default.producer', '@enqueue.events.registry']
129+
arguments: ['@enqueue.client.default.producer', '@enqueue.events.registry']

0 commit comments

Comments
 (0)