Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delayed Retry Request Logic #20

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 63 additions & 4 deletions src/Provider/XiboEntityProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ private function getAccessToken()
public function get($url, $params = [])
{
$this->logger->debug('Passing GET params to request');
return $this->request('GET', $url . '?' . http_build_query($params));
return $this->delayedRetryRequest('GET', $url, $params);
}

/**
Expand All @@ -108,7 +108,7 @@ public function get($url, $params = [])
public function post($url, $params = [])
{
$this->logger->debug('Passing POST params to request');
return $this->request('POST', $url, $params);
return $this->delayedRetryRequest('POST', $url, $params);
}

/**
Expand All @@ -119,7 +119,7 @@ public function post($url, $params = [])
public function put($url, $params = [])
{
$this->logger->debug('Passing PUT params to request');
return $this->request('PUT', $url, $params);
return $this->delayedRetryRequest('PUT', $url, $params);
}

/**
Expand All @@ -130,7 +130,7 @@ public function put($url, $params = [])
public function delete($url, $params = [])
{
$this->logger->debug('Passing Delete params to request');
return $this->request('DELETE', $url, $params);
return $this->delayedRetryRequest('DELETE', $url, $params);
}

/**
Expand All @@ -143,10 +143,18 @@ public function delete($url, $params = [])
*/
private function request($method, $url, $params = [])
{
//Capture Statistics on Xibo API Calls
$this->getLogger()->info('request-statistics', array('method' => $method, 'url' => $url, 'params' => http_build_query($params)));

$this->getLogger()->debug('Creating a new request with received parameters');
$options = [
'headers' => null, 'body' => null
];

if($method == 'GET'){
$url = $url . '?' . http_build_query($params);
}

// Multipart
if (array_key_exists('multipart', $params)) {
// Build the multipart message
Expand All @@ -166,4 +174,55 @@ private function request($method, $url, $params = [])
$this->logger->debug('Getting parsed response from Abstract Provider');
return $this->provider->getParsedResponse($request);
}

/**
* delayedRetryRequest
* @param $method
* @param $url
* @param array $params
* @return mixed
* @throws EmptyProviderException
*/
private function delayedRetryRequest($method, $url, $params = [])
{
// Set delayed retry time in seconds
$delayedRetryTime = defined($GLOBALS['delayedRetryTime']) ? $GLOBALS['delayedRetryTime']: 20;

$res = null;
try {
$res = $this->request($method, $url, $params);
}catch(\Xibo\OAuth2\Client\Exception\XiboApiException | \Exception $ex) {
//TODO: Check response for 500 error or rate limited error
$this->logger->error($ex->getMessage());

$retryCount = 0;
do {
sleep($delayedRetryTime);

try {

$this->logger->info("Waited ".$delayedRetryTime." seconds, retry #$retryCount of Xibo call");
$res = $this->request($method, $url, $params);

//Break delayed retry loop request successful
if(isset($res)){
$this->logger->info("Delayed retry successful!");
break;
}
} catch (\Xibo\OAuth2\Client\Exception\XiboApiException | \Exception $ex1) {
//TODO: Check response for 500 error or rate limited error
$this->logger->error($ex1->getMessage());
}
} while (++$retryCount < 2);


//Re-throw exception delayed retry failed
if (!isset($res)) {
$this->logger->error("Delayed retry failed!");
throw $ex;
}
}

return $res;
}
}