You are here

function sparkpost_requeue_sparkpost_mailsend_error in Sparkpost email 7

Same name and namespace in other branches
  1. 8.2 modules/sparkpost_requeue/sparkpost_requeue.module \sparkpost_requeue_sparkpost_mailsend_error()
  2. 7.2 modules/sparkpost_requeue/sparkpost_requeue.module \sparkpost_requeue_sparkpost_mailsend_error()

Implements hook_sparkpost_message_failed().

File

modules/sparkpost_requeue/sparkpost_requeue.module, line 28
Sparkpost requeue module.

Code

function sparkpost_requeue_sparkpost_mailsend_error(SparkpostMessageWrapperInterface $message_wrapper) {

  // Check if this message has been bouncing around for way too long.
  $limit = variable_get('sparkpost_requeue_max_retries', 10);
  $minimum_time = variable_get('sparkpost_requeue_minimum_time', 300);
  if (!$message_wrapper instanceof SparkpostQueuedMessageWrapper) {

    // We want to wrap it in our own class.
    $message_wrapper = new SparkpostQueuedMessageWrapper($message_wrapper);
  }
  if ($message_wrapper
    ->getRetryCount() >= $limit) {

    // Log the problem.
    watchdog('sparkpost_requeue', 'Message has been requeued a total of @number times. Message will be discarded.', array(
      '@number' => $message_wrapper
        ->getRetryCount(),
    ));
    return;
  }

  // Check if it's not too soon for retrying.
  if (time() - $message_wrapper
    ->getLastRetry() < $minimum_time) {
    throw new Exception('Too soon to retry');
  }

  // Set a log message about what we are doing.
  watchdog('sparkpost_requeue', 'Caught a failed message. Will requeue it for sending later. Current count is @count', array(
    '@count' => $message_wrapper
      ->getRetryCount(),
  ));

  // Increment a counter about how many times we have tried to send this
  // message.
  $message_wrapper
    ->incrementRetryCount();

  // Set last retry timestamp.
  $message_wrapper
    ->setLastRetry(time());

  // Then just put it in the queue.
  $queue = DrupalQueue::get(SPARKPOST_QUEUE_NAME);
  $queue
    ->createItem($message_wrapper);
}