TaskLoggerService.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace WorkflowBundle\Services;
  3. use OldSound\RabbitMqBundle\RabbitMq\ConsumerInterface;
  4. use PhpAmqpLib\Message\AMQPMessage;
  5. use Symfony\Component\Process\Process;
  6. use Symfony\Component\DependencyInjection\ContainerInterface;
  7. class TaskLoggerService implements ConsumerInterface
  8. {
  9. /**
  10. * Directorio donde se guardan los script
  11. */
  12. const TASKLOGGER_PATH = '/tmp/tasklogger';
  13. /**
  14. * @var ContainerInterface
  15. */
  16. protected $serviceContainer;
  17. /**
  18. * @param ContainerInterface $serviceContainer
  19. */
  20. public function __construct(ContainerInterface $serviceContainer)
  21. {
  22. $this->serviceContainer = $serviceContainer;
  23. }
  24. /**
  25. * $msg will be an instance of `PhpAmqpLib\Message\AMQPMessage`
  26. * with the $msg->body being the data sent over RabbitMQ.
  27. *
  28. * @param AMQPMessage $msg
  29. */
  30. public function execute(AMQPMessage $msg)
  31. {
  32. $msgBody = unserialize($msg->body);
  33. if (isset($msgBody['id']) && isset($msgBody['content'])) {
  34. $taskloggerId = $msgBody['id'];
  35. $content = $msgBody['content'];
  36. $filename = $this->createTaskLoggerCmdFile($taskloggerId, $content);
  37. $output = $this->runProcess($filename);
  38. $output["process"] = $filename;
  39. $monologLoggerId = 'monolog.logger.devicelog';
  40. if ($this->serviceContainer->has($monologLoggerId)) {
  41. $this->serviceContainer->get($monologLoggerId)->info($output['output'], [
  42. 'deviceType' => $msgBody['entityClass'],
  43. 'deviceId' => $msgBody['entityId'],
  44. 'tenancyId' => $this->serviceContainer->get('base_tenancy.tenancy_service')->getTenancyIdCurrent(),
  45. ]);
  46. }
  47. var_export($output);
  48. return true;
  49. }
  50. return false;
  51. }
  52. /**
  53. * @param string $taskloggerId
  54. * @param string $data
  55. *
  56. * @return string
  57. */
  58. public function createTaskLoggerCmdFile($taskloggerId, $data)
  59. {
  60. $mode = 0777;
  61. $tasklogger_dir = self::TASKLOGGER_PATH . DIRECTORY_SEPARATOR . $taskloggerId;
  62. if (!file_exists($tasklogger_dir)) {
  63. mkdir($tasklogger_dir, $mode, true);
  64. }
  65. $filename = $tasklogger_dir . DIRECTORY_SEPARATOR . 'cmd.sh';
  66. file_put_contents($filename, $data);
  67. chmod($filename, $mode);
  68. return $filename;
  69. }
  70. /**
  71. * @param string $filename
  72. *
  73. * @return array
  74. */
  75. public function runProcess($filename)
  76. {
  77. $predir = getcwd();
  78. chdir(dirname($filename));
  79. $process = new Process($filename);
  80. $process->run();
  81. chdir($predir);
  82. return array(
  83. 'output' => $process->getOutput(),
  84. 'error' => $process->getErrorOutput(),
  85. 'exit_code' => $process->getExitCode(),
  86. );
  87. }
  88. }