You are here

public function UltimateCronQueueSettings::throttle in Ultimate Cron 7.2

Throttle queues.

Enables or disables queue threads depending on remaining items in queue.

1 call to UltimateCronQueueSettings::throttle()
UltimateCronQueueSettings::cron_pre_schedule in plugins/ultimate_cron/settings/queue.class.php
Implements hook_cron_alter().

File

plugins/ultimate_cron/settings/queue.class.php, line 389
Queue settings for Ultimate Cron.

Class

UltimateCronQueueSettings
Queue settings plugin class.

Code

public function throttle($job) {
  if (!empty($job->hook['settings']['queue']['master'])) {

    // We always base the threads on the master.
    $master_job = _ultimate_cron_job_load($job->hook['settings']['queue']['master']);
    $settings = $master_job
      ->getSettings('settings');
  }
  else {
    return;
  }
  if ($settings['queue']['throttle']) {
    $queue = DrupalQueue::get($settings['queue']['name']);
    $items = $queue
      ->numberOfItems();
    $thread = $job->hook['settings']['queue']['thread'];
    $name = $master_job->name . '_' . $thread;
    $status = empty($master_job->disabled) && $items >= ($thread - 1) * $settings['queue']['threshold'];
    $new_status = !$status ? TRUE : FALSE;
    $old_status = ultimate_cron_job_get_status($name) ? TRUE : FALSE;
    if ($old_status !== $new_status) {
      $log_entry = $job
        ->startLog(uniqid($job->name, TRUE), 'throttling', ULTIMATE_CRON_LOG_TYPE_ADMIN);
      $log_entry
        ->log($job->name, 'Job @status by queue throttling (items:@items, boundary:@boundary, threshold:@threshold)', array(
        '@status' => $new_status ? t('disabled') : t('enabled'),
        '@items' => $items,
        '@boundary' => ($thread - 1) * $settings['queue']['threshold'],
        '@threshold' => $settings['queue']['threshold'],
      ), WATCHDOG_DEBUG);
      $log_entry
        ->finish();
      $job->dont_log = TRUE;
      $job
        ->setStatus($new_status);
    }
  }
}