Skip to content

Commit

Permalink
added a "failover" system for bad crest requests (HTTP status 5xx,.. )
Browse files Browse the repository at this point in the history
  • Loading branch information
exodus4d committed May 19, 2016
1 parent 95ff8cd commit f4079a0
Showing 1 changed file with 54 additions and 26 deletions.
80 changes: 54 additions & 26 deletions app/main/lib/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ class Web extends \Web {

const ERROR_STATUS_LOG = 'HTTP %s: \'%s\' | url: %s \'%s\'%s';

/**
* max number of CREST curls for a single endpoint until giving up...
* this is because CREST is not very stable
*/
const RETRY_COUNT_MAX = 3;

/**
* end of line
* @var string
Expand Down Expand Up @@ -92,12 +98,17 @@ protected function getCacheKey($url, $options = null){
* @param string $url
* @param array|null $options
* @param array $additionalOptions
* @param int $retryCount request counter for failed crest call
* @return array|FALSE|mixed
*/
public function request($url,array $options = null, $additionalOptions = []) {
public function request($url,array $options = null, $additionalOptions = [], $retryCount = 0 ) {
$f3 = \Base::instance();

if( !$f3->exists( $hash = $this->getCacheKey($url, $options) ) ){

// retry same request until request limit is reached
$retry = false;

$result = parent::request($url, $options);
$result['timeout'] = false;
$statusCode = $this->getStatusCodeFromHeaders( $result['headers'] );
Expand Down Expand Up @@ -130,35 +141,43 @@ public function request($url,array $options = null, $additionalOptions = []) {
case 502:
case 503:
case 505:
$errorMsg = $this->getErrorMessageFromJsonResponse(
$statusCode,
$options['method'],
$url,
json_decode($result['body'])
);
LogController::getLogger('error')->write($errorMsg);
$retry = true;

if( $retryCount == self::RETRY_COUNT_MAX ){
$errorMsg = $this->getErrorMessageFromJsonResponse(
$statusCode,
$options['method'],
$url,
json_decode($result['body'])
);
LogController::getLogger('error')->write($errorMsg);

// trigger error
$f3->error($statusCode, $errorMsg);
// trigger error
$f3->error($statusCode, $errorMsg);
}
break;
case 504:
case 0:
// timeout -> response should not be cached
$result['timeout'] = true;

$errorMsg = $this->getErrorMessageFromJsonResponse(
504,
$options['method'],
$url,
json_decode($result['body'])
);

// log error
LogController::getLogger('error')->write($errorMsg);

if($additionalOptions['suppressTimeoutErrors'] !== true){
// trigger error
$f3->error(504, $errorMsg);
$retry = true;

if( $retryCount == self::RETRY_COUNT_MAX ){
// timeout -> response should not be cached
$result['timeout'] = true;

$errorMsg = $this->getErrorMessageFromJsonResponse(
504,
$options['method'],
$url,
json_decode($result['body'])
);

// log error
LogController::getLogger('error')->write($errorMsg);

if($additionalOptions['suppressTimeoutErrors'] !== true){
// trigger error
$f3->error(504, $errorMsg);
}
}
break;
default:
Expand All @@ -172,6 +191,15 @@ public function request($url,array $options = null, $additionalOptions = []) {
LogController::getLogger('error')->write($errorMsg);
break;
}

if(
$retry &&
$retryCount < self::RETRY_COUNT_MAX
){
$retryCount++;
$this->request($url, $options, $additionalOptions, $retryCount);
}

}else{
$result = $f3->get($hash);
}
Expand Down

0 comments on commit f4079a0

Please sign in to comment.