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, ); } }