Skip to content

Commit a160ca4

Browse files
committed
Async commands extended
1 parent 9baddd1 commit a160ca4

File tree

3 files changed

+29
-7
lines changed

3 files changed

+29
-7
lines changed

DependencyInjection/AsyncCommandExtension.php

Lines changed: 14 additions & 5 deletions
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
])

RunCommandProcessor.php

Lines changed: 8 additions & 2 deletions
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()) {

Tests/RunCommandProcessorTest.php

Lines changed: 7 additions & 0 deletions
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
}

0 commit comments

Comments
 (0)