12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- <?php
- namespace WorkflowBundle\Services;
- use OldSound\RabbitMqBundle\RabbitMq\ConsumerInterface;
- use PhpAmqpLib\Message\AMQPMessage;
- use Symfony\Component\Process\Process;
- class TaskLoggerService implements ConsumerInterface
- {
-
- /**
- * Directorio donde se guardan los script
- */
- const TASKLOGGER_PATH = '/tmp/tasklogger';
-
- /**
- * $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->getBody());
- if (isset($msgBody['id']) && isset($msgBody['content'])) {
- $taskloggerId = $msgBody['id'];
- $content = $msgBody['content'];
-
- $filename = $this->createTaskLoggerCmdFile($taskloggerId, $content);
- $output = $this->runProcess($filename);
- var_export($output);
- return true;
- }
-
- return false;
- }
-
- /**
- * @param string $taskloggerId
- * @param string $data
- *
- * @return string
- */
- public function createTaskLoggerCmdFile($taskloggerId, $data)
- {
- $mode = 0777;
- $tasklogger_dir = self::TASKLOGGER_PATH . DIRECTORY_SEPARATOR . $taskloggerId;
- if (!file_exists($tasklogger_dir)) {
- mkdir($tasklogger_dir, $mode, true);
- }
-
- $filename = $tasklogger_dir . DIRECTORY_SEPARATOR . 'cmd.sh';
- file_put_contents($filename, $data);
- chmod($filename, $mode);
-
- return $filename;
- }
-
- /**
- * @param string $filename
- *
- * @return array
- */
- public function runProcess($filename)
- {
- $process = new Process($filename);
- $process->run();
-
- return array(
- 'output' => $process->getOutput(),
- 'error' => $process->getErrorOutput(),
- 'exit_code' => $process->getExitCode(),
- );
- }
- }
|