Skip to content

Added more details to quick purge error messages #243

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

Merged
merged 1 commit into from
Jan 23, 2019
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions Controller/Adminhtml/FastlyCdn/Purge/Quick.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,11 @@ public function execute()

// purge uri
$result = $this->purgeCache->sendPurgeRequest($uri);
if ($result) {
if ($result['status']) {
$this->messageManager->addSuccessMessage(__('The URL\'s "' . $url . '" cache has been cleaned.'));
} else {
$this->getMessageManager()->addErrorMessage(
__('The purge request was not processed successfully.')
__('The purge request was not processed successfully. [' . $result['msg'] . ']')
);
}
}
Expand Down
38 changes: 22 additions & 16 deletions Model/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ class Api
*/
private $state;

private $resultJson;

/**
* Api constructor.
*
Expand Down Expand Up @@ -150,12 +152,14 @@ private function _getWafEndpoint()
* Purge a single URL
*
* @param $url
* @return bool
* @return \Magento\Framework\Controller\Result\Json
* @throws \Zend_Uri_Exception
*/
public function cleanUrl($url)
{
if ($result = $this->_purge($url, 'PURGE')) {
$result = $this->_purge($url, 'PURGE');

if ($result['status']) {
$this->logger->execute($url);
}

Expand All @@ -170,7 +174,7 @@ public function cleanUrl($url)
* Purge Fastly by a given surrogate key
*
* @param $keys
* @return bool
* @return bool|\Magento\Framework\Controller\Result\Json
* @throws \Zend_Uri_Exception
*/
public function cleanBySurrogateKey($keys)
Expand All @@ -191,7 +195,8 @@ public function cleanBySurrogateKey($keys)

foreach ($collection as $keys) {
$payload = json_encode(['surrogate_keys' => $keys]);
if ($result = $this->_purge($uri, null, \Zend_Http_Client::POST, $payload)) {
$result = $this->_purge($uri, null, \Zend_Http_Client::POST, $payload);
if ($result['status']) {
foreach ($keys as $key) {
$this->logger->execute('surrogate key: ' . $key);
}
Expand All @@ -201,27 +206,27 @@ public function cleanBySurrogateKey($keys)
$canPublishPurgeChanges = $this->config->canPublishPurgeChanges();

if ($this->config->areWebHooksEnabled() && ($canPublishKeyUrlChanges || $canPublishPurgeChanges)) {
$status = $result ? '' : 'FAILED ';
$status = $result['status'] ? '' : 'FAILED ';
$this->sendWebHook($status . '*clean by key on ' . join(" ", $keys) . '*');

$canPublishPurgeByKeyDebugBacktrace = $this->config->canPublishPurgeByKeyDebugBacktrace();
$canPublishPurgeDebugBacktrace = $this->config->canPublishPurgeDebugBacktrace();

if ($canPublishPurgeByKeyDebugBacktrace == false && $canPublishPurgeDebugBacktrace == false) {
return $result;
return $result['status'];
}

$this->stackTrace($type . join(" ", $keys));
}
}

return $result;
return $result['status'];
}

/**
* Purge all of Fastly's CDN content. Can be called only once per request
*
* @return bool
* @return bool|\Magento\Framework\Controller\Result\Json
* @throws \Zend_Uri_Exception
*/
public function cleanAll()
Expand All @@ -234,7 +239,8 @@ public function cleanAll()

$type = 'clean/purge all';
$uri = $this->_getApiServiceUri() . 'purge_all';
if ($result = $this->_purge($uri, null)) {
$result = $this->_purge($uri, null);
if ($result['status']) {
$this->logger->execute('clean all items');
}

Expand All @@ -248,13 +254,13 @@ public function cleanAll()
$canPublishPurgeDebugBacktrace = $this->config->canPublishPurgeDebugBacktrace();

if ($canPublishPurgeAllDebugBacktrace == false && $canPublishPurgeDebugBacktrace == false) {
return $result;
return $result['status'];
}

$this->stackTrace($type);
}

return $result;
return $result['status'];
}

/**
Expand All @@ -264,7 +270,7 @@ public function cleanAll()
* @param $type
* @param string $method
* @param null $payload
* @return bool
* @return \Magento\Framework\Controller\Result\Json
* @throws \Zend_Uri_Exception
*/
private function _purge($uri, $type, $method = \Zend_Http_Client::POST, $payload = null)
Expand Down Expand Up @@ -296,8 +302,7 @@ private function _purge($uri, $type, $method = \Zend_Http_Client::POST, $payload
self::FASTLY_HEADER_SOFT_PURGE . ': 1'
);
}

$result = true;
$result['status'] = true;
try {
$client = $this->curlFactory->create();
$client->setConfig(['timeout' => self::PURGE_TIMEOUT]);
Expand All @@ -314,11 +319,12 @@ private function _purge($uri, $type, $method = \Zend_Http_Client::POST, $payload
if ($responseCode == '429') {
throw new LocalizedException(__($responseMessage));
} elseif ($responseCode != '200') {
throw new LocalizedException(__('Return status ' . $responseCode));
throw new LocalizedException(__($responseCode . ': ' . $responseMessage));
}
} catch (\Exception $e) {
$this->logger->critical($e->getMessage(), $uri);
$result = false;
$result['status'] = false;
$result['msg'] = $e->getMessage();
}

if (empty($type)) {
Expand Down