TaskLoggerService.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. echo PHP_EOL . PHP_EOL;
  48. var_export($output);
  49. echo PHP_EOL . PHP_EOL;
  50. return true;
  51. }
  52. return false;
  53. }
  54. /**
  55. * @param string $taskloggerId
  56. * @param string $data
  57. *
  58. * @return string
  59. */
  60. public function createTaskLoggerCmdFile($taskloggerId, $data)
  61. {
  62. $mode = 0777;
  63. $tasklogger_dir = self::TASKLOGGER_PATH . DIRECTORY_SEPARATOR . $taskloggerId;
  64. if (!file_exists($tasklogger_dir)) {
  65. mkdir($tasklogger_dir, $mode, true);
  66. }
  67. $filename = $tasklogger_dir . DIRECTORY_SEPARATOR . 'cmd.sh';
  68. file_put_contents($filename, $data);
  69. chmod($filename, $mode);
  70. return $filename;
  71. }
  72. /**
  73. * @param string $filename
  74. *
  75. * @return array
  76. */
  77. public function runProcess($filename)
  78. {
  79. $predir = getcwd();
  80. chdir(dirname($filename));
  81. $process = new Process($filename);
  82. $process->run();
  83. chdir($predir);
  84. return array(
  85. 'output' => $process->getOutput(),
  86. 'error' => $process->getErrorOutput(),
  87. 'exit_code' => $process->getExitCode(),
  88. );
  89. }
  90. }