Commit 8e02edb4 authored by Vadym Abramchuk's avatar Vadym Abramchuk Committed by Mateu Aguiló Bosch
Browse files

Issue #3272428 by abramm, e0ipso: CDN Warmer always waits for the first Promise in the batch

parent aa37940f
Loading
Loading
Loading
Loading
+10 −4
Original line number Diff line number Diff line
@@ -8,6 +8,7 @@ use Drupal\Core\Form\SubformStateInterface;
use Drupal\Core\Logger\LoggerChannelTrait;
use Drupal\warmer\Plugin\WarmerPluginBase;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Promise\Utils;
use Psr\Http\Message\ResponseInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

@@ -66,7 +67,7 @@ final class CdnWarmer extends WarmerPluginBase {
    $promises = [];
    $success = 0;

    foreach ($items as $key => $url) {
    foreach ($items as $url) {
      // Fire async request.
      $promises[] = $this->httpClient
        ->requestAsync('GET', $url, ['headers' => $headers, 'verify' => $verify])
@@ -77,14 +78,19 @@ final class CdnWarmer extends WarmerPluginBase {
        }, 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();
      if (count($promises) >= $max_concurrent_requests) {
        Utils::all($promises)->wait();
        $promises = [];
      }
    }

    // Wait for remaining requests to complete, if any.
    if (!empty($promises)) {
      Utils::all($promises)->wait();
    }

    return $success;
  }