Skip to content
Snippets Groups Projects
Commit f5bbe1e2 authored by David Barratt's avatar David Barratt
Browse files

Issue #3467741 by davidwbarratt: When an error is thrown the status is never updated

parent 601cc959
No related branches found
No related tags found
1 merge request!4Update State on success/failure of each chunk.
......@@ -5,11 +5,10 @@ namespace Drupal\cloudflare_worker_purge\Plugin\Purge\Purger;
use Drupal\Core\Config\Config;
use Drupal\purge\Plugin\Purge\Invalidation\InvalidationInterface;
use Drupal\purge\Plugin\Purge\Purger\PurgerBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Promise\Utils;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Cloudflare Worker purger.
......@@ -65,7 +64,7 @@ class CloudflareWorkerPurger extends PurgerBase {
Config $cloudflareConfig,
Config $config,
ClientInterface $client,
LoggerInterface $logger
LoggerInterface $logger,
) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
......@@ -108,6 +107,10 @@ class CloudflareWorkerPurger extends PurgerBase {
* {@inheritdoc}
*/
public function invalidate(array $invalidations) {
if (empty($invalidations)) {
return;
}
$url = $this->config->get('url');
if (empty($url)) {
......@@ -121,47 +124,64 @@ class CloudflareWorkerPurger extends PurgerBase {
return;
}
if (empty($invalidations)) {
return;
// Group the messages by tag.
/** @var array<string,InvalidationInterface[]> */
$grouped = array_reduce($invalidations, function ($carry, $item) {
$expression = $item->getExpression();
if (empty($expression) || !is_string($expression)) {
$item->setState(InvalidationInterface::NOT_SUPPORTED);
return $carry;
}
if (!array_key_exists($expression, $carry)) {
$carry[$expression] = [$item];
}
else {
$carry[$expression][] = $item;
}
return $carry;
}, []);
/** @var array<int,array<string,InvalidationInterface[]>> */
$chunks = array_chunk($grouped, self::MAX_TAG_PURGES_PER_REQUEST, TRUE);
/** @var array<string,string> */
$headers = [];
if ($this->cloudflareConfig->get('zone_id')) {
$headers['CF-Zone'] = $this->cloudflareConfig->get('zone_id');
}
if ($this->cloudflareConfig->get('email')) {
$headers['X-Auth-Email'] = $this->cloudflareConfig->get('email');
}
if ($this->cloudflareConfig->get('apikey')) {
$headers['X-Auth-Key'] = $this->cloudflareConfig->get('apikey');
}
// Get the unique tags to purge.
$tags = array_unique(array_map(function ($invalidation) {
return $invalidation->getExpression();
}, $invalidations));
$promises = array_map(function ($chunk) use ($url) {
return $this->client->postAsync($url, [
'headers' => [
'CF-Zone' => $this->cloudflareConfig->get('zone_id'),
'X-Auth-Email' => $this->cloudflareConfig->get('email'),
'X-Auth-Key' => $this->cloudflareConfig->get('apikey'),
],
$promises = array_map(function ($chunk) use ($url, $headers) {
return $this->client->requestAsync('POST', $url, [
'headers' => $headers,
'json' => [
'tags' => $chunk,
'tags' => array_keys($chunk),
],
])->then(function ($response) use ($chunk) {
return $chunk;
}, function (ClientException $e) {
$this->logger->error($e->getMessage());
// None of the tags were resolved.
return [];
])->then(function () use ($chunk) {
foreach ($chunk as $invalidations) {
foreach ($invalidations as $invalidation) {
$invalidation->setState(InvalidationInterface::SUCCEEDED);
}
}
}, function (\Throwable $e) use ($chunk) {
$this->logger->critical($e->getMessage());
foreach ($chunk as $invalidations) {
foreach ($invalidations as $invalidation) {
$invalidation->setState(InvalidationInterface::FAILED);
}
}
});
}, array_chunk($tags, self::MAX_TAG_PURGES_PER_REQUEST));
}, $chunks);
// Execute the promises.
$resolved = array_merge(...Utils::unwrap($promises));
// Set the state of each invalidation.
foreach ($invalidations as $invalidation) {
if (in_array($invalidation->getExpression(), $resolved)) {
$invalidation->setState(InvalidationInterface::SUCCEEDED);
}
else {
$invalidation->setState(InvalidationInterface::FAILED);
}
}
Utils::unwrap($promises);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment