You are here

function simplenews_get_spool in Simplenews 6.2

Same name and namespace in other branches
  1. 6 simplenews.module \simplenews_get_spool()
  2. 7.2 includes/simplenews.mail.inc \simplenews_get_spool()
  3. 7 includes/simplenews.mail.inc \simplenews_get_spool()

This function allocates messages to be sent in current run.

Drupal acquire_lock quarantees that no concurrency issue happend. If message status is SIMPLENEWS_SPOOL_IN_PROGRESS but the maximum send time has expired the message id will be returned as a message which is not allocated to another process. MessageIDs to be sent in current run are returned.

Parameters

array $status of data to be retrieved: SIMPLENEWS_SPOOL_HOLD, _PENDING, _DONE, _IN_PROGRESS

integer $limit The maximum number of mails to load from the spool:

Return value

array Mail message array $message['msid'] $message['mail'] $message['nid'] $message['vid'] $message['tid'] $message['status'] $message['time']

1 call to simplenews_get_spool()
simplenews_mail_spool in includes/simplenews.mail.inc
Send simplenews newsletters from the spool.

File

includes/simplenews.mail.inc, line 409
Simplenews email send and spool handling

Code

function simplenews_get_spool($status, $limit = NULL) {
  $messages = array();
  $clauses = array();
  $params = array();
  if (!is_array($status)) {
    $status = array(
      $status,
    );
  }
  foreach ($status as $s) {
    if ($s == SIMPLENEWS_SPOOL_IN_PROGRESS) {

      // Select messages which are allocated by another process, but where the maximum send time has expired.
      $clauses[] = '(s.status = %d AND s.timestamp < %d)';
      $params[] = $s;
      $params[] = simplenews_get_expiration_time();
    }
    else {
      $clauses[] = 's.status = %d';
      $params[] = $s;
    }
  }
  $query = "SELECT *\n    FROM {simplenews_mail_spool} s\n    WHERE " . implode(' OR ', $clauses) . "\n    ORDER BY s.timestamp ASC";

  /* BEGIN CRITICAL SECTION */

  // The semaphore ensures that multiple processes get different message ID's. So there could not occur any duplicate messages.
  if (lock_acquire('simplenews_acquire_mail')) {

    // Get message id's
    // Allocate messages
    if (is_numeric($limit)) {
      $result = db_query_range($query, $params, 0, $limit);
    }
    else {
      $result = db_query($query, $params);
    }
    while ($message = db_fetch_array($result)) {
      $messages[$message['msid']] = $message;
    }
    if (count($messages) > 0) {

      // Set the state and the timestamp of the messages
      simplenews_update_spool(array_keys($messages), array(
        'status' => SIMPLENEWS_SPOOL_IN_PROGRESS,
      ));
    }
    lock_release('simplenews_acquire_mail');
  }

  /* END CRITICAL SECTION */
  return $messages;
}