You are here

function cron_debug_form_submit in Cron Debug 7

Handle submission of the form.

Will start each cron hook in sequence, log the results and return to the main form.

File

./cron_debug.module, line 192
Cron debugging for administrators.

Code

function cron_debug_form_submit($form, &$form_state) {

  // Register start in watchdog
  watchdog('cron debug', 'Starting Cron Debug.');

  // Hoist a flag that we're running.
  variable_set('cron_debug_flag', CRON_DEBUG_RUNNING);

  // Start main timer
  timer_start('cron');

  // Run through each hook as selected in form
  foreach ($form_state['values']['cron'] as $key => $module) {
    if ($module) {

      // The module was selected
      // Change the flag to the current module name
      variable_set('cron_debug_flag', $module);

      // Register start of each module. Enables us to see stuck hooks in the log
      watchdog('cron debug', 'Starting cron for @module module.', array(
        '@module' => $module,
      ));

      // Make the name of the hook, start timer and call the cron hook
      $function = $module . '_cron';
      timer_start($function);
      $function();

      // Stop the individual timer, get the time for this module in seconds
      $timer = timer_stop($function);
      $total = round($timer['time'] / 1000, 3);

      // Create messages, add one to results and log one
      $cron_debug_log_message = t('Finished successfully in @secs seconds.', array(
        '@secs' => $total,
      ));
      $cron_debug_log[$module] = $cron_debug_log_message;
      watchdog('cron debug', 'cron_@module() finished successfully in @secs seconds.', array(
        '@module' => $module,
        '@secs' => $total,
      ));
    }
    else {

      // Add FALSE as return message
      $cron_debug_log[$key] = FALSE;
    }
  }

  // Stop overall timer and get time
  $cron_timer = timer_stop('cron');
  $total = round($cron_timer['time'] / 1000, 3);

  // Create message, add and log it
  $cron_debug_log_message = t('Total time was @secs seconds.', array(
    '@secs' => $total,
  ));
  $cron_debug_log['done'] = '<p>' . $cron_debug_log_message . '<p />';
  watchdog('cron debug', $cron_debug_log_message);

  // Save results in $form_state and set rebuild
  $form_state['storage']['results'] = $cron_debug_log;
  $form_state['rebuild'] = TRUE;

  // Get rid of the flag
  variable_del('cron_debug_flag');

  // Set messages
  watchdog('cron debug', 'Ended Cron Debug run succesfully. Returning to Drupal');
  drupal_set_message(t('Ended Cron Debug run succesfully. See the results below.'));

  // Function will return to the form, which will rebuild
}