You are here

function ultimate_cron_background_process_shutdown in Ultimate Cron 8

Same name and namespace in other branches
  1. 6 ultimate_cron.module \ultimate_cron_background_process_shutdown()
  2. 7.2 ultimate_cron.background_process.inc \ultimate_cron_background_process_shutdown()
  3. 7 ultimate_cron.module \ultimate_cron_background_process_shutdown()

Implements hook_background_process_shutdown().

Shutdown handler for cronjobs.

1 call to ultimate_cron_background_process_shutdown()
_ultimate_cron_run_hook in ./ultimate_cron.module
This is the function that is launched into a background process. It runs the cron job and does housekeeping, pre/post execute hooks, etc.

File

./ultimate_cron.module, line 574
@todo Add filter on overview page. @todo Add log view (with graph). @todo Make proper markup for overview page. @todo Refactor drush stuff, too many intimate relations with Background Process @todo Refactor Cron % offset stuff. Too mixed up and…

Code

function ultimate_cron_background_process_shutdown($process, $shutdown_msg = NULL) {
  $args = func_get_args();
  $record = drupal_static('ultimate_cron_record', FALSE);
  $handle_prefix = variable_get('ultimate_cron_handle_prefix', ULTIMATE_CRON_HANDLE_PREFIX);
  $name = preg_replace('/^' . $handle_prefix . '/', '', $process->handle);
  if (!empty($name) && $name != $process->handle) {
    static $has_run = array();
    if (!empty($has_run[$name])) {
      return;
    }
    $has_run[$name] = TRUE;

    // Record end time
    $end = microtime(TRUE);
    if ($record) {

      // Get drupal messages
      $messages = drupal_get_messages(NULL, TRUE);
      $messages['status'] = empty($messages['status']) ? array() : $messages['status'];
      $messages['warning'] = empty($messages['warning']) ? array() : $messages['warning'];
      $messages['error'] = empty($messages['error']) ? array() : $messages['error'];
      foreach ($messages['status'] as $message) {
        ultimate_cron_record_log($message);
      }
      foreach ($messages['warning'] as $message) {
        ultimate_cron_record_log($message, FALSE, WATCHDOG_WARNING);
      }
      foreach ($messages['error'] as $message) {
        ultimate_cron_record_log($message, FALSE, WATCHDOG_ERROR);
      }

      // Get error messages
      $error = error_get_last();
      if ($error) {
        $message = $error['message'] . ' (line ' . $error['line'] . ' of ' . $error['file'] . ').' . "\n";
        $severity = WATCHDOG_INFO;
        if ($error['type'] && (E_NOTICE || E_USER_NOTICE || E_USER_WARNING)) {
          $severity = WATCHDOG_NOTICE;
        }
        if ($error['type'] && (E_WARNING || E_CORE_WARNING || E_USER_WARNING)) {
          $severity = WATCHDOG_WARNING;
        }
        if ($error['type'] && (E_ERROR || E_CORE_ERROR || E_USER_ERROR || E_RECOVERABLE_ERROR)) {
          $severity = WATCHDOG_ERROR;
        }
        ultimate_cron_record_log($message, FALSE, $severity);
      }
    }
    if ($shutdown_msg) {
      ultimate_cron_record_log($shutdown_msg, FALSE, WATCHDOG_ERROR);
    }
    $log = drupal_static('ultimate_cron_record_log', array(
      'msg' => '',
      'severity' => -1,
    ));
    $severity = $log['severity'];
    $msg = $log['msg'];
    $result = $severity < 0 || $severity >= WATCHDOG_INFO ? 1 : 0;

    // log results here ...
    $object = (object) array(
      'name' => $name,
      'start_stamp' => $process->start,
      'end_stamp' => $end,
      'service_host' => $process->service_host,
      'exec_status' => $result,
      'severity' => $severity,
      'msg' => trim($msg),
    );
    $old_db = db_set_active('background_process');
    drupal_write_record('ultimate_cron_log', $object);
    db_set_active($old_db);
    $object->formatted['start_stamp'] = format_date((int) $object->start_stamp, 'custom', 'Y-m-d H:i:s');
    $object->formatted['end_stamp'] = format_date((int) $object->end_stamp, 'custom', 'Y-m-d H:i:s');
    $object->formatted['duration'] = gmdate('H:i:s', (int) ($object->end_stamp - $object->start_stamp));
    $object->formatted['severity'] = $object->severity < 0 ? 'success' : ($object->severity >= WATCHDOG_NOTICE ? 'info' : ($object->severity >= WATCHDOG_WARNING ? 'warning' : 'error'));
    $object->formatted['executeURL'] = url('admin/ultimate-cron/service/start/' . $object->name, array(
      'query' => array(
        'destination' => 'admin/config/system/cron',
      ),
    ));
    $object->formatted['msg'] = strip_tags(html_entity_decode($object->msg, ENT_QUOTES));
    if (module_exists('nodejs')) {
      $message = (object) array(
        'channel' => 'ultimate_cron',
        'data' => (object) array(
          'action' => 'log',
          'log' => $object,
        ),
        'callback' => 'nodejsUltimateCron',
      );
      nodejs_send_content_channel_message($message);
    }
  }
}