1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- <?php
- namespace WorkflowBundle\Services;
- use OldSound\RabbitMqBundle\RabbitMq\ConsumerInterface;
- use PhpAmqpLib\Message\AMQPMessage;
- use Symfony\Component\Process\Process;
- use Symfony\Component\Process\Exception\ProcessFailedException;
- class TaskLoggerService implements ConsumerInterface
- {
- /**
- * $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)
- {
- $data = unserialize($msg->getBody());
- if (isset($data['id']) && isset($data['cmd'])) {
- $taskloggerId = $data['id'];
- $cmd = $data['cmd'];
-
- $file_name = $this->createTaskLoggerCmdFile($taskloggerId, $cmd);
- $output = $this->runFileProcess($file_name);
- return true;
- }
-
- return false;
- }
-
- /**
- * @param string $taskloggerId
- *
- * @return string
- */
- public function createTaskLoggerCmdFile($taskloggerId, $cmd)
- {
- $mode = 0777;
- $tasklogger_dir = "/tmp/tasklogger/{$taskloggerId}/";
- if (!file_exists($tasklogger_dir)) {
- mkdir($tasklogger_dir, $mode, true);
- }
-
- $file_name = $tasklogger_dir.'cmd.sh';
- file_put_contents($file_name, $cmd);
- chmod($file_name, $mode);
-
- return $file_name;
- }
-
- /**
- * @param string $filename
- *
- * @return string
- */
- public function runFileProcess($filename)
- {
- $process = new Process($filename);
- $output = null;
- try {
- $process->mustRun();
- $output = $process->getOutput();
- } catch (ProcessFailedException $e) {
- $output = $e->getMessage();
- }
-
- return $output;
- }
- }
|