You are here

function automatic_updates_cron in Automatic Updates 8

Same name and namespace in other branches
  1. 8.2 automatic_updates.module \automatic_updates_cron()
  2. 7 automatic_updates.module \automatic_updates_cron()

Implements hook_cron().

File

./automatic_updates.module, line 77
Contains automatic_updates.module..

Code

function automatic_updates_cron() {
  $state = \Drupal::state();
  $request_time = \Drupal::time()
    ->getRequestTime();
  $last_check = $state
    ->get('automatic_updates.cron_last_check', 0);

  // Only allow cron to run once every hour.
  if ($request_time - $last_check < 3600) {
    return;
  }

  // Checkers should run before updates because of class caching.

  /** @var \Drupal\automatic_updates\Services\NotifyInterface $notify */
  $notify = \Drupal::service('automatic_updates.psa_notify');
  $notify
    ->send();

  /** @var \Drupal\automatic_updates\ReadinessChecker\ReadinessCheckerManagerInterface $checker */
  $checker = \Drupal::service('automatic_updates.readiness_checker');
  foreach ($checker
    ->getCategories() as $category) {
    $checker
      ->run($category);
  }

  // In-place updates won't function for dev releases of Drupal core.
  $dev_core = strpos(\Drupal::VERSION, '-dev') !== FALSE;

  /** @var \Drupal\Core\Config\ImmutableConfig $config */
  $config = \Drupal::config('automatic_updates.settings');
  if (!$dev_core && $config
    ->get('enable_cron_updates')) {
    \Drupal::service('update.manager')
      ->refreshUpdateData();
    \Drupal::service('update.processor')
      ->fetchData();
    $available = update_get_available(TRUE);
    $projects = update_calculate_project_data($available);
    $not_recommended_version = $projects['drupal']['status'] !== UpdateManagerInterface::CURRENT;
    $security_update = in_array($projects['drupal']['status'], [
      UpdateManagerInterface::NOT_SECURE,
      UpdateManagerInterface::REVOKED,
    ], TRUE);
    $recommended_release = isset($projects['drupal']['releases'][$projects['drupal']['recommended']]) ? $projects['drupal']['releases'][$projects['drupal']['recommended']] : NULL;
    $existing_minor_version = explode('.', \Drupal::VERSION, -1);
    $recommended_minor_version = explode('.', $recommended_release['version'], -1);
    $major_upgrade = $existing_minor_version !== $recommended_minor_version;
    if ($major_upgrade) {
      foreach (range(1, 30) as $point_version) {
        $potential_version = implode('.', array_merge($existing_minor_version, (array) $point_version));
        if (isset($available['drupal']['releases'][$potential_version])) {
          $recommended_release = $available['drupal']['releases'][$potential_version];
        }
        else {
          break;
        }
      }
    }

    // Don't automatically update major version bumps or from/to same version.
    if ($not_recommended_version && $projects['drupal']['existing_version'] !== $recommended_release['version']) {
      if ($config
        ->get('enable_cron_security_updates')) {
        if ($security_update) {
          $metadata = new UpdateMetadata('drupal', 'core', \Drupal::VERSION, $recommended_release['version']);

          /** @var \Drupal\automatic_updates\Services\UpdateInterface $updater */
          $updater = \Drupal::service('automatic_updates.update');
          $updater
            ->update($metadata);
        }
      }
      else {
        $metadata = new UpdateMetadata('drupal', 'core', \Drupal::VERSION, $recommended_release['version']);

        /** @var \Drupal\automatic_updates\Services\UpdateInterface $updater */
        $updater = \Drupal::service('automatic_updates.update');
        $updater
          ->update($metadata);
      }
    }
  }
  $state
    ->set('automatic_updates.cron_last_check', \Drupal::time()
    ->getCurrentTime());
}