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(), ]); } var_export($output); 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); $process->run(); chdir($predir); return array( 'output' => $process->getOutput(), 'error' => $process->getErrorOutput(), 'exit_code' => $process->getExitCode(), ); } }