123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- <?php
- namespace WorkflowBundle\Services;
- use OldSound\RabbitMqBundle\RabbitMq\ConsumerInterface;
- use PhpAmqpLib\Message\AMQPMessage;
- use Symfony\Component\DependencyInjection\ContainerInterface;
- use Symfony\Component\Process\Process;
- use Symfony\Component\Process\Exception\ProcessFailedException;
- class CommandConsumer implements ConsumerInterface
- {
- /**
- * @var ContainerInterface
- */
- protected $serviceContainer;
- /**
- * @param ContainerInterface $serviceContainer
- */
- public function __construct(ContainerInterface $serviceContainer)
- {
- $this->serviceContainer = $serviceContainer;
- }
- /**
- * $msg will be an instance of `PhpAmqpLib\Message\AMQPMessage`
- * with the $msg->body being the data sent over RabbitMQ.
- *
- * @param AMQPMessage $msg
- */
- public function execute(AMQPMessage $msg)
- {
- $msgBody = unserialize($msg->body);
- if (isset($msgBody['name']) && isset($msgBody['args'])) {
- // command name and args
- $name = $msgBody['name'];
- $args = $msgBody['args'];
- $input = array();
- foreach ($args as $arg) {
- $arg = str_replace('\\', '\\\\', $arg);
- $pos = strpos($arg, ':');
- $pos2 = strpos($arg, '--');
- if ($pos2 === false) {
- // command argument, no option
- $input[] = substr($arg, $pos + 1);
- } else {
- // reemplazo el primer : por = para las options
- $input[] = preg_replace('/:/', '=', $arg, 1);
- }
- }
- if(in_array("--executeName",$input)) {
- $execute = $name;
- } else {
- $consolePath = $this->serviceContainer->get('kernel')->getRootDir() . '/../bin/console';
- $command = $name . ' ' . implode(' ', $input);
- $execute = "{$consolePath} {$command}";
- }
-
- try {
- $process = new Process($execute);
- $process->setTimeout(10 * 60);
- $process->run();
-
- $content = $process->getOutput();
- echo $content;
- } catch (ProcessFailedException $e) {
- echo 'KILL COMMAND: ', $e->getMessage(), "\n";
- } catch (\Exception $e) {
- echo 'KILL COMMAND: ', $e->getMessage(), "\n";
- }
-
- return true;
- }
- return false;
- }
- }
|