private function initClient() { $handlerStack = HandlerStack::create(); $handlerStack->push(MiddlewareBuilder::factoryForPing($this->logger, self::MAX_RETRIES)); $handlerStack->push(Middleware::log($this->logger, new MessageFormatter('{hostname} {req_header_User-Agent} - [{ts}] \\"{method} {resource} {protocol}/{version}\\" {code} {res_header_Content-Length}'))); $this->client = new \GuzzleHttp\Client(['base_uri' => $this->storageApi->getApiUrl(), 'handler' => $handlerStack]); }
public function __construct(array $config = [], Logger $logger) { if (array_key_exists('handler', $config)) { throw new \InvalidArgumentException('Handler option is unsupported in Orchestrator client'); } $handlerStack = HandlerStack::create(); $handlerStack->push(EffectiveUrlMiddleware::middleware(self::EFFECTIVE_URL_HEADER_NAME)); // effective url alternative $handlerStack->push(MiddlewareBuilder::factoryFor500($logger, 7)); $handlerStack->push(MiddlewareBuilder::factory($logger, 18)); $config['handler'] = $handlerStack; parent::__construct($config); $this->logger = $logger; }
private static function create500Decider(LoggerInterface $logger, $maxRetries) { return function ($retries, RequestInterface $request, ResponseInterface $response = null, $error = null) use($maxRetries, $logger) { if ($retries >= $maxRetries) { return false; } elseif ($response && in_array($response->getStatusCode(), array(500))) { if (!$request->hasHeader('X-Orchestrator-Poll')) { return false; } $formatter = new MessageFormatter('[{ts}] {method} {url} - {code} {phrase}'); $delay = MiddlewareBuilder::createDefaultDelay(); $logger->log(LogLevel::INFO, sprintf('%s - Retries: %s, Delay: %s s', $formatter->format($request, $response, $error), $retries + 1, $delay($retries) / 1000)); return true; } elseif ($error) { return false; } else { return false; } }; }
/** * @FIXME replace with ping client * * @param Metadata\Job $job * @param Encryptor $encryptor * @return bool */ private function sapiPing(Metadata\Job $job, Encryptor $encryptor) { $maxRetries = 4; $handlerStack = HandlerStack::create(); $handlerStack->push(MiddlewareBuilder::factoryForWatchdog($this->logger, $maxRetries)); try { $guzzle = new \GuzzleHttp\Client(['handler' => $handlerStack]); $response = $guzzle->get(sprintf('%s/storage/tokens/verify', $this->getConfiguration()->getStorageApiUrl()), array('config' => array('curl' => array(CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_0)), 'headers' => array('X-StorageApi-Token' => $encryptor->decrypt($job->getToken()["token"]), 'X-User-Agent', KeboolaOrchestratorBundle::SYRUP_COMPONENT_NAME . " - Watchdog"))); if ($response->getStatusCode() == 200) { $data = ResponseDecoder::decode($response); if (array_key_exists('id', $data) && array_key_exists('token', $data)) { return true; } } } catch (\Exception $e) { } return false; }