You are here

protected function PurgersService::checksBeforeTakeoff in Purge 8.3

Perform pre-flight checks.

Parameters

\Drupal\purge\Plugin\Purge\Invalidation\InvalidationInterface[] $invalidations: Non-associative array of invalidation objects that each describe what needs to be invalidated by the external caching system. Usually these objects originate from the queue but direct invalidation is also possible, in either cases the behavior of your plugin stays the same.

Return value

bool TRUE if all checks passed, FALSE if there's reason to stop.

See also

\Drupal\purge\Plugin\Purge\Purger\PurgersServiceInterface::invalidate()

1 call to PurgersService::checksBeforeTakeoff()
PurgersService::invalidate in src/Plugin/Purge/Purger/PurgersService.php
Invalidate content from external caches.

File

src/Plugin/Purge/Purger/PurgersService.php, line 165

Class

PurgersService
Provides the service that distributes access to one or more purgers.

Namespace

Drupal\purge\Plugin\Purge\Purger

Code

protected function checksBeforeTakeoff(array $invalidations) {

  // Block cache invalidation if there's a serious diagnostic severity.
  if ($fire = $this->purgeDiagnostics
    ->isSystemOnFire()) {
    throw new DiagnosticsException($fire
      ->getRecommendation());
  }

  // Stop when no invalidations are given (DX improvement) and then verify if
  // all incoming objects are InvalidationInterface compliant.
  if (empty($invalidations)) {
    $this->logger
      ->debug("no invalidation objects given.");
    return FALSE;
  }
  foreach ($invalidations as $i => $invalidation) {
    if (!$invalidation instanceof InvalidationInterface) {
      throw new BadBehaviorException("Item {$i} is not a \\Drupal\\purge\\Plugin\\Purge\\Invalidation\\InvalidationInterface derivative.");
    }
  }

  // Verify that we have the runtime capacity to process anything at all.
  $inv_limit = $this
    ->capacityTracker()
    ->getRemainingInvalidationsLimit();
  if (!$inv_limit) {
    $this->logger
      ->debug("capacity limits exceeded.");
    throw new CapacityException('Capacity limits exceeded.');
  }
  if (($count = count($invalidations)) > $inv_limit) {
    $this->logger
      ->debug("capacity limit allows @limit invalidations during this request, @no given.", [
      '@limit' => $inv_limit,
      '@no' => $count,
    ]);
    throw new CapacityException("Capacity limit allows {$inv_limit} invalidations during this request, {$count} given.");
  }

  // Attempt to claim the lock to guard that we're the only one processing.
  $lease = $this
    ->capacityTracker()
    ->getLeaseTimeHint(count($invalidations));
  if (!$this->lock
    ->acquire(self::LOCKNAME, (double) $lease)) {
    $this->logger
      ->debug("could not acquire processing lock.");
    throw new LockException("Could not acquire processing lock.");
  }
  return TRUE;
}