You are here

public function CdnWarmer::warmMultiple in Warmer 2.x

Same name and namespace in other branches
  1. 8 modules/warmer_cdn/src/Plugin/warmer/CdnWarmer.php \Drupal\warmer_cdn\Plugin\warmer\CdnWarmer::warmMultiple()

Warms multiple items.

Parameters

array $items: The items to warm.

Return value

int The number of items that were successfully warmed.

Overrides WarmerInterface::warmMultiple

File

modules/warmer_cdn/src/Plugin/warmer/CdnWarmer.php, line 57

Class

CdnWarmer
The cache warmer for the built-in entity cache.

Namespace

Drupal\warmer_cdn\Plugin\warmer

Code

public function warmMultiple(array $items = []) {
  $headers = $this
    ->parseHeaders();
  $verify = (bool) $this
    ->getConfiguration()['verify'];
  $max_concurrent_requests = (int) $this
    ->getConfiguration()['maxConcurrentRequests'];

  // Default to one request at a time.
  if ($max_concurrent_requests <= 0) {
    $max_concurrent_requests = 1;
  }
  $promises = [];
  $success = 0;
  foreach ($items as $key => $url) {

    // Fire async request.
    $promises[] = $this->httpClient
      ->requestAsync('GET', $url, [
      'headers' => $headers,
      'verify' => $verify,
    ])
      ->then(function (ResponseInterface $response) use (&$success) {
      if ($response
        ->getStatusCode() < 399) {
        $success++;
      }
    }, function (\Exception $e) {
      $this
        ->getLogger('warmer')
        ->warning($e
        ->getMessage());
    });

    // Wait for all fired requests if max number is reached.
    $item_keys = array_keys($items);
    if ($key % $max_concurrent_requests == 0 || $key == end($item_keys)) {
      \GuzzleHttp\Promise\all($promises)
        ->wait();
      $promises = [];
    }
  }
  return $success;
}