1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- <?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_DIR = '/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)
- {
- $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
- * @param string $cmd
- *
- * @return string
- */
- public function createTaskLoggerCmdFile($taskloggerId, $cmd)
- {
- $mode = 0777;
- $tasklogger_dir = self::TASKLOGGER_DIR . DIRECTORY_SEPARATOR . $taskloggerId;
- if (!file_exists($tasklogger_dir)) {
- mkdir($tasklogger_dir, $mode, true);
- }
-
- $file_name = $tasklogger_dir . DIRECTORY_SEPARATOR .'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);
- $process->run();
-
- return array(
- 'output' => $process->getOutput(),
- 'error' => $process->getErrorOutput(),
- );
- }
- }
|