You are here

function messaging_store_queue_process in Messaging 6

Same name and namespace in other branches
  1. 5 messaging.store.inc \messaging_store_queue_process()
  2. 6.2 messaging.store.inc \messaging_store_queue_process()
  3. 6.3 messaging.store.inc \messaging_store_queue_process()

Process and send messages in queue, to be called from cron

It will check for predefined limits and repeat the cycle [fetch] -> [send] -> [check] until the queue is empty or any of the limits are met

Parameters

$timeout: Optional time out to use instead of cron, just for this api to be testable

File

./messaging.store.inc, line 24
Database storage for the messaging framework

Code

function messaging_store_queue_process($timeout = 0) {
  $limit = variable_get('messaging_process_limit', array(
    'message' => 0,
    'time' => 0,
    'percent' => 0,
  ));

  // Calculate time limit. We get the smaller of all these times in seconds
  if ($timeout) {
    $timelimit[] = time() + $timeout;
  }
  else {
    $timelimit[] = variable_get('cron_semaphore', 0) + ini_get('max_execution_time') - MESSAGING_TIME_MARGIN;
  }
  if ($limit['time']) {
    $timelimit[] = time() + $limit['time'];
  }
  if ($limit['percent']) {
    $timelimit[] = time() + ini_get('max_execution_time') * $limit['percent'] / 100;
    unset($limit['percent']);
  }
  $limit['time'] = min($timelimit);

  // Processing loop. Will stop when we run out of rows or reach time / messages limit
  $count = 0;
  $max = !empty($limit['message']) ? $limit['message'] : 0;
  do {
    $step = $max ? min(MESSAGING_STEP_ROWS, $max - $count) : MESSAGING_STEP_ROWS;
    $number = messaging_store_queue_process_step($step, $limit['time']);
    $count += $number;
  } while ($number == $step && time() <= $limit['time'] && (!$max || $max > $count));
}