You are here

function _acquia_purge_queue_pop in Acquia Purge 6

Same name and namespace in other branches
  1. 7 acquia_purge.deprecated.inc \_acquia_purge_queue_pop()

Queue manager: pop X amount of items from the queue.

@warning Calling this helper commits the caller into actually processing the items popped from the queue, either by iterating the return value or by providing a processing callback that processes individual values. Not processing the result will lead into confusion and broken functionality. @returns Non-associative array of which every value record represents one resulting HTTP PURGE request. Array items are non-associative arrays itself with the path in key #0.

Parameters

string $processor_callback: The name of a PHP function or callable that gets called to process a individual item popped from the queue. The callback is given the path as argument. This parameter is optional.

2 calls to _acquia_purge_queue_pop()
acquia_purge_ajax_processor in ./acquia_purge.admin.inc
Menu callback; process a chunk of purge items via AJAX.
drush_acquia_purge_ap_process in ./acquia_purge.drush.inc
Purge all queued items from the command line.

File

./acquia_purge.module, line 679
Acquia Purge, Top-notch Varnish purging on Acquia Cloud!

Code

function _acquia_purge_queue_pop($processor_callback = NULL) {
  $items = array();

  // Determine the maximum amount of requests that we can process at once.
  $max_execution_time = (int) ini_get('max_execution_time');
  $max_requests = ACQUIA_PURGE_MAX_REQUESTS;
  if ($max_execution_time != 0) {

    // Never take more then 80% of all available execution time.
    $max_execution_time = intval(0.8 * $max_execution_time);
    $max_requests = $max_execution_time / ACQUIA_PURGE_REQUEST_TIMEOUT;
    $max_requests = ACQUIA_PURGE_PARALLEL_REQUESTS * $max_requests;
    if ($max_requests > ACQUIA_PURGE_MAX_REQUESTS) {
      $max_requests = ACQUIA_PURGE_MAX_REQUESTS;
    }
  }

  // Calculate the amount of paths to pop from the queue based on how many HTTP
  // requests we are going to generate and how much we can maximally process.
  $balancers = count(_acquia_purge_get_balancers());
  $domains = count(_acquia_purge_get_domains());
  $schemes = count(_acquia_purge_get_protocol_schemes());
  $requests = $schemes * ($domains * $balancers);
  $paths = intval($max_requests / $requests);

  // In scenarios with many balancers or many domains it might happen that even
  // one path will trigger more HTTP requests than we can handle. To prevent
  // not purging anything anymore we will attempt one but with taking risks.
  if ($paths < 1) {
    $paths = 1;
  }

  // Limit the number of paths to pop when it maxed out the set limit. Usually
  // only happens on the command line.
  if ($paths > ACQUIA_PURGE_MAX_PATHS) {
    $paths = ACQUIA_PURGE_MAX_PATHS;
  }

  // Retrieve the amount of paths that we just calculated we could handle.
  $items_to_be_removed = array();
  $itemsq = db_query("SELECT * FROM {ap_queue} LIMIT %d", $paths);
  while ($item = db_fetch_object($itemsq)) {

    // Add this item to the items list released back or being processed.
    $items[] = array(
      $item->path,
    );

    // Call the callback once provided and pass on the arguments to purge.
    if (!is_null($processor_callback)) {

      // If the processor throws FALSE: release the item and try again later.
      if (call_user_func_array($processor_callback, array(
        $item->path,
      ))) {
        $items_to_be_removed[] = $item->item_id;
      }
    }
    else {
      $items_to_be_removed[] = $item->item_id;
    }
  }

  // Remove the items that we're listed as safe to be removed from the queue.
  if (count($items_to_be_removed)) {
    $ids = implode(',', $items_to_be_removed);
    db_query('DELETE FROM {ap_queue} WHERE item_id IN (' . $ids . ')');
  }

  // Set acquia_purge_queue_counter to 0 once the queue is empty as well.
  if ((int) db_result(db_query("SELECT COUNT(*) FROM {ap_queue}")) === 0) {

    // Once its back to 0, progress is 100% and no activity is assumed.
    variable_set('acquia_purge_queue_counter', 0);
    variable_set('acquia_purge_queue_owners', array());
  }
  return $items;
}