You are here

public function UpdateManager::fetchDataBatch in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/update/src/UpdateManager.php \Drupal\update\UpdateManager::fetchDataBatch()

Processes a step in batch for fetching available update data.

Before calling this method, call UpdateManagerInterface::refreshUpdateData() to clear existing update data and initiate re-fetching.

Parameters

array $context: Reference to an array used for Batch API storage.

Overrides UpdateManagerInterface::fetchDataBatch

See also

\Drupal\update\UpdateManagerInterface::refreshUpdateData()

File

core/modules/update/src/UpdateManager.php, line 196

Class

UpdateManager
Default implementation of UpdateManagerInterface.

Namespace

Drupal\update

Code

public function fetchDataBatch(&$context) {
  if (empty($context['sandbox']['max'])) {
    $context['finished'] = 0;
    $context['sandbox']['max'] = $this->updateProcessor
      ->numberOfQueueItems();
    $context['sandbox']['progress'] = 0;
    $context['message'] = $this
      ->t('Checking available update data ...');
    $context['results']['updated'] = 0;
    $context['results']['failures'] = 0;
    $context['results']['processed'] = 0;
  }

  // Grab another item from the fetch queue.
  for ($i = 0; $i < 5; $i++) {
    if ($item = $this->updateProcessor
      ->claimQueueItem()) {
      if ($this->updateProcessor
        ->processFetchTask($item->data)) {
        $context['results']['updated']++;
        $context['message'] = $this
          ->t('Checked available update data for %title.', [
          '%title' => $item->data['info']['name'],
        ]);
      }
      else {
        $context['message'] = $this
          ->t('Failed to check available update data for %title.', [
          '%title' => $item->data['info']['name'],
        ]);
        $context['results']['failures']++;
      }
      $context['sandbox']['progress']++;
      $context['results']['processed']++;
      $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
      $this->updateProcessor
        ->deleteQueueItem($item);
    }
    else {

      // If the queue is currently empty, we're done. It's possible that
      // another thread might have added new fetch tasks while we were
      // processing this batch. In that case, the usual 'finished' math could
      // get confused, since we'd end up processing more tasks that we thought
      // we had when we started and initialized 'max' with numberOfItems(). By
      // forcing 'finished' to be exactly 1 here, we ensure that batch
      // processing is terminated.
      $context['finished'] = 1;
      return;
    }
  }
}