You are here

hosting_cron.module in Hosting 6.2

Allow the hosting system to cron all the installed sites on a schedule.

File

cron/hosting_cron.module
View source
<?php

/**
 * @file
 *   Allow the hosting system to cron all the installed sites on a schedule.
 */

/**
 * Implementation of hook_hosting_queues().
 */
function hosting_cron_hosting_queues() {
  $items['cron'] = array(
    'type' => 'batch',
    'name' => t('Cron queue'),
    'description' => t('Run cron on hosted sites.'),
    'total_items' => hosting_site_count(),
    'frequency' => strtotime("1 hour", 0),
    'min_threads' => 6,
    'max_threads' => 12,
    'threshold' => 100,
    'singular' => t('site'),
    'plural' => t('sites'),
  );
  return $items;
}

/**
 * Implementation of hosting_QUEUE_TYPE_queue().
 */
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='site' and s.status = %d ORDER BY s.last_cron ASC, n.nid ASC", HOSTING_SITE_ENABLED);
  $i = 0;
  while ($i < $count && ($nid = db_fetch_object($result))) {
    $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 {

      // 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_query("UPDATE {hosting_site} SET last_cron=%d WHERE nid=%d", time(), $site->nid);
    $i++;
  }
}

/**
 * Implementation of hook_nodeapi().
 */
function hosting_cron_nodeapi(&$node, $op, $a3 = null) {
  if ($node->type == 'site') {
    switch ($op) {
      case 'view':
        if (!$a3) {

          // @todo : turn it into x minutes ago
          $node->content['info']['last_cron'] = array(
            '#type' => 'item',
            '#title' => t('Cron run'),
            '#weight' => 20,
            '#value' => hosting_format_interval($node->last_cron),
          );
        }
        break;
    }
  }
}

/**
 * Get a number of sites that need to have cron executed.
 *
 * @param $limit
 *   Limit to a maximum of this number of sites.
 * @return
 *   An array of site nodes that need to have cron executed.
 *
 * @deprecated function unused
 */
function hosting_cron_get_sites($limit = 5) {
  $result = db_query("SELECT n.nid FROM {node} n LEFT JOIN {hosting_site} s ON n.nid=s.nid WHERE n.type='site' and s.status = %d ORDER BY s.last_cron ASC, n.nid ASC limit %d", HOSTING_SITE_ENABLED, $limit);
  while ($nid = db_fetch_object($result)) {
    $sites[$nid->nid] = node_load($nid->nid);
  }
  return $sites;
}

Functions

Namesort descending Description
hosting_cron_get_sites Deprecated Get a number of sites that need to have cron executed.
hosting_cron_hosting_queues Implementation of hook_hosting_queues().
hosting_cron_nodeapi Implementation of hook_nodeapi().
hosting_cron_queue Implementation of hosting_QUEUE_TYPE_queue().