You are here

function varnish_focal_point_purge_entity_update in Varnish purger 8

Same name and namespace in other branches
  1. 8.2 modules/varnish_focal_point_purge/varnish_focal_point_purge.module \varnish_focal_point_purge_entity_update()

Implements hook_entity_update().

File

modules/varnish_focal_point_purge/varnish_focal_point_purge.module, line 42
Contains varnish_focal_point_purge.module.

Code

function varnish_focal_point_purge_entity_update(EntityInterface $entity) {
  if ($entity
    ->bundle() !== 'focal_point') {
    return;
  }

  // Get the name(s) of the varnish purge configs, they get random suffix.
  // This is ugly, should be done better I guess.
  $query = \Drupal::database()
    ->select('config', 'c');
  $query
    ->fields('c', [
    'name',
  ]);
  $query
    ->condition('c.name', $query
    ->escapeLike('varnish_purger.settings') . '%', 'LIKE');
  $varnish_purgers = $query
    ->execute()
    ->fetchAllKeyed(0, 0);

  // Set the array to use for varnish purgers.
  $purgers = [];
  foreach ($varnish_purgers as $key => $value) {
    $config_purge = \Drupal::config($key);
    $purgers[$key]['hostname'] = $config_purge
      ->get('hostname');
    $purgers[$key]['port'] = $config_purge
      ->get('port');
  }
  if (empty($varnish_purgers)) {
    return;
  }
  $file = File::load($entity->entity_id->value);
  if (!$file instanceof FileInterface) {
    return;
  }

  // TODO: Consider are we not doing duplicated work related to varnish_image purge...
  // Do the actual purging...

  /* @var $client Client */
  $client = \Drupal::service('http_client');

  // Generator for purge requests to be sent out.
  $requests = function () use ($client, $file) {
    $styles = ImageStyle::loadMultiple();

    /* @var $style Drupal\image\Entity\ImageStyle; */
    foreach ($styles as $style) {
      $url = $style
        ->buildUrl($file
        ->getFileUri());
      (yield function () use ($client, $url) {
        return $client
          ->requestAsync('URIBAN', $url)
          ->then(function () {
        }, function ($reason) use ($url) {
          $message = $reason instanceof \Exception ? $reason
            ->getMessage() : (string) $reason;
          $logger = \Drupal::logger('varnish_focal_point_purge');
          $logger
            ->error("URL not purged {$url} {$message}");
        });
      });
    }
  };

  // Prepare a POOL that will make the requests with a given concurrency.
  // TODO: Have the concurrency exposed as configuration somewhere.
  $concurrency = VarnishPurger::VARNISH_PURGE_CONCURRENCY;
  (new \GuzzleHttp\Pool($client, $requests(), [
    'concurrency' => $concurrency,
  ]))
    ->promise()
    ->wait();
}