You are here

poormanscron.module in Poormanscron 5

Same filename and directory in other branches
  1. 5.2 poormanscron.module
  2. 6.2 poormanscron.module
  3. 6 poormanscron.module

A module which runs Drupal cron jobs without the cron application.

File

poormanscron.module
View source
<?php

/**
 * @file
 * A module which runs Drupal cron jobs without the cron application.
 */

/**
 * Implementation of hook_help().
 */
function poormanscron_help($section) {
  switch ($section) {
    case 'admin/help#poormanscron':
      return '<p>' . t('The Poormanscron module runs cron jobs without the need of the cron application.') . '</p>';
    case 'admin/settings/poormanscron':
      return '<p>' . t('The settings provided here allow you to administer Poormancron.') . '</p>';
  }
}

/**
 * Implementation of hook_exit().
 *
 * Checks if poormanscron needs to be run. If this is the case, it invokes
 * the cron hooks of all enabled modules, which are executed after
 * all HTML is returned to the browser. So the user who kicks off the cron
 * jobs should not notice any delay.
 */
function poormanscron_exit() {

  // Calculate when the next poormanscron run is due.
  $lastrun = variable_get('poormanscron_lastrun', 0);
  $nextrun = $lastrun + 60 * variable_get('poormanscron_interval', 60);

  // If the configured time has passed, start the next poormanscron run.
  if (time() > $nextrun) {

    // If this cron run fails to complete, wait a few minutes before retrying.
    variable_set('poormanscron_lastrun', $lastrun + 60 * variable_get('poormanscron_retry_interval', 10));

    // Get the current Drupal messages. Use drupal_set_message() so that
    // the messages aren't deleted in case the cron run fails and
    // we don't get a chance to restore them below.
    $saved_messages = drupal_set_message();

    // Invoke the cron hooks of all enabled modules.
    if (drupal_cron_run()) {
      $message = 'Cron run completed (via poormanscron).';
    }
    else {
      $message = 'Cron run unsuccessful (via poormanscron).';
    }

    // Write a message to the logs if the user wants us to do so.
    if (variable_get('poormanscron_log_cron_runs', 1) == 1) {
      watchdog('cron', $message);
    }
    $t = time();

    // Update the time of the last poormanscron run (this one).
    variable_set('poormanscron_lastrun', $t);

    // Delete any messages added during the cron run (and existing prior
    // messages).
    drupal_get_messages();

    // Restore any prior messages.
    if (isset($saved_messages)) {
      foreach ($saved_messages as $type => $types_messages) {
        foreach ($types_messages as $message) {
          drupal_set_message($message, $type);
        }
      }
    }
  }
}

/**
 * Implmentation of hook_menu().
 */
function poormanscron_menu($may_cache) {
  if ($may_cache) {
    $items[] = array(
      'path' => 'admin/settings/poormanscron',
      'title' => t('Poormanscron'),
      'description' => t('A module which runs Drupal cron jobs without the cron application.'),
      'callback' => 'drupal_get_form',
      'callback arguments' => 'poormanscron_admin_settings',
      'access' => user_access('administer site configuration'),
    );
  }
  return $items;
}

/**
 * Administration page for Poormanscron.
 */
function poormanscron_admin_settings() {
  $form['time_intervals'] = array(
    '#type' => 'fieldset',
    '#title' => t('Time intervals'),
  );
  $form['time_intervals']['poormanscron_interval'] = array(
    '#type' => 'textfield',
    '#title' => t('Cron runs interval'),
    '#default_value' => variable_get('poormanscron_interval', 60),
    '#size' => 5,
    '#maxlength' => 10,
    '#description' => t('Minimum number of minutes between cron runs. Cron will actually execute during the first page request after the interval has elapsed.'),
  );
  $form['time_intervals']['poormanscron_retry_interval'] = array(
    '#type' => 'textfield',
    '#title' => t('Retry interval'),
    '#default_value' => variable_get('poormanscron_retry_interval', 10),
    '#size' => 5,
    '#maxlength' => 10,
    '#description' => t('The number of minutes to wait after a cron run error before retrying.'),
  );
  $form['logging'] = array(
    '#type' => 'fieldset',
    '#title' => t('Logging'),
  );
  $form['logging']['poormanscron_log_cron_runs'] = array(
    '#type' => 'select',
    '#title' => t('Log successful cron runs'),
    '#default_value' => variable_get('poormanscron_log_cron_runs', 1),
    '#options' => array(
      '1' => t('Yes'),
      '0' => t('No'),
    ),
    '#description' => t('If you want to log successful cron runs to the Drupal watchdog, say Yes here. If those messages annoy you, disable them by selecting No.'),
  );
  return system_settings_form($form);
}

Functions

Namesort descending Description
poormanscron_admin_settings Administration page for Poormanscron.
poormanscron_exit Implementation of hook_exit().
poormanscron_help Implementation of hook_help().
poormanscron_menu Implmentation of hook_menu().