You are here

function hosting_get_queues in Hosting 7.4

Same name and namespace in other branches
  1. 5 hosting.queues.inc \hosting_get_queues()
  2. 6.2 hosting.queues.inc \hosting_get_queues()
  3. 7.3 hosting.queues.inc \hosting_get_queues()

Retrieve a list of queues that need to be dispatched

Generate a list of queues, and the frequency / amount of items that need to be processed for each of them.

Related topics

12 calls to hosting_get_queues()
drush_hosting_dispatch in ./dispatch.hosting.inc
Main queue processing drush command for hostmaster.
hosting_drush_command in ./hosting.drush.inc
Implements hook_drush_command().
hosting_form_submit_messages in ./hosting.module
Submit handler for hosting node forms: Display helpful messages.
hosting_queues in ./hosting.module
List queues or tasks in a queue if a key is provided.
hosting_queues_configure in ./hosting.module
Form to configure the frequency of queue execution.

... See full list

1 string reference to 'hosting_get_queues'
hosting_drush_command in ./hosting.drush.inc
Implements hook_drush_command().

File

./hosting.queues.inc, line 56
This file defines an API for defining new queues.

Code

function hosting_get_queues($refresh = FALSE) {
  static $cache = NULL;
  if (is_null($cache) || $refresh) {
    $cache = array();
    $defaults = array(
      'type' => HOSTING_QUEUE_TYPE_SERIAL,
      'max_threads' => 6,
      'threshold' => '100',
      'min_threads' => 1,
      'timeout' => strtotime("10 minutes", 0),
      'frequency' => strtotime("5 minutes", 0),
      'items' => 5,
      'enabled' => TRUE,
      'singular' => t('item'),
      'plural' => t('items'),
    );
    $queues = module_invoke_all('hosting_queues');
    if (!is_array($queues)) {
      $queues = array();
    }
    drupal_alter('hosting_queues', $queues);
    foreach ($queues as $key => $queue) {
      $queue = array_merge($defaults, $queue);

      // Configurable settings.
      $configured = array(
        'frequency' => variable_get('hosting_queue_' . $key . '_frequency', $queue['frequency']),
        'items' => variable_get('hosting_queue_' . $key . '_items', $queue['items']),
        'enabled' => variable_get('hosting_queue_' . $key . '_enabled', $queue['enabled']),
        'last_run' => variable_get('hosting_queue_' . $key . '_last_run', FALSE),
        'interval' => variable_get('hosting_queue_' . $key . '_interval', FALSE),
      );
      $queue = array_merge($queue, $configured);
      if ($queue['type'] == HOSTING_QUEUE_TYPE_BATCH) {
        $threads = $queue['total_items'] / $queue['threshold'];
        if ($threads <= $queue['min_threads']) {
          $threads = $queue['min_threads'];
        }
        elseif ($threads > $queue['max_threads']) {
          $threads = $queue['max_threads'];
        }
        $queue['calc_threads'] = $threads;
        $queue['calc_frequency'] = ceil($queue['frequency'] / $threads);
        $queue['calc_items'] = ceil($queue['total_items'] / $threads);
      }
      elseif ($queue['type'] == HOSTING_QUEUE_TYPE_SPREAD) {

        // If there are 0 items in the queue avoid a division by 0.
        if ($queue['total_items'] > 0) {

          // Process the queue as often as is needed to get through all the
          // items once per frequency, but at most once a minute since the
          // default dispatcher runs that quickly.
          $queue['calc_frequency'] = max(60, $queue['frequency'] / $queue['total_items']);

          // Compute the number of items to process per invocation. Usually this is will be 1.
          $queue['calc_items'] = ceil($queue['calc_frequency'] / $queue['frequency'] * $queue['total_items']);
        }
        else {

          // There are 0 items in the queue, process 0 tasks once a day anyway.
          $queue['calc_frequency'] = 86400;
          $queue['calc_items'] = 0;
        }

        // Pretend like we do not have any previously run items still processing.
        $queue['running_items'] = 0;
      }
      else {
        $queue['calc_frequency'] = $queue['frequency'];
        $queue['calc_items'] = $queue['items'];
      }
      $queue['last'] = variable_get('hosting_queue_' . $key . '_last_run', 0);
      $queues[$key] = $queue;
    }

    // Allow altering the processed queue data.
    drupal_alter('hosting_processed_queues', $queues);
    $cache = $queues;
  }
  return $cache;
}