You are here

advancedqueue_example.module in Advanced Queue 7

Example module for Advanced Queues module.

File

advancedqueue_example/advancedqueue_example.module
View source
<?php

/**
 * @file
 * Example module for Advanced Queues module.
 */

/**
 * Implements hook_advanced_queue_info().
 */
function advancedqueue_example_advanced_queue_info() {
  $items['example_queue'] = array(
    'label' => t('Example queue'),
    'worker callback' => 'advancedqueue_example_worker',
    'groups' => array(
      'example',
    ),
    // The number of seconds to retry after.
    'retry after' => 10,
    // The maximum number of attempts after a failure.
    'max attempts' => 5,
    // Time after which an item is released from processing and requeued.
    'lease time' => 45,
  );
  return $items;
}

/**
 * Advanced queue worker; Process a queue item.
 *
 * @param $item
 *   The item object to process.
 * @param $end_time
 *   (Optional) The time this process should end.
 *
 * @return
 *   Boolean indicating if the process succeeded, or an array with
 *   "status" and "result" keys.
 */
function advancedqueue_example_worker($item, $end_time = FALSE) {
  $data = $item->data;
  $params = array(
    '@id' => $item->item_id,
    '@uid' => $data['uid'],
    '@time' => date('r', $data['timestamp']),
  );
  advancedqueue_log_message(format_string('The "worker" is now processing a example task number @id for user ID @uid created at @time.', $params));

  // For example purposes we will return an array with detailed message
  // for odd item IDs, and boolean for even ones.
  if ($item->item_id % 2) {
    return TRUE;
  }
  return array(
    'status' => ADVANCEDQUEUE_STATUS_SUCCESS,
    'result' => 'Processed ' . $item->item_id,
  );
}

/**
 * Implements hook_init().
 *
 * Create dummy queue items.
 */
function advancedqueue_example_init() {
  global $user;
  $queue = DrupalQueue::get('example_queue', TRUE);
  $task = array(
    'timestamp' => time(),
    'uid' => $user->uid,
  );
  $queue
    ->createItem($task);
}

Functions

Namesort descending Description
advancedqueue_example_advanced_queue_info Implements hook_advanced_queue_info().
advancedqueue_example_init Implements hook_init().
advancedqueue_example_worker Advanced queue worker; Process a queue item.