hosting_cron.module in Hosting 7.4
Same filename and directory in other branches
Allow the hosting system to cron all the installed sites on a schedule.
File
cron/hosting_cron.moduleView source
<?php
/**
* @file
* Allow the hosting system to cron all the installed sites on a schedule.
*/
/**
* Implements hook_hosting_queues().
*
* @todo: In Hosting 4.x change the type to HOSTING_QUEUE_TYPE_SPREAD.
*/
function hosting_cron_hosting_queues() {
$items['cron'] = array(
'type' => HOSTING_QUEUE_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;
}
/**
* Implements 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 = :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++;
}
}
/**
* Implements hook_node_view().
*/
function hosting_cron_node_view($node, $view_mode, $langcode) {
if ($node->type == 'site') {
if ($view_mode != 'teaser') {
// @todo : turn it into x minutes ago
$node->content['info']['last_cron'] = array(
'#type' => 'item',
'#title' => t('Cron run'),
'#weight' => 20,
'#markup' => hosting_format_interval($node->last_cron),
);
}
}
}
Functions
Name | Description |
---|---|
hosting_cron_hosting_queues | Implements hook_hosting_queues(). |
hosting_cron_node_view | Implements hook_node_view(). |
hosting_cron_queue | Implements hosting_QUEUE_TYPE_queue(). |