You are here

function ultimate_cron_cronapi in Ultimate Cron 7

Same name and namespace in other branches
  1. 8 ultimate_cron.module \ultimate_cron_cronapi()
  2. 6 ultimate_cron.module \ultimate_cron_cronapi()
  3. 7.2 ultimate_cron.cron.inc \ultimate_cron_cronapi()

Implements hook_cronapi().

File

./ultimate_cron.module, line 394
@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_cronapi($op, $job = NULL, $hook = NULL) {

  // Grab the defined cron queues.
  static $queues = NULL;
  if (!isset($queues)) {
    $queues = module_invoke_all('cron_queue_info');
    drupal_alter('cron_queue_info', $queues);
  }
  switch ($op) {
    case 'list':
      $jobs['ultimate_cron_cleanup_log'] = t('Cleanup log entries');
      foreach ($queues as $queue_name => $info) {
        $jobs['ultimate_cron_queue_' . $queue_name] = t('Queue: %name', array(
          '%name' => $queue_name,
        ));
      }
      return $jobs;
    case 'rule':
      $queue_name = preg_replace('/^ultimate_cron_queue_/', '', $job);
      if ($queue_name === $job) {
        return;
      }
      return '* * * * *';
    case 'execute':
      $queue_name = preg_replace('/^ultimate_cron_queue_/', '', $job);
      if ($queue_name === $job) {
        return;
      }
      $polling_latency = variable_get('ultimate_cron_queue_polling_latency', ULTIMATE_CRON_QUEUE_POLLING_LATENCY);
      $info = $queues[$queue_name];
      $function = $info['worker callback'];
      $end = time() + (isset($info['time']) ? $info['time'] : 15);
      $queue = DrupalQueue::get($queue_name);
      $items = 0;
      $settings = ultimate_cron_get_settings("ultimate_cron_queue_{$queue_name}");
      $polling_throttle = isset($settings['queue_polling_throttle']) ? $settings['queue_polling_throttle'] : 0;
      while (time() < $end) {
        $lease_time = isset($hook['settings']['queue_lease_time']) ? $hook['settings']['queue_lease_time'] : variable_get('ultimate_cron_queue_lease_time', ULTIMATE_CRON_QUEUE_LEASE_TIME);
        $item = $queue
          ->claimItem($lease_time);
        if (!$item) {
          if (is_numeric($polling_latency)) {
            usleep($polling_latency * 1000);
            continue;
          }
          else {
            break;
          }
        }
        try {
          if (is_numeric($polling_throttle)) {
            if ($items == 0) {

              // Move the boundary if using a throttle, to avoid waiting for nothing
              $end -= $polling_throttle / 1000;
            }
            else {

              // Sleep before retrieving
              usleep($polling_throttle * 1000);
            }
          }
          $function($item->data);
          $queue
            ->deleteItem($item);
          $items++;
        } catch (Exception $e) {

          // Just continue ...
          watchdog('ultimate_cron', "Queue item %item_id failed with message %message", array(
            '%item_id' => $item->item_id,
            '%message' => $e
              ->getMessage(),
          ), WATCHDOG_ERROR);
        }
      }
      drupal_set_message(t('Processed @items items', array(
        '@items' => $items,
      )));
      if (is_numeric($polling_latency)) {
        $settings = ultimate_cron_get_settings($job);
        $hook['settings'] = $settings + $hook['settings'];
        if (ultimate_cron_hook_should_run($hook)) {
          background_process_keepalive();
        }
      }
      return;
  }
}