3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
29 * The queue service provides an interface for creating or locating
30 * queues. Note that this approach hides the details of data-storage:
31 * different queue-providers may store the queue content in different
32 * ways (in memory, in SQL, or in an external service).
35 * $queue = CRM_Queue_Service::singleton()->create(array(
36 * 'type' => 'interactive',
37 * 'name' => 'upgrade-tasks',
39 * $queue->createItem($myData);
41 * // Some time later...
42 * $item = $queue->claimItem();
44 * if (my_process($item->data)) {
45 * $queue->deleteItem($item);
47 * $queue->releaseItem($item);
52 class CRM_Queue_Service
{
57 * FIXME: Singleton pattern should be removed when dependency-injection
60 * @param $forceNew bool
62 * @return \CRM_Queue_Service
64 static function &singleton($forceNew = FALSE) {
65 if ($forceNew ||
!self
::$_singleton) {
66 self
::$_singleton = new CRM_Queue_Service();
68 return self
::$_singleton;
72 * @var array(queueName => CRM_Queue_Queue)
79 function __construct() {
80 $this->queues
= array();
84 * @param $queueSpec, array with keys:
85 * - type: string, required, e.g. "interactive", "immediate", "stomp", "beanstalk"
86 * - name: string, required, e.g. "upgrade-tasks"
87 * - reset: bool, optional; if a queue is found, then it should be flushed; default to TRUE
88 * - (additional keys depending on the queue provider)
90 * @return CRM_Queue_Queue
92 function create($queueSpec) {
93 if (@is_object
($this->queues
[$queueSpec['name']]) && empty($queueSpec['reset'])) {
94 return $this->queues
[$queueSpec['name']];
97 $queue = $this->instantiateQueueObject($queueSpec);
98 $exists = $queue->existsQueue();
100 $queue->createQueue();
102 elseif (@$queueSpec['reset']) {
103 $queue->deleteQueue();
104 $queue->createQueue();
109 $this->queues
[$queueSpec['name']] = $queue;
114 * @param $queueSpec, array with keys:
115 * - type: string, required, e.g. "interactive", "immediate", "stomp", "beanstalk"
116 * - name: string, required, e.g. "upgrade-tasks"
117 * - (additional keys depending on the queue provider)
119 * @return CRM_Queue_Queue
121 function load($queueSpec) {
122 if (is_object($this->queues
[$queueSpec['name']])) {
123 return $this->queues
[$queueSpec['name']];
125 $queue = $this->instantiateQueueObject($queueSpec);
127 $this->queues
[$queueSpec['name']] = $queue;
132 * Convert a queue "type" name to a class name
134 * @param $type string, e.g. "interactive", "immediate", "stomp", "beanstalk"
136 * @return string, class-name
138 protected function getQueueClass($type) {
139 $type = preg_replace('/[^a-zA-Z0-9]/', '', $type);
140 $className = 'CRM_Queue_Queue_' . $type;
141 // FIXME: when used with class-autoloader, this may be unnecessary
142 if (!class_exists($className)) {
143 $classFile = 'CRM/Queue/Queue/' . $type . '.php';
144 require_once $classFile;
150 * @param $queueSpec array, see create()
152 * @return CRM_Queue_Queue
154 protected function instantiateQueueObject($queueSpec) {
155 // note: you should probably never do anything else here
156 $class = new ReflectionClass($this->getQueueClass($queueSpec['type']));
157 return $class->newInstance($queueSpec);