You are here

public function QueueSettings::throttle in Ultimate Cron 8.2

Throttle queues.

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

1 call to QueueSettings::throttle()
QueueSettings::cron_pre_schedule in src/QueueSettings.php
Implements hook_cron_alter().

File

src/QueueSettings.php, line 344

Class

QueueSettings
Queue settings plugin class.

Namespace

Drupal\ultimate_cron

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
        ->id(), TRUE), 'throttling', ULTIMATE_CRON_LOG_TYPE_ADMIN);
      $log_entry
        ->log('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'],
      ), RfcLogLevel::INFO);
      $log_entry
        ->finish();
      $job->dont_log = TRUE;
      ultimate_cron_job_set_status($job, $new_status);
      $job->disabled = $new_status;
    }
  }
}