Skip to content

Commit a943592

Browse files
committed
Async commands extended
1 parent 594e878 commit a943592

File tree

8 files changed

+53
-14
lines changed

8 files changed

+53
-14
lines changed

docs/bundle/async_commands.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ $ 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+
prefix: ~
2528
```
2629
2730
## Usage

docs/bundle/config_reference.md

+2
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ enqueue:
6262
storage_factory_class: ~
6363
async_commands:
6464
enabled: false
65+
timeout: 60
66+
prefix: ~
6567
job:
6668
enabled: false
6769
async_events:

pkg/async-command/DependencyInjection/AsyncCommandExtension.php

+14-5
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,22 @@ 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+
'prefix' => '',
20+
'timeout' => 60,
21+
];
22+
}
23+
24+
$id = sprintf('enqueue.async_command.%s.run_command_processor', $client['name']);
1625
$container->register($id, RunCommandProcessor::class)
17-
->addArgument('%kernel.project_dir%')
26+
->addArgument('%kernel.project_dir%', $client['timeout'])
1827
->addTag('enqueue.processor', [
19-
'client' => $client,
20-
'command' => Commands::RUN_COMMAND,
21-
'queue' => Commands::RUN_COMMAND,
28+
'client' => $client['name'],
29+
'command' => $client['prefix'].Commands::RUN_COMMAND,
30+
'queue' => $client['prefix'].Commands::RUN_COMMAND,
2231
'prefix_queue' => false,
2332
'exclusive' => true,
2433
])

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()) {

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
}

pkg/enqueue-bundle/DependencyInjection/Configuration.php

+5
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ private function getAsyncCommandsConfiguration(): ArrayNodeDefinition
7373
}
7474

7575
return (new ArrayNodeDefinition('async_commands'))
76+
->children()
77+
->booleanNode('enabled')->defaultFalse()->end()
78+
->integerNode('timeout')->min(0)->defaultValue(60)->end()
79+
->scalarNode('prefix')->defaultValue('')->end()
80+
->end()
7681
->addDefaultsIfNotSet()
7782
->canBeEnabled()
7883
;

pkg/enqueue-bundle/DependencyInjection/EnqueueExtension.php

+8-4
Original file line numberDiff line numberDiff line change
@@ -291,14 +291,18 @@ private function loadReplyExtension(array $config, ContainerBuilder $container):
291291

292292
private function loadAsyncCommands(array $config, ContainerBuilder $container): void
293293
{
294-
$configNames = [];
294+
$configs = [];
295295
foreach ($config as $name => $modules) {
296296
if (false === empty($modules['async_commands']['enabled'])) {
297-
$configNames[] = $name;
297+
$configs[] = [
298+
'name' => $name,
299+
'timeout' => $modules['async_commands']['timeout'],
300+
'prefix' => $modules['async_commands']['prefix'],
301+
];
298302
}
299303
}
300304

301-
if (false == $configNames) {
305+
if (false == $configs) {
302306
return;
303307
}
304308

@@ -307,7 +311,7 @@ private function loadAsyncCommands(array $config, ContainerBuilder $container):
307311
}
308312

309313
$extension = new AsyncCommandExtension();
310-
$extension->load(['clients' => $configNames], $container);
314+
$extension->load(['clients' => $configs], $container);
311315
}
312316

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

pkg/enqueue-bundle/Tests/Functional/App/config/config.yml

+5-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ 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+
prefix: ''
3235

3336
services:
3437
test_enqueue.client.default.traceable_producer:
@@ -122,4 +125,4 @@ services:
122125
enqueue.events.async_listener:
123126
class: 'Enqueue\Bundle\Tests\Functional\App\AsyncListener'
124127
public: true
125-
arguments: ['@enqueue.client.default.producer', '@enqueue.events.registry']
128+
arguments: ['@enqueue.client.default.producer', '@enqueue.events.registry']

0 commit comments

Comments
 (0)