You are here

function _eloqua_cron in Eloqua 6

Same name and namespace in other branches
  1. 7 eloqua_webform/eloqua_webform.cron.inc \_eloqua_cron()

Implementation of hook_cron()

1 call to _eloqua_cron()
eloqua_cron in ./eloqua.module

File

./eloqua.cron.inc, line 26

Code

function _eloqua_cron() {

  // Ensure that curl exists.  Otherwise this will fail hard.
  if (!function_exists('curl_init')) {
    $message = t('Curl does not appear to be installed.');
    $params = array();
    watchdog('eloqua', $message, $params, WATCHDOG_ALERT);
    return;
  }

  // Post all the data to Eloqua
  // Fetch Unposted Data
  $posts = eloqua_post_get_batch();

  // If nothnig to do, or something funky happened, bail.
  if (empty($posts) || !is_array($posts)) {
    return;
  }
  foreach ($posts as $post) {
    if (!is_array($post->data)) {
      $post->data = array();
    }

    // Only pass along the accept-language header as a header. We pass the user
    // agent information in the curl request below.
    $original_headers = $post->data['user_headers'];
    $headers = array(
      'Accept-Language' => array_key_exists('accept-language', $original_headers) ? $original_headers['accept-language'] : 'en',
    );

    // Fetch the post fields to send to Eloqua
    $post_fields = _eloqua_cron_get_post_fields($post);

    // Create the Curl Request
    $options = array();
    $options['post_fields'] = $post_fields;
    $options['user_agent'] = array_key_exists('user-agent', $original_headers) ? $original_headers['user-agent'] : 'User Relay';
    $options['http_headers'] = $headers;
    $ch = _eloqua_cron_get_curl_resource($options);
    if (is_null($ch)) {
      $message = t('Something went wrong with curl, unable to obtain handle.  Aborting status updates.');
      $params = array();
      watchdog('eloqua', $message, $params, WATCHDOG_ERROR);
      break;
    }
    try {
      $data = curl_exec($ch);
      $curl_status = curl_getinfo($ch);
      $status_class = (int) floor($curl_status['http_code'] / 100);
      $success = $data && ($status_class == 2 || $status_class == 3);
    } catch (Exception $e) {

      // PHP needs to implement finally
      // No idea what went wrong, should just bail out of the process.
      // However, log.
      $message = t('Something went wrong with curl. Uncaught exception: !message');
      $params = array(
        '!message' => $e
          ->getMessage(),
      );
      watchdog('eloqua', $message, $params, WATCHDOG_ERROR);

      // Throwing an error here will result in an uncaught exception.  This, when it happens in
      // cron, will result in the "Cron exceeding time limit" error, because it didn't end properly.
      // The above will log the message to watchdog, and that's the extent of the notice that
      // will be given.
      // To mimic current behaviour, break will be used instead of continue.
      // throw $e;
      break;
    }
    curl_close($ch);

    // CURL
    $post->data['server_post'][] = array(
      'timestamp' => time(),
      'response' => $data,
      'http_status' => $curl_status['http_code'],
    );

    // Update Post Data
    if ($success) {
      $post->{ELOQUA_POST_FIELD_STATUS} = ELOQUA_STATUS_UPLOADED;
    }
    else {
      $post->{ELOQUA_POST_FIELD_STATUS} = ELOQUA_STATUS_FAILED;
    }
    eloqua_post_update($post);
  }
}