Update Service.php
[civicrm-core.git] / CRM / Queue / Service.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
06b69b18 4 | CiviCRM version 4.5 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26*/
27
28/**
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).
33 *
34 * @code
35 * $queue = CRM_Queue_Service::singleton()->create(array(
36 * 'type' => 'interactive',
37 * 'name' => 'upgrade-tasks',
38 * ));
39 * $queue->createItem($myData);
40 *
41 * // Some time later...
42 * $item = $queue->claimItem();
5e74f823
TO
43 * if ($item) {
44 * if (my_process($item->data)) {
45 * $myMessage->deleteItem($item);
46 * } else {
47 * $myMessage->releaseItem($item);
48 * }
6a488035
TO
49 * }
50 * @endcode
51 */
52class CRM_Queue_Service {
53
54 static $_singleton;
55
56 /**
57 * FIXME: Singleton pattern should be removed when dependency-injection
58 * becomes available.
59 *
60 * @param $forceNew bool
77b97be7
EM
61 *
62 * @return \CRM_Queue_Service
6a488035
TO
63 */
64 static function &singleton($forceNew = FALSE) {
65 if ($forceNew || !self::$_singleton) {
66 self::$_singleton = new CRM_Queue_Service();
67 }
68 return self::$_singleton;
69 }
70
71 /**
72 * @var array(queueName => CRM_Queue_Queue)
73 */
74 var $queues;
75 function __construct() {
76 $this->queues = array();
77 }
78
79 /**
80 *
81 * @param $queueSpec, array with keys:
82 * - type: string, required, e.g. "interactive", "immediate", "stomp", "beanstalk"
83 * - name: string, required, e.g. "upgrade-tasks"
84 * - reset: bool, optional; if a queue is found, then it should be flushed; default to TRUE
85 * - (additional keys depending on the queue provider)
86 *
87 * @return CRM_Queue_Queue
88 */
89 function create($queueSpec) {
90 if (@is_object($this->queues[$queueSpec['name']]) && empty($queueSpec['reset'])) {
91 return $this->queues[$queueSpec['name']];
92 }
93
94 $queue = $this->instantiateQueueObject($queueSpec);
95 $exists = $queue->existsQueue();
96 if (!$exists) {
97 $queue->createQueue();
98 }
99 elseif (@$queueSpec['reset']) {
100 $queue->deleteQueue();
101 $queue->createQueue();
102 }
103 else {
104 $queue->loadQueue();
105 }
106 $this->queues[$queueSpec['name']] = $queue;
107 return $queue;
108 }
109
110 /**
111 *
112 * @param $queueSpec, array with keys:
113 * - type: string, required, e.g. "interactive", "immediate", "stomp", "beanstalk"
114 * - name: string, required, e.g. "upgrade-tasks"
115 * - (additional keys depending on the queue provider)
116 *
117 * @return CRM_Queue_Queue
118 */
119 function load($queueSpec) {
120 if (is_object($this->queues[$queueSpec['name']])) {
121 return $this->queues[$queueSpec['name']];
122 }
123 $queue = $this->instantiateQueueObject($queueSpec);
124 $queue->loadQueue();
125 $this->queues[$queueSpec['name']] = $queue;
126 return $queue;
127 }
128
129 /**
130 * Convert a queue "type" name to a class name
131 *
132 * @param $type string, e.g. "interactive", "immediate", "stomp", "beanstalk"
133 *
134 * @return string, class-name
135 */
136 protected function getQueueClass($type) {
137 $type = preg_replace('/[^a-zA-Z0-9]/', '', $type);
138 $className = 'CRM_Queue_Queue_' . $type;
139 // FIXME: when used with class-autoloader, this may be unnecessary
140 if (!class_exists($className)) {
141 $classFile = 'CRM/Queue/Queue/' . $type . '.php';
142 require_once $classFile;
143 }
144 return $className;
145 }
146
147 /**
148 *
149 * @param $queueSpec array, see create()
150 *
151 * @return CRM_Queue_Queue
152 */
153 protected function instantiateQueueObject($queueSpec) {
154 // note: you should probably never do anything else here
155 $class = new ReflectionClass($this->getQueueClass($queueSpec['type']));
156 return $class->newInstance($queueSpec);
157 }
158}
159