function UpdateManager::fetchDataBatch

Same name and namespace in other branches
  1. 9 core/modules/update/src/UpdateManager.php \Drupal\update\UpdateManager::fetchDataBatch()
  2. 10 core/modules/update/src/UpdateManager.php \Drupal\update\UpdateManager::fetchDataBatch()
  3. 11.x core/modules/update/src/UpdateManager.php \Drupal\update\UpdateManager::fetchDataBatch()

Overrides UpdateManagerInterface::fetchDataBatch

File

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

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;
        }
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.