The upcoming Enqueue release will contain a feature that allows to run Symfony commands asynchronously. You can use it right now, just make sure the package is in place and you have EnqueueBundle properly configured plus one of supported MQ transports installed. The package could be used stand alone or as a part of Symfony application. It runs command with ability to get command output.

First, install the enqueue/async-command package:

$ composer req enqueue/async-command:0.9.x-dev

Create the file enqueue_async_commands.yaml and enable the feature there:

# app/config/packages/ enqueue_async_commands.yaml enqueue:

async_commands: true

A simple use case example, that shows how to send a command to message queue so it could be processed later in a consumer.

<?php use Enqueue\Client\ProducerInterface;

use Enqueue\AsyncCommand\Commands;

use Enqueue\AsyncCommand\RunCommand;

use Symfony\Component\DependencyInjection\ContainerInterface; /** @var $container ContainerInterface */

$producer = $container->get(ProducerInterface::class); /** @var ProducerInterface $producer */$producer = $container->get(ProducerInterface::class); $producer->sendCommand(

Commands::RUN_COMMAND,

new RunCommand('debug:container')

);

Optionally, you can get a command output, as well as exit code and error stream:

<?php use Enqueue\Client\ProducerInterface;

use Enqueue\AsyncCommand\CommandResult;

use Enqueue\AsyncCommand\Commands;

use Enqueue\AsyncCommand\RunCommand;

use Symfony\Component\DependencyInjection\ContainerInterface; /** @var $container ContainerInterface */

$producer = $container->get(ProducerInterface::class); /** @var ProducerInterface $producer */$producer = $container->get(ProducerInterface::class); $promise = $producer->sendCommand(Commands::RUN_COMMAND, new RunCommand(‘debug:container’), true); // do other stuff. if ($replyMessage = $promise->receive(5000)) {

$result = CommandResult::jsonUnserialize(

$replyMessage->getBody()

);

} echo $result->getExitCode().PHP_EOL;

echo $result->getOutput().PHP_EOL;

Don`t forget that you need a consumer working in a background:

$ ./bin/console enqueue:consume --setup-broker -vvv

Neat and simple solution. Enjoy!