You are here

function hosting_cron_queue in Hosting 7.3

Same name and namespace in other branches
  1. 5 cron/hosting_cron.module \hosting_cron_queue()
  2. 6.2 cron/hosting_cron.module \hosting_cron_queue()
  3. 7.4 cron/hosting_cron.module \hosting_cron_queue()

Implements hosting_QUEUE_TYPE_queue().

File

cron/hosting_cron.module, line 31
Allow the hosting system to cron all the installed sites on a schedule.

Code

function hosting_cron_queue($count) {
  $result = db_query("SELECT n.nid FROM {node} n LEFT JOIN {hosting_site} s ON n.nid=s.nid WHERE n.type = :type and s.status = :status ORDER BY s.last_cron ASC, n.nid ASC", array(
    ':type' => 'site',
    ':status' => HOSTING_SITE_ENABLED,
  ));
  $i = 0;
  while ($i < $count && ($nid = $result
    ->fetch())) {
    $site = node_load($nid->nid);
    $site_name = hosting_context_name($site->nid);
    if (variable_get('hosting_cron_use_backend', TRUE)) {
      provision_backend_invoke($site_name, "cron");
    }
    else {
      $drush_result = provision_backend_invoke($site_name, 'core-status', array(
        'drupal-version',
      ), array(
        '--pipe',
      ));
      if ($drush_result['error_status'] === DRUSH_SUCCESS) {
        $core_status = json_decode(trim($drush_result['output']), TRUE);
        if (!empty($core_status['drupal-version'])) {
          $version = explode('.', $core_status['drupal-version']);
          $major_version = array_shift($version);
        }
      }
      if (!empty($major_version) && $major_version == 8) {
        $url = _hosting_site_url($site) . '/cron';
        if (!empty($site->cron_key)) {
          $url .= '/' . rawurlencode($site->cron_key);
        }
      }
      else {

        // Optionally add the cron_key querystring key if the site has one.
        $url = _hosting_site_url($site) . '/cron.php';
        if (!empty($site->cron_key)) {
          $url .= '?cron_key=' . rawurlencode($site->cron_key);
        }
      }
      drush_log(dt("running cron on URL %url", array(
        '%url' => $url,
      )));
      $response = drupal_http_request($url);
      if (isset($response->error) && $response->error) {
        watchdog('hosting_cron', 'cron failed on site %site with error %error', array(
          '%site' => $site->title,
          '%error' => $response->error,
        ), WATCHDOG_NOTICE);
        continue;

        // don't update the timestamp
      }
    }

    // We are updating the site table here directly to avoid a possible race condition,
    // with the task queue. There exists a chance that they might both try to save the
    // same node at the same time, and then an old record from the cron queue might
    // replace the newly updated record.
    db_update('hosting_site')
      ->fields(array(
      'last_cron' => REQUEST_TIME,
    ))
      ->condition('nid', $site->nid)
      ->execute();
    $i++;
  }
}