You are here

class UltimateCronCrontabScheduler in Ultimate Cron 7.2

Crontab scheduler.

Hierarchy

Expanded class hierarchy of UltimateCronCrontabScheduler

2 string references to 'UltimateCronCrontabScheduler'
crontab.inc in plugins/ultimate_cron/scheduler/crontab.inc
simple.inc in plugins/ultimate_cron/scheduler/simple.inc

File

plugins/ultimate_cron/scheduler/crontab.class.php, line 10
Crontab cron job scheduler for Ultimate Cron.

View source
class UltimateCronCrontabScheduler extends UltimateCronScheduler {

  /**
   * Default settings.
   */
  public function defaultSettings() {
    return array(
      'rules' => array(
        '*/10+@ * * * *',
      ),
      'catch_up' => '86400',
    );
  }

  /**
   * Label for schedule.
   */
  public function formatLabel($job) {
    $settings = $job
      ->getSettings($this->type);
    return implode("\n", $settings['rules']);
  }

  /**
   * Label for schedule.
   */
  public function formatLabelVerbose($job) {
    $settings = $job
      ->getSettings($this->type);
    $job->log_entry = isset($job->log_entry) ? $job->log_entry : $job
      ->loadLatestLogEntry();
    $parsed = '';
    $next_schedule = NULL;
    $time = REQUEST_TIME;
    $skew = $this
      ->getSkew($job);
    foreach ($settings['rules'] as $rule) {
      $cron = CronRule::factory($rule, $time, $skew);
      $parsed .= $cron
        ->parseRule() . "\n";
      $result = $cron
        ->getNextSchedule();
      $next_schedule = is_null($next_schedule) || $next_schedule > $result ? $result : $next_schedule;
      $result = $cron
        ->getLastSchedule();

      // If job didn't run at its last schedule, check if the catch up time
      // will triger it, and adjust $next_schedule accordingly.
      if ($job->log_entry->start_time < $result && $time < $result + $settings['catch_up']) {
        $result = floor($time / 60) * 60 + 60;
        $next_schedule = $next_schedule > $result ? $result : $next_schedule;
      }
    }
    $parsed .= t('Next scheduled run at @datetime', array(
      '@datetime' => format_date($next_schedule, 'custom', 'Y-m-d H:i:s'),
    ));
    return $parsed;
  }

  /**
   * Settings form for the crontab scheduler.
   */
  public function settingsForm(&$form, &$form_state, $job = NULL) {
    $elements =& $form['settings'][$this->type][$this->name];
    $values =& $form_state['values']['settings'][$this->type][$this->name];
    $rules = is_array($values['rules']) ? implode(';', $values['rules']) : '';
    $elements['rules'] = array(
      '#title' => t("Rules"),
      '#type' => 'textfield',
      '#default_value' => $rules,
      '#description' => t('Semi-colon separated list of crontab rules.'),
      '#fallback' => TRUE,
      '#required' => TRUE,
      '#element_validate' => array(
        'ultimate_cron_plugin_crontab_element_validate_rule',
      ),
    );
    $elements['rules_help'] = array(
      '#type' => 'fieldset',
      '#title' => t('Rules help'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
    );
    $elements['rules_help']['info'] = array(
      '#markup' => file_get_contents(drupal_get_path('module', 'ultimate_cron') . '/help/rules.html'),
    );
    $elements['catch_up'] = array(
      '#title' => t("Catch up"),
      '#type' => 'textfield',
      '#default_value' => $values['catch_up'],
      '#description' => t("Don't run job after X seconds of rule."),
      '#fallback' => TRUE,
      '#required' => TRUE,
    );
  }

  /**
   * Submit handler.
   */
  public function settingsFormSubmit(&$form, &$form_state, $job = NULL) {
    $values =& $form_state['values']['settings'][$this->type][$this->name];
    if (!empty($values['rules'])) {
      $rules = explode(';', $values['rules']);
      $values['rules'] = array_map('trim', $rules);
    }
  }

  /**
   * Schedule handler.
   */
  public function isScheduled($job) {
    $settings = $job
      ->getSettings($this->type);
    $log_entry = isset($job->log_entry) ? $job->log_entry : $job
      ->loadLatestLogEntry();
    $skew = $this
      ->getSkew($job);
    $class = get_class($this);
    return $class::shouldRun($settings['rules'], $log_entry->start_time, NULL, $settings['catch_up'], $skew) !== FALSE ? TRUE : FALSE;
  }

  /**
   * Check crontab rules against times.
   */
  public static function shouldRun($rules, $job_last_ran, $time = NULL, $catch_up = 0, $skew = 0) {
    $time = is_null($time) ? time() : $time;
    foreach ($rules as $rule) {
      $cron = CronRule::factory($rule, $time, $skew);
      $cron_last_ran = $cron
        ->getLastSchedule();
      if ($job_last_ran < $cron_last_ran && $cron_last_ran <= $time) {
        if ($time <= $cron_last_ran + $catch_up) {
          return $time - $job_last_ran;
        }
      }
    }
    return FALSE;
  }

  /**
   * Determine if job is behind schedule.
   */
  public function isBehind($job) {

    // Disabled jobs are not behind!
    if (!empty($job->disabled)) {
      return FALSE;
    }
    $log_entry = isset($job->log_entry) ? $job->log_entry : $job
      ->loadLatestLogEntry();

    // If job hasn't run yet, then who are we to say it's behind its schedule?
    // Check the registered time, and use that if it's available.
    $job_last_ran = $log_entry->start_time;
    if (!$job_last_ran) {
      $registered = variable_get('ultimate_cron_hooks_registered', array());
      if (empty($registered[$job->name])) {
        return FALSE;
      }
      $job_last_ran = $registered[$job->name];
    }
    $settings = $job
      ->getSettings($this->type);
    $skew = $this
      ->getSkew($job);
    $next_schedule = NULL;
    foreach ($settings['rules'] as $rule) {
      $cron = CronRule::factory($rule, $job_last_ran, $skew);
      $time = $cron
        ->getNextSchedule();
      $next_schedule = is_null($next_schedule) || $time < $next_schedule ? $time : $next_schedule;
    }
    $behind = REQUEST_TIME - $next_schedule;
    return $behind > $settings['catch_up'] ? $behind : FALSE;
  }

  /**
   * Get a "unique" skew for a job.
   */
  protected function getSkew($job) {
    return $job
      ->getUniqueID() & 0xff;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
UltimateCronCrontabScheduler::defaultSettings public function Default settings. Overrides UltimateCronPlugin::defaultSettings 1
UltimateCronCrontabScheduler::formatLabel public function Label for schedule. Overrides UltimateCronPlugin::formatLabel 1
UltimateCronCrontabScheduler::formatLabelVerbose public function Label for schedule. Overrides UltimateCronPlugin::formatLabelVerbose
UltimateCronCrontabScheduler::getSkew protected function Get a "unique" skew for a job.
UltimateCronCrontabScheduler::isBehind public function Determine if job is behind schedule. Overrides UltimateCronScheduler::isBehind
UltimateCronCrontabScheduler::isScheduled public function Schedule handler. Overrides UltimateCronScheduler::isScheduled
UltimateCronCrontabScheduler::settingsForm public function Settings form for the crontab scheduler. Overrides UltimateCronPlugin::settingsForm 1
UltimateCronCrontabScheduler::settingsFormSubmit public function Submit handler. Overrides UltimateCronPlugin::settingsFormSubmit
UltimateCronCrontabScheduler::shouldRun public static function Check crontab rules against times.
UltimateCronPlugin::$description public property
UltimateCronPlugin::$globalOptions public static property
UltimateCronPlugin::$instances public static property
UltimateCronPlugin::$multiple public static property 1
UltimateCronPlugin::$name public property
UltimateCronPlugin::$plugin public property
UltimateCronPlugin::$settings public property
UltimateCronPlugin::$title public property
UltimateCronPlugin::$weight public property 1
UltimateCronPlugin::build_operations_alter public function Allow plugins to alter the allowed operations for a job. 2
UltimateCronPlugin::cleanForm public function Clean form of empty fallback values.
UltimateCronPlugin::cronapi public function A hook_cronapi() for plugins. 1
UltimateCronPlugin::cron_alter public function A hook_cron_alter() for plugins. 3
UltimateCronPlugin::cron_post_invoke public function A hook_cron_post_invoke() for plugins.
UltimateCronPlugin::cron_post_launch public function A hook_cron_post_launch() for plugins.
UltimateCronPlugin::cron_post_run public function A hook_cron_post_run() for plugins.
UltimateCronPlugin::cron_post_schedule public function A hook_cron_post_schedule() for plugins. 1
UltimateCronPlugin::cron_pre_invoke public function A hook_cron_pre_invoke() for plugins.
UltimateCronPlugin::cron_pre_launch public function A hook_cron_pre_launch() for plugins.
UltimateCronPlugin::cron_pre_run public function A hook_cron_pre_run() for plugins.
UltimateCronPlugin::cron_pre_schedule public function A hook_cron_pre_schedule() for plugins. 2
UltimateCronPlugin::defaultSettingsForm public static function Default settings form. 1
UltimateCronPlugin::drupal_array_remove_nested_value public function Modified version drupal_array_get_nested_value().
UltimateCronPlugin::factory public static function Singleton factoryLogEntry.
UltimateCronPlugin::fallbackalize public function Process fallback form parameters.
UltimateCronPlugin::getDefaultSettings public function Get default settings. 1
UltimateCronPlugin::getGlobalOption public static function Get global plugin option.
UltimateCronPlugin::getGlobalOptions public static function Get all global plugin options.
UltimateCronPlugin::hook_cron_alter final public static function Invoke hook_cron_alter() on plugins.
UltimateCronPlugin::hook_cron_post_invoke final public static function Invoke hook_cron_post_invoke() on plugins.
UltimateCronPlugin::hook_cron_post_launch final public static function Invoke hook_cron_post_launch() on plugins.
UltimateCronPlugin::hook_cron_post_run final public static function Invoke hook_cron_post_run() on plugins.
UltimateCronPlugin::hook_cron_post_schedule final public static function Invoke hook_cron_post_schedule() on plugins.
UltimateCronPlugin::hook_cron_pre_invoke final public static function Invoke hook_cron_pre_invoke() on plugins.
UltimateCronPlugin::hook_cron_pre_launch final public static function Invoke hook_cron_pre_launch() on plugins.
UltimateCronPlugin::hook_cron_pre_run final public static function Invoke hook_cron_pre_run() on plugins.
UltimateCronPlugin::hook_cron_pre_schedule final public static function Invoke hook_cron_pre_schedule() on plugins.
UltimateCronPlugin::isValid public function Default plugin valid for all jobs. 2
UltimateCronPlugin::jobSettingsForm public static function Job settings form. 1
UltimateCronPlugin::jobSettingsFormSubmit public static function Job settings form submit handler. 1
UltimateCronPlugin::jobSettingsFormValidate public static function Job settings form validate handler. 1
UltimateCronPlugin::setGlobalOption public static function Set global plugin option.
UltimateCronPlugin::setSettings public function Save settings to db.
UltimateCronPlugin::settingsFormValidate public function Settings form validate handler. 1
UltimateCronPlugin::settingsLabel public function Get label for a specific setting. 3
UltimateCronPlugin::signal public function Signal page for plugins. 2
UltimateCronPlugin::unsetGlobalOption public static function Remove a global plugin option.
UltimateCronPlugin::unsetGlobalOptions public static function Remove all global plugin options.
UltimateCronPlugin::__construct public function Constructor. 1