You are here

function _acquia_purge_queue_processpurge_requests in Acquia Purge 6

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

Queue manager: process the given HTTP requests and do it efficiently.

@returns The given requests array with added properties that describe the result of the request: 'result', 'error_curl', 'error_http', 'error_debug'.

Parameters

string $requests: Unassociative array (list) of simple Stdclass objects with the following properties: scheme, rtype, server, domain, path, uri, uribal.

1 call to _acquia_purge_queue_processpurge_requests()
_acquia_purge_queue_processpurge in ./acquia_purge.module
Queue manager: process a single path (on all domains and balancers).

File

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

Code

function _acquia_purge_queue_processpurge_requests($requests) {
  $single_mode = count($requests) === 1;
  $results = array();

  // Initialize the cURL multi handler.
  if (!$single_mode) {
    static $curl_multi;
    if (is_null($curl_multi)) {
      $curl_multi = curl_multi_init();
    }
  }

  // Enter our event loop and keep on requesting until $unprocessed is empty.
  $unprocessed = count($requests);
  while ($unprocessed > 0) {

    // Group requests per sets that we can run in parallel.
    for ($i = 0; $i < ACQUIA_PURGE_PARALLEL_REQUESTS; $i++) {
      if ($rqst = array_shift($requests)) {
        $rqst->curl = curl_init();

        // Instantiate the cURL resource and configure its runtime parameters.
        curl_setopt($rqst->curl, CURLOPT_URL, $rqst->uribal);
        curl_setopt($rqst->curl, CURLOPT_TIMEOUT, ACQUIA_PURGE_REQUEST_TIMEOUT);
        curl_setopt($rqst->curl, CURLOPT_HTTPHEADER, $rqst->headers);
        curl_setopt($rqst->curl, CURLOPT_CUSTOMREQUEST, $rqst->rtype);
        curl_setopt($rqst->curl, CURLOPT_FAILONERROR, TRUE);

        // Add our handle to the multiple cURL handle.
        if (!$single_mode) {
          curl_multi_add_handle($curl_multi, $rqst->curl);
        }

        // Add the shifted request to the results array and change the counter.
        $results[] = $rqst;
        $unprocessed--;
      }
    }

    // Execute the created handles in parallel.
    if (!$single_mode) {
      $active = NULL;
      do {
        $mrc = curl_multi_exec($curl_multi, $active);
      } while ($mrc == CURLM_CALL_MULTI_PERFORM);
      while ($active && $mrc == CURLM_OK) {
        if (curl_multi_select($curl_multi) != -1) {
          do {
            $mrc = curl_multi_exec($curl_multi, $active);
          } while ($mrc == CURLM_CALL_MULTI_PERFORM);
        }
      }
    }
    else {
      curl_exec($results[0]->curl);
      $single_info = array(
        'result' => curl_errno($results[0]->curl),
      );
    }

    // Iterate the set of results and fetch cURL result and resultcodes. Only
    // process those with the 'curl' property as the property will be removed.
    foreach ($results as $i => $rqst) {
      if (!isset($rqst->curl)) {
        continue;
      }
      $info = $single_mode ? $single_info : curl_multi_info_read($curl_multi);
      $results[$i]->result = $info['result'] == CURLE_OK ? TRUE : FALSE;
      $results[$i]->error_curl = $info['result'];
      $results[$i]->error_http = curl_getinfo($rqst->curl, CURLINFO_HTTP_CODE);

      // When the result failed but the HTTP code is 404 we turn the result
      // into a TRUE as Varnish simply couldn't find the entry as its not there.
      if (!$results[$i]->result && $results[$i]->error_http == 404) {
        $results[$i]->result = TRUE;
      }

      // Collect debugging information if necessary.
      $results[$i]->error_debug = '';
      if (!$results[$i]->result) {
        $debug = curl_getinfo($rqst->curl);
        $debug['headers'] = implode('|', $rqst->headers);
        unset($debug['certinfo']);
        $results[$i]->error_debug = _acquia_purge_export_debug_symbols($debug);
      }

      // Remove the handle if parallel processing occurred.
      if (!$single_mode) {
        curl_multi_remove_handle($curl_multi, $rqst->curl);
      }

      // Close the resource and delete its property.
      curl_close($rqst->curl);
      unset($rqst->curl);
    }
  }
  return $results;
}