Skip to content

Instantly share code, notes, and snippets.

@jverdeyen
Created February 13, 2018 14:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jverdeyen/d8b8953655b74e6c3b91584d40ba15ce to your computer and use it in GitHub Desktop.
Save jverdeyen/d8b8953655b74e6c3b91584d40ba15ce to your computer and use it in GitHub Desktop.
Enqueue Symfony Console commands [RunCommandProcessor.php]
<?php
namespace AppBundle\Service\Queue\Processor;
use Enqueue\Client\CommandSubscriberInterface;
use Enqueue\Consumption\Result;
use Interop\Queue\PsrContext;
use Interop\Queue\PsrMessage;
use Interop\Queue\PsrProcessor;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Process;
class RunCommandProcessor implements PsrProcessor, CommandSubscriberInterface
{
/**
* @var string
*/
private $projectDir;
public function __construct(string $projectDir)
{
$this->projectDir = $projectDir;
}
public function process(PsrMessage $message, PsrContext $context)
{
$commandline = $message->getBody();
$process = new Process('./bin/console '.$commandline, $this->projectDir);
try {
$process->mustRun();
return Result::ACK;
} catch (ProcessFailedException $e) {
return Result::reject(sprintf('The process failed with exception: "%s" in %s at %s', $e->getMessage(), $e->getFile(), $e->getLine()));
}
}
public static function getSubscribedCommand()
{
return 'run_command';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment