Commit d2f749e4 authored by Mathew Winstone's avatar Mathew Winstone
Browse files

fix(updates): improve reliability of update calls

Ensure dropfort update runs later so update_status has a chance to run
parent 1ec2bad8
Loading
Loading
Loading
Loading
+27 −1
Original line number Diff line number Diff line
@@ -7,6 +7,7 @@

use GuzzleHttp\Exception\RequestException;
use Drupal\Component\Utility\UrlHelper;
use Drupal\update\UpdateFetcherInterface;

/**
 * Implements hook_cron().
@@ -19,6 +20,19 @@ function dropfort_update_cron() {
  }
}

/**
 * Implements hook_module_implements_alter().
 */
function dropfort_update_module_implements_alter(&$implementations, $hook) {
  // Lower Dropfort Update's weight so it runs after the update module.
  // Reduces the chance of not getting update data.
  if ($hook == 'cron' && array_key_exists('dropfort_update', $implementations)) {
    $cron_config = $implementations['dropfort_update'];
    unset($implementations['dropfort_update']);
    $implementations['dropfort_update'] = $cron_config;
  }
}

/**
 * Send updates and status data to Dropfort.
 *
@@ -79,10 +93,22 @@ function dropfort_update_send_status(array $options = NULL) {
    return;
  }

  // Sometimes Drupal hasn't fetched the module info recently.
  // If that's the case, have Dropfort Update try again sooner.
  $last_status = time();
  foreach ($data['update_status'] as $item) {
    if (in_array($item['status'], [UpdateFetcherInterface::NOT_FETCHED, UpdateFetcherInterface::FETCH_PENDING])) {
      // Set the last status to an hour ago since we didn't get a full
      // status run. We'll take the existing data but we treat this
      // as if it hadn't run so it gets run again next cron.
      $last_status = time() - 3600;
    }
  }

  try {
    $client = \Drupal::httpClient();
    $request = $client->request('POST', $status_url, $options);
    \Drupal::state()->set('dropfort_update.last_status', time());
    \Drupal::state()->set('dropfort_update.last_status', $last_status);

    return $request;
  }