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->getBody()); if (isset($msgBody['id']) && isset($msgBody['content'])) { $taskloggerId = $msgBody['id']; $content = $msgBody['content']; $filename = $this->createTaskLoggerCmdFile($taskloggerId, $content); $output = $this->runProcess($filename); $monologLoggerId = 'monolog.logger.devicelog'; if ($this->serviceContainer->has($monologLoggerId)) { $this->serviceContainer->get($monologLoggerId)->info($output['output'], [ 'deviceType' => $msgBody['entityClass'], 'deviceId' => $msgBody['entityId'], ]); } 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) { $process = new Process($filename); $process->run(); return array( 'output' => $process->getOutput(), 'error' => $process->getErrorOutput(), 'exit_code' => $process->getExitCode(), ); } }