View source
<?php
namespace Drupal\varnish_purger\Plugin\Purge\Purger;
use Drupal\purge\Plugin\Purge\Purger\PurgerInterface;
use Drupal\purge\Plugin\Purge\Invalidation\InvalidationInterface;
use GuzzleHttp\Pool;
class VarnishPurger extends VarnishPurgerBase implements PurgerInterface {
const VARNISH_PURGE_CONCURRENCY = 10;
public function invalidate(array $invalidations) {
$requests = function () use ($invalidations) {
$client = $this->client;
$method = $this->settings->request_method;
$logger = $this
->logger();
foreach ($invalidations as $invalidation) {
$token_data = [
'invalidation' => $invalidation,
];
$uri = $this
->getUri($token_data);
$options = $this
->getOptions($token_data);
(yield function () use ($client, $uri, $method, $options, $invalidation, $logger) {
return $client
->requestAsync($method, $uri, $options)
->then(function ($response) use ($invalidation) {
$invalidation
->setState(InvalidationInterface::SUCCEEDED);
}, function ($reason) use ($invalidation, $uri, $options, $logger) {
$invalidation
->setState(InvalidationInterface::FAILED);
$message = $reason instanceof \Exception ? $reason
->getMessage() : (string) $reason;
$headers = $options['headers'];
unset($options['headers']);
$debug = json_encode(str_replace("\n", ' ', [
'msg' => $message,
'uri' => $uri,
'method' => $this->settings->request_method,
'guzzle_opt' => $options,
'headers' => $headers,
]));
$logger
->emergency("item failed due @e, details (JSON): @debug", [
'@e' => is_object($reason) ? get_class($reason) : (string) $reason,
'@debug' => $debug,
]);
});
});
}
};
(new Pool($this->client, $requests(), [
'concurrency' => self::VARNISH_PURGE_CONCURRENCY,
]))
->promise()
->wait();
}
}