You are here

public function CronUpdater::handleCron in Automatic Updates 8.2

Handles updates during cron.

File

src/CronUpdater.php, line 91

Class

CronUpdater
Defines a service that updates via cron.

Namespace

Drupal\automatic_updates

Code

public function handleCron() : void {
  $level = $this->configFactory
    ->get('automatic_updates.settings')
    ->get('cron');

  // If automatic updates are disabled, bail out.
  if ($level === static::DISABLED) {
    return;
  }
  $recommender = new UpdateRecommender();
  try {
    $recommended_release = $recommender
      ->getRecommendedRelease(TRUE);
  } catch (\Throwable $e) {
    $this->logger
      ->error($e
      ->getMessage());
    return;
  }

  // If we're already up-to-date, there's nothing else we need to do.
  if ($recommended_release === NULL) {
    return;
  }
  $project = $recommender
    ->getProjectInfo();
  if (empty($project['existing_version'])) {
    $this->logger
      ->error('Unable to determine the current version of Drupal core.');
    return;
  }

  // If automatic updates are only enabled for security releases, bail out if
  // the recommended release is not a security release.
  if ($level === static::SECURITY && !$recommended_release
    ->isSecurityRelease()) {
    return;
  }

  // @todo Use the queue to add update jobs allowing jobs to span multiple
  //   cron runs.
  $recommended_version = $recommended_release
    ->getVersion();
  try {
    $this->updater
      ->begin([
      'drupal' => $recommended_version,
    ]);
    $this->updater
      ->stage();
    $this->updater
      ->commit();
    $this->updater
      ->clean();
  } catch (\Throwable $e) {
    $this->logger
      ->error($e
      ->getMessage());
    return;
  }
  $this->logger
    ->info('Drupal core has been updated from %previous_version to %update_version', [
    '%previous_version' => $project['existing_version'],
    '%update_version' => $recommended_version,
  ]);
}