Created
February 13, 2018 14:49
-
-
Save jverdeyen/d8b8953655b74e6c3b91584d40ba15ce to your computer and use it in GitHub Desktop.
Enqueue Symfony Console commands [RunCommandProcessor.php]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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