123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- <?php
- namespace WorkflowBundle\Services;
- use OldSound\RabbitMqBundle\RabbitMq\ConsumerInterface;
- use PhpAmqpLib\Message\AMQPMessage;
- use Symfony\Component\Process\Process;
- use Symfony\Component\DependencyInjection\ContainerInterface;
- class TaskLoggerService implements ConsumerInterface
- {
- /**
- * Directorio donde se guardan los script
- */
- const TASKLOGGER_PATH = '/tmp/tasklogger';
- /**
- * @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['id']) && isset($msgBody['content'])) {
- $taskloggerId = $msgBody['id'];
- $content = $msgBody['content'];
- $filename = $this->createTaskLoggerCmdFile($taskloggerId, $content);
- $output = $this->runProcess($filename);
- $output["process"] = $filename;
-
- $monologLoggerId = 'monolog.logger.devicelog';
- if ($this->serviceContainer->has($monologLoggerId)) {
- $this->serviceContainer->get($monologLoggerId)->info($output['output'], [
- 'deviceType' => $msgBody['entityClass'],
- 'deviceId' => $msgBody['entityId'],
- 'tenancyId' => $this->serviceContainer->get('base_tenancy.tenancy_service')->getTenancyIdCurrent(),
- ]);
- }
-
- echo PHP_EOL . PHP_EOL;
- var_export($output);
- echo PHP_EOL . PHP_EOL;
- 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)
- {
- $predir = getcwd();
- chdir(dirname($filename));
- $process = new Process($filename);
- try {
- $process->run();
- chdir($predir);
- $output = $process->getOutput();
- $error = $process->getErrorOutput();
- $exit_code = $process->getExitCode();
- } catch (\Throwable $t) {
- $output = $filename .
- "\n==================================ERROR==================================\n";
- $error = "\n==================================ERROR==================================\n" .
- $t->getMessage() . "\n" . $t->getTraceAsString() .
- "\n==================================ERROR==================================\n";
- $exit_code = $t->getCode();
- }
- return array(
- 'output' => $output,
- 'error' => $error,
- 'exit_code' => $exit_code,
- );
- }
- }
|