You are here

function ultimate_cron_view_page in Ultimate Cron 7

Same name and namespace in other branches
  1. 8 ultimate_cron.admin.inc \ultimate_cron_view_page()
  2. 6 ultimate_cron.admin.inc \ultimate_cron_view_page()

Page overviewing cron jobs.

2 string references to 'ultimate_cron_view_page'
ultimate_cron_menu in ./ultimate_cron.module
Implements hook_menu().
ultimate_cron_menu_alter in ./ultimate_cron.module
Implements hook_menu_alter().

File

./ultimate_cron.admin.inc, line 276

Code

function ultimate_cron_view_page($status = NULL) {
  require_once 'CronRule.class.php';
  drupal_add_css(drupal_get_path('module', 'ultimate_cron') . '/css/ultimate_cron.admin.css');
  drupal_add_js(drupal_get_path('module', 'ultimate_cron') . '/js/ultimate_cron.js');
  if (module_exists('nodejs')) {
    drupal_add_js(array(
      'ultimate_cron' => array(
        'processes' => new stdClass(),
        'skew' => 0,
        'handle_prefix' => variable_get('ultimate_cron_handle_prefix', ULTIMATE_CRON_HANDLE_PREFIX),
      ),
    ), 'setting');
    nodejs_send_content_channel_token('ultimate_cron');
    nodejs_send_content_channel_token('background_process');
    nodejs_send_content_channel_token('progress');
    drupal_add_js(drupal_get_path('module', 'ultimate_cron') . '/js/nodejs.ultimate_cron.js');
  }
  module_load_install('ultimate_cron');
  $requirements = ultimate_cron_requirements('runtime');
  if ($requirements['ultimate_cron']['severity'] != REQUIREMENT_OK) {
    drupal_set_message($requirements['ultimate_cron']['value'], 'error');
    drupal_set_message($requirements['ultimate_cron']['description'], 'error');
  }

  // Get hooks and their data
  $data = _ultimate_cron_preload_cron_data();
  $hooks = ultimate_cron_get_hooks();
  $modules = array();
  foreach ($hooks as $function => $hook) {
    $hook['settings'] = $data[$function]['settings'] + $hook['settings'];
    $hook['background_process'] = $data[$function]['background_process'];
    $hook['log'] = ultimate_cron_get_log($function);
    $modules[$hook['module']][$function] = $hook;
  }
  $output = '';
  $rows = array();
  $handle_prefix = variable_get('ultimate_cron_handle_prefix', ULTIMATE_CRON_HANDLE_PREFIX);
  $overview = array();
  $overview['running'] = 0;
  $overview['success'] = 0;
  $overview['info'] = 0;
  $overview['warning'] = 0;
  $overview['error'] = 0;

  // Used for JS encodeURIComponent emulation
  $revert = array(
    '%21' => '!',
    '%2A' => '*',
    '%27' => "'",
    '%28' => '(',
    '%29' => ')',
  );
  foreach ($modules as $module => $hooks) {
    foreach ($hooks as $function => $hook) {

      // Setup settings
      $conf = $hook['settings'];
      $rules = $hook['settings']['rules'];
      $cron = new CronRule();
      $parsed_rules = array();
      foreach ($rules as $rule) {
        $cron->rule = $rule;
        $cron->offset = $hook['delta'];
        $parsed_rules[] = $cron
          ->parseRule();
      }

      // Setup process
      $process = $hook['background_process'];
      $service_host = empty($process->service_host) ? t('N/A') : $process->service_host;

      // Setup log
      $log = $hook['log'];
      if (!$log) {
        $log = array(
          'severity' => -1,
          'status' => NULL,
          'start' => NULL,
          'end' => NULL,
        );
      }
      $severity_type = $log['severity'] < 0 ? 'success' : ($log['severity'] >= WATCHDOG_NOTICE ? 'info' : ($log['severity'] >= WATCHDOG_WARNING ? 'warning' : 'error'));
      $css_status = !empty($process) ? 'running' : $severity_type;
      $short_msg = $log['severity'];
      $msg = !empty($log['msg']) ? $log['msg'] : t('No errors');
      $duration = '';
      if ($process) {
        $overview['running']++;
        $log['previous_start'] = $log['start'];
        $log['previous_end'] = $log['end'];
        $log['start'] = $process->start;
        if ($process->status == BACKGROUND_PROCESS_STATUS_RUNNING) {
          $log['end'] = microtime(TRUE);
        }
        else {
          $log['end'] = NULL;
        }
        $progress = progress_get_progress($handle_prefix . $function);
        if ($progress && $progress->progress > 0) {
          $duration .= sprintf(" (%d%%)", $progress->progress * 100);
        }
      }
      $overview[$severity_type]++;
      $link_configure = '';
      if (!empty($hook['configure'])) {
        $link_configure = _ultimate_cron_l('Settings', $hook['configure']);
      }
      $link_unlock = '';
      if ($process) {
        $link_unlock = _ultimate_cron_l('Unlock', 'background-process/unlock/' . $process->handle);
      }
      $link_settings = _ultimate_cron_l('Schedule', 'admin/config/system/cron/settings/' . $function);
      $link_execute = _ultimate_cron_l('Run', 'admin/ultimate-cron/service/start/' . $function);
      $link_log = _ultimate_cron_l('Log', 'admin/reports/cron/' . $function);
      $enable = !empty($conf) && empty($conf['enabled']);
      $link_toggle = _ultimate_cron_l($enable ? 'Enable' : 'Disable', 'admin/ultimate-cron/service/' . ($enable ? 'enable' : 'disable') . '/' . $function);
      $data = array(
        array(
          'class' => $enable ? 'ultimate-cron-admin-enable' : 'ultimate-cron-admin-disable',
        ),
        array(
          'class' => 'ultimate-cron-admin-module',
        ),
        array(
          'class' => 'ultimate-cron-admin-function',
        ),
        array(
          'class' => 'ultimate-cron-admin-rules',
        ),
        array(
          'class' => 'ultimate-cron-admin-start',
        ),
        array(
          'class' => 'ultimate-cron-admin-end',
        ),
        array(
          'class' => 'ultimate-cron-admin-status ultimate-cron-admin-status-' . $css_status,
        ),
        array(
          'class' => 'ultimate-cron-admin-settings',
        ),
        array(
          'class' => 'ultimate-cron-admin-configure',
        ),
        array(
          'class' => 'ultimate-cron-admin-log',
        ),
        array(
          'class' => $process ? 'ultimate-cron-admin-unlock' : 'ultimate-cron-admin-execute',
        ),
      );
      $data[0]['data'] = $link_toggle;
      $data[0]['title'] = $enable ? t('Enable') : t('Disable');
      $data[1]['data'] = ultimate_cron_module_name($module);
      $data[2]['data'] = $hook['description'];
      $data[2]['title'] = $function;
      $data[3]['data'] = join("<br/>", $rules);
      $data[3]['title'] = join("\n", $parsed_rules);
      $data[4]['data'] = $log['start'] ? format_date((int) $log['start'], 'custom', 'Y-m-d H:i:s') : t('Never');
      $data[5]['data'] = $log['end'] ? gmdate('H:i:s', (int) ($log['end'] - $log['start'])) . $duration : ($process ? t('Starting') : t('N/A'));
      $finish = !empty($log['previous_end']) ? $log['previous_end'] : $log['end'];
      $data[5]['title'] = t('Previous run finished @ !timestamp', array(
        '!timestamp' => $finish ? format_date((int) $finish, 'custom', 'Y-m-d H:i:s') : t('N/A'),
      ));
      if (!empty($log['previous_start'])) {
        $data[4]['title'] = t('Previous run started @ !timestamp', array(
          '!timestamp' => format_date((int) $log['previous_start'], 'custom', 'Y-m-d H:i:s'),
        ));
        $data[5]['title'] .= ' - ' . t('Run time: !duration', array(
          '!duration' => gmdate('H:i:s', (int) ($log['previous_end'] - $log['previous_start'])) . $duration,
        ));
      }
      if ($process) {
        $data[6]['data'] = '<span>' . t('Running') . '</span>';
        $data[6]['title'] = t('Running on @host', array(
          '@host' => $service_host,
        ));
      }
      else {
        $data[6]['data'] = '<span>' . $short_msg . '</span>';
        $data[6]['title'] = strip_tags(html_entity_decode($msg, ENT_QUOTES));
      }
      $data[7]['data'] = $link_settings;
      $data[7]['title'] = t('Schedule');
      $data[8]['data'] = $link_configure;
      $data[8]['title'] = $link_configure ? t('Settings') : '';
      $data[9]['data'] = $link_log;
      $data[9]['title'] = t('Log');
      $data[10]['data'] = $process ? $link_unlock : $link_execute;
      $data[10]['title'] = $process ? t('Unlock') : t('Run');
      $rows[(int) $enable][] = array(
        'class' => array(
          'row-' . str_replace('%', '_', strtr(rawurlencode($function), $revert)),
        ),
        'data' => $data,
        'style' => $status && $status != 'all' && $css_status != $status ? 'display: none' : '',
      );
    }
  }
  if (!empty($rows[0])) {
    $headers = array(
      t('Enabled'),
      t('Module'),
      t('Function'),
      t('Rules'),
      t('Start'),
      t('Duration'),
      t('Status'),
      array(
        'colspan' => 3,
        'data' => '',
      ),
      l(t('Run all'), 'admin/reports/status/run-cron', array(
        'query' => drupal_get_destination(),
      )),
    );
    $output .= theme('table', array(
      'header' => $headers,
      'rows' => $rows[0],
      'attributes' => array(
        'id' => array(
          'ultimate-cron-view',
        ),
      ),
    ));
    $output .= '<div class="clear-block"></div>';
  }
  if (!empty($rows[1])) {
    $headers = array(
      t('Disabled'),
      t('Module'),
      t('Function'),
      t('Rules'),
      t('Start'),
      t('Duration'),
      t('Status'),
      array(
        'colspan' => 4,
        'data' => '',
      ),
    );
    $output .= theme('table', array(
      'header' => $headers,
      'rows' => $rows[1],
      'attributes' => array(
        'id' => array(
          'ultimate-cron-view',
        ),
      ),
    ));
    $output .= '<div class="clear-block"></div>';
  }
  if ($overview['running']) {
    drupal_set_message(format_plural($overview['running'], '@jobs job is currently running', '@jobs jobs are currently running', array(
      '@jobs' => $overview['running'],
    )));
  }
  if ($overview['warning']) {
    drupal_set_message(format_plural($overview['warning'], '@jobs job had warnings during its last run', '@jobs jobs had warnings during their last run', array(
      '@jobs' => $overview['warning'],
    )), 'warning');
  }
  if ($overview['error']) {
    drupal_set_message(format_plural($overview['error'], '@jobs job had errors during its last run', '@jobs jobs had errors during their last run', array(
      '@jobs' => $overview['error'],
    )), 'error');
  }
  return $output;
}