/** * @test */ public function shouldThrowExceptionOnError() { global $mockCurlError; $mockCurlError = true; $this->setExpectedException('\\Exception', 'Curl error: cURL timeout'); $curlRequest = new CurlRequest(); $curlRequest->getCurlResult('non-existent-url'); $mockCurlError = false; }
/** * @param InputInterface $input * @param OutputInterface $output * @return int */ protected function execute(InputInterface $input, OutputInterface $output) { $playbookFile = $input->getOption('playbook'); $playbook = $this->fileLoader->load($playbookFile); $this->variablesPlacer->setVars(Registry::get('container')['argv_parser']->all()); $this->variablesPlacer->setText($playbook); $playbook = $this->variablesPlacer->place(); $url = sprintf('http://%s:%d/playbook/run/', $input->getOption('host'), $input->getOption('port')); $response = $this->curlRequest->getCurlResult($url, [CURLOPT_POST => true, CURLOPT_POSTFIELDS => ['playbook' => urlencode($playbook), 'filename' => $playbookFile], CURLOPT_TIMEOUT_MS => 100000]); echo $response['body']; }
/** * Fires request to Slack server and returns response * @param string $method Slack API method to call * @param array $data request data * @return array * @throws \Exception */ private function processRequest($method, $data = []) { $methodsToFilter = ['api.test', 'rtm.start', 'chat.postMessage']; $method = $this->getApiMethodName($method); $url = self::BASE_URL . $method; $data['token'] = $this->token; if (true !== Ar::get($data, 'in_logger')) { Logger::get()->raw("➡️ %s", $url, json_encode($data)); } /** @var ApiCache $cache */ $cache = Registry::get('container')['api_cache']; $cachedResponse = $cache->get($url, $data); if (null !== $cachedResponse && !in_array($method, $methodsToFilter)) { if (true !== Ar::get($data, 'in_logger')) { Logger::get()->raw("⬅️ %s: cache hit", $url); } return $cachedResponse; } $result = $this->curlRequest->getCurlResult($url, [CURLOPT_POST => true, CURLOPT_POSTFIELDS => $data])['body']; if (true !== Ar::get($data, 'in_logger')) { Logger::get()->raw("⬅️ %s", $url, $result); } if (!in_array($method, $methodsToFilter)) { $cache->set($url, $data, json_decode($result, true)); } return json_decode($result, true); }
/** * @param InputInterface $input * @param OutputInterface $output * @return int */ protected function execute(InputInterface $input, OutputInterface $output) { $this->logPath = $input->getOption('log'); $cronInfoUrl = sprintf('http://%s:%d/info/cron/', $input->getOption('host'), $input->getOption('port')); $response = json_decode($this->curlRequest->getCurlResult($cronInfoUrl, [CURLOPT_TIMEOUT_MS => 100000])['body'], true); if (!is_array($response)) { $this->log('Core connection failed, exiting'); throw new \RuntimeException('Error connecting to core server'); } foreach ($response as $cronItem) { $this->cronExpression->setExpression(Ar::get($cronItem, 'time')); if ($this->cronExpression->isDue()) { $this->log('Executing: ' . Ar::get($cronItem, 'type')); switch (Ar::get($cronItem, 'type')) { case 'playbook': $url = sprintf('http://%s:%d/playbook/run/', $input->getOption('host'), $input->getOption('port')); $playbookFile = Ar::get($cronItem, 'playbook'); $playbook = $this->fileLoader->load($playbookFile); $this->curlRequest->getCurlResult($url, [CURLOPT_POST => true, CURLOPT_POSTFIELDS => ['playbook' => urlencode($playbook), 'filename' => $playbookFile], CURLOPT_TIMEOUT_MS => 100000]); $this->log('Executed playbook: ' . $playbookFile); break; case 'command': $url = sprintf('http://%s:%d/command/run/', $input->getOption('host'), $input->getOption('port')); $command = Ar::get($cronItem, 'command'); if (null === $command) { break; } $this->curlRequest->getCurlResult($url, [CURLOPT_POST => true, CURLOPT_POSTFIELDS => ['command' => urlencode($command)], CURLOPT_TIMEOUT_MS => 100000]); $this->log('Executed command: ' . $command); break; case 'curl': break; } } } }
/** * Main execution loop * @throws \Exception */ protected function processLoop() { while (true) { try { $data = $this->client->receive(); $parsedData = json_decode($data, true); if ('message' === Ar::get($parsedData, 'type') && 'bot_message' !== Ar::get($parsedData, 'subtype')) { echo sprintf("[INFO] Got message: '%s' from %s in %s\n", Ar::get($parsedData, 'text') ?: '<nothing>', Ar::get($parsedData, 'user') ?: 'bot', Ar::get($parsedData, 'channel') ?: 'unknown channel'); try { $this->curlRequest->getCurlResult($this->serverUrl, [CURLOPT_POST => true, CURLOPT_POSTFIELDS => ['message' => $data]]); } catch (\Exception $e) { } } } catch (Exception $e) { $result = $this->curlRequest->getCurlResult($this->authUrl); $result = json_decode($result['body'], true); $this->socketUrl = $result['url']; $this->client = $this->createClient(); } } }