INFRA-132 - Put "else" and "catch" on new line
[civicrm-core.git] / CRM / Queue / Service.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
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)) {
16e38e82 45 * $queue->deleteItem($item);
5e74f823 46 * } else {
16e38e82 47 * $queue->releaseItem($item);
5e74f823 48 * }
6a488035
TO
49 * }
50 * @endcode
51 */
52class CRM_Queue_Service {
53
4523a2f5 54 protected static $_singleton;
6a488035
TO
55
56 /**
57 * FIXME: Singleton pattern should be removed when dependency-injection
58 * becomes available.
59 *
4523a2f5
TO
60 * @param bool $forceNew
61 * TRUE if a new instance must be created.
77b97be7
EM
62 *
63 * @return \CRM_Queue_Service
6a488035 64 */
4523a2f5 65 public static function &singleton($forceNew = FALSE) {
6a488035
TO
66 if ($forceNew || !self::$_singleton) {
67 self::$_singleton = new CRM_Queue_Service();
68 }
69 return self::$_singleton;
70 }
71
72 /**
4523a2f5 73 * @var array (string $queueName => CRM_Queue_Queue)
6a488035 74 */
4523a2f5 75 public $queues;
e0ef6999
EM
76
77 /**
78 *
79 */
4523a2f5 80 public function __construct() {
6a488035
TO
81 $this->queues = array();
82 }
83
84 /**
4523a2f5
TO
85 * Create a queue. If one already exists, then it will be reused.
86 *
87 * @param array $queueSpec
88 * Array with keys:
89 * - type: string, required, e.g. "interactive", "immediate", "stomp",
90 * "beanstalk"
6a488035 91 * - name: string, required, e.g. "upgrade-tasks"
4523a2f5
TO
92 * - reset: bool, optional; if a queue is found, then it should be
93 * flushed; default to TRUE
94 * - (additional keys depending on the queue provider).
6a488035
TO
95 *
96 * @return CRM_Queue_Queue
97 */
4523a2f5 98 public function create($queueSpec) {
6a488035
TO
99 if (@is_object($this->queues[$queueSpec['name']]) && empty($queueSpec['reset'])) {
100 return $this->queues[$queueSpec['name']];
101 }
102
103 $queue = $this->instantiateQueueObject($queueSpec);
104 $exists = $queue->existsQueue();
105 if (!$exists) {
106 $queue->createQueue();
107 }
108 elseif (@$queueSpec['reset']) {
109 $queue->deleteQueue();
110 $queue->createQueue();
111 }
112 else {
113 $queue->loadQueue();
114 }
115 $this->queues[$queueSpec['name']] = $queue;
116 return $queue;
117 }
118
119 /**
4523a2f5
TO
120 * Look up an existing queue.
121 *
122 * @param array $queueSpec
123 * Array with keys:
124 * - type: string, required, e.g. "interactive", "immediate", "stomp",
125 * "beanstalk"
6a488035 126 * - name: string, required, e.g. "upgrade-tasks"
4523a2f5 127 * - (additional keys depending on the queue provider).
6a488035
TO
128 *
129 * @return CRM_Queue_Queue
130 */
4523a2f5 131 public function load($queueSpec) {
6a488035
TO
132 if (is_object($this->queues[$queueSpec['name']])) {
133 return $this->queues[$queueSpec['name']];
134 }
135 $queue = $this->instantiateQueueObject($queueSpec);
136 $queue->loadQueue();
137 $this->queues[$queueSpec['name']] = $queue;
138 return $queue;
139 }
140
141 /**
142 * Convert a queue "type" name to a class name
143 *
4523a2f5
TO
144 * @param string $type
145 * E.g. "interactive", "immediate", "stomp", "beanstalk".
6a488035 146 *
4523a2f5
TO
147 * @return string
148 * Class-name
6a488035
TO
149 */
150 protected function getQueueClass($type) {
151 $type = preg_replace('/[^a-zA-Z0-9]/', '', $type);
152 $className = 'CRM_Queue_Queue_' . $type;
153 // FIXME: when used with class-autoloader, this may be unnecessary
154 if (!class_exists($className)) {
155 $classFile = 'CRM/Queue/Queue/' . $type . '.php';
156 require_once $classFile;
157 }
158 return $className;
159 }
160
161 /**
4523a2f5
TO
162 * @param array $queueSpec
163 * See create().
6a488035
TO
164 *
165 * @return CRM_Queue_Queue
166 */
167 protected function instantiateQueueObject($queueSpec) {
168 // note: you should probably never do anything else here
169 $class = new ReflectionClass($this->getQueueClass($queueSpec['type']));
170 return $class->newInstance($queueSpec);
171 }
172}