Skip to content

Commit

Permalink
Merge pull request #914 from uro/feature/async-command-extensions
Browse files Browse the repository at this point in the history
async_commands: extended configuration proposal
  • Loading branch information
makasim authored Aug 20, 2019
2 parents 690ba08 + 600ac06 commit ee19071
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 14 deletions.
6 changes: 5 additions & 1 deletion docs/bundle/async_commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ $ composer require enqueue/async-command:0.9.x-dev

enqueue:
default:
async_commands: true
async_commands:
enabled: true
timeout: 60
command_name: ~
queue_name: ~
```
## Usage
Expand Down
3 changes: 3 additions & 0 deletions docs/bundle/config_reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ enqueue:
storage_factory_class: ~
async_commands:
enabled: false
timeout: 60
command_name: ~
queue_name: ~
job:
enabled: false
async_events:
Expand Down
20 changes: 15 additions & 5 deletions pkg/async-command/DependencyInjection/AsyncCommandExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,23 @@ 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,
'command_name' => Commands::RUN_COMMAND,
'queue_name' => Commands::RUN_COMMAND,
'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['command_name'] ?? Commands::RUN_COMMAND,
'queue' => $client['queue_name'] ?? Commands::RUN_COMMAND,
'prefix_queue' => false,
'exclusive' => true,
])
Expand Down
10 changes: 8 additions & 2 deletions pkg/async-command/RunCommandProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()) {
Expand Down
7 changes: 7 additions & 0 deletions pkg/async-command/Tests/RunCommandProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
6 changes: 6 additions & 0 deletions pkg/enqueue-bundle/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ private function getAsyncCommandsConfiguration(): ArrayNodeDefinition
}

return (new ArrayNodeDefinition('async_commands'))
->children()
->booleanNode('enabled')->defaultFalse()->end()
->integerNode('timeout')->min(0)->defaultValue(60)->end()
->scalarNode('command_name')->defaultNull()->end()
->scalarNode('queue_name')->defaultNull()->end()
->end()
->addDefaultsIfNotSet()
->canBeEnabled()
;
Expand Down
13 changes: 9 additions & 4 deletions pkg/enqueue-bundle/DependencyInjection/EnqueueExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -363,14 +363,19 @@ 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'],
'command_name' => $modules['async_commands']['command_name'],
'queue_name' => $modules['async_commands']['queue_name'],
];
}
}

if (false == $configNames) {
if (false == $configs) {
return;
}

Expand All @@ -379,7 +384,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)
Expand Down
8 changes: 6 additions & 2 deletions pkg/enqueue-bundle/Tests/Functional/App/config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ enqueue:
traceable_producer: true
job: true
async_events: true
async_commands: true
async_commands:
enabled: true
timeout: 60
command_name: ~
queue_name: ~

services:
test_enqueue.client.default.traceable_producer:
Expand Down Expand Up @@ -122,4 +126,4 @@ services:
enqueue.events.async_listener:
class: 'Enqueue\Bundle\Tests\Functional\App\AsyncListener'
public: true
arguments: ['@enqueue.client.default.producer', '@enqueue.events.registry']
arguments: ['@enqueue.client.default.producer', '@enqueue.events.registry']

0 comments on commit ee19071

Please sign in to comment.