Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * The queue runner is a helper which runs all jobs in a queue. | |
30 | * | |
31 | * The queue runner is most useful for one-off queues (such as an upgrade); | |
32 | * if the intention is to develop a dedicated, long-running worker thread, | |
33 | * then one should consider writing a new queue consumer. | |
34 | */ | |
35 | class CRM_Queue_Runner { | |
36 | ||
37 | /** | |
4523a2f5 | 38 | * The failed task should be discarded, and queue processing should continue. |
6a488035 | 39 | */ |
4523a2f5 | 40 | const ERROR_CONTINUE = 1; |
6a488035 TO |
41 | |
42 | /** | |
4523a2f5 TO |
43 | * The failed task should be kept in the queue, and queue processing should |
44 | * abort. | |
6a488035 | 45 | */ |
4523a2f5 | 46 | const ERROR_ABORT = 2; |
6a488035 TO |
47 | |
48 | /** | |
49 | * @var string | |
50 | */ | |
4523a2f5 | 51 | public $title; |
6a488035 TO |
52 | |
53 | /** | |
54 | * @var CRM_Queue_Queue | |
55 | */ | |
4523a2f5 TO |
56 | public $queue; |
57 | public $errorMode; | |
58 | public $isMinimal; | |
59 | public $onEnd; | |
60 | public $onEndUrl; | |
61 | public $pathPrefix; | |
6a488035 | 62 | // queue-runner id; used for persistence |
4523a2f5 | 63 | public $qrid; |
6a488035 TO |
64 | |
65 | /** | |
66 | * @var array whether to display buttons, eg ('retry' => TRUE, 'skip' => FALSE) | |
67 | */ | |
4523a2f5 | 68 | public $buttons; |
6a488035 TO |
69 | |
70 | /** | |
71 | * @var CRM_Queue_TaskContext | |
72 | */ | |
4523a2f5 | 73 | public $taskCtx; |
6a488035 TO |
74 | |
75 | /** | |
4523a2f5 | 76 | * Locate a previously-created instance of the queue-runner. |
6a488035 | 77 | * |
4523a2f5 TO |
78 | * @param string $qrid |
79 | * The queue-runner ID. | |
6a488035 | 80 | * |
4523a2f5 | 81 | * @return CRM_Queue_Runner|NULL |
6a488035 | 82 | */ |
4523a2f5 | 83 | public static function instance($qrid) { |
6a488035 TO |
84 | if (!empty($_SESSION['queueRunners'][$qrid])) { |
85 | return unserialize($_SESSION['queueRunners'][$qrid]); | |
86 | } | |
87 | else { | |
88 | return NULL; | |
89 | } | |
90 | } | |
91 | ||
92 | /** | |
93 | * | |
94 | * FIXME: parameter validation | |
95 | * FIXME: document signature of onEnd callback | |
96 | * | |
4523a2f5 TO |
97 | * @param array $runnerSpec |
98 | * Array with keys: | |
6a488035 | 99 | * - queue: CRM_Queue_Queue |
4523a2f5 TO |
100 | * - errorMode: int, ERROR_CONTINUE or ERROR_ABORT. |
101 | * - onEnd: mixed, a callback to update the UI after running; should be | |
102 | * both callable and serializable. | |
103 | * - onEndUrl: string, the URL to which one redirects. | |
104 | * - pathPrefix: string, prepended to URLs for the web-runner; | |
105 | * default: 'civicrm/queue'. | |
6a488035 TO |
106 | */ |
107 | public function __construct($runnerSpec) { | |
4523a2f5 TO |
108 | $this->title = CRM_Utils_Array::value('title', $runnerSpec, ts('Queue Runner')); |
109 | $this->queue = $runnerSpec['queue']; | |
110 | $this->errorMode = CRM_Utils_Array::value('errorMode', $runnerSpec, self::ERROR_ABORT); | |
111 | $this->isMinimal = CRM_Utils_Array::value('isMinimal', $runnerSpec, FALSE); | |
112 | $this->onEnd = CRM_Utils_Array::value('onEnd', $runnerSpec, NULL); | |
113 | $this->onEndUrl = CRM_Utils_Array::value('onEndUrl', $runnerSpec, NULL); | |
6a488035 | 114 | $this->pathPrefix = CRM_Utils_Array::value('pathPrefix', $runnerSpec, 'civicrm/queue'); |
4523a2f5 | 115 | $this->buttons = CRM_Utils_Array::value('buttons', $runnerSpec, array('retry' => TRUE, 'skip' => TRUE)); |
6a488035 TO |
116 | // perhaps this value should be randomized? |
117 | $this->qrid = $this->queue->getName(); | |
118 | } | |
119 | ||
e0ef6999 EM |
120 | /** |
121 | * @return array | |
122 | */ | |
4523a2f5 | 123 | public function __sleep() { |
6a488035 | 124 | // exclude taskCtx |
4523a2f5 TO |
125 | return array( |
126 | 'title', | |
127 | 'queue', | |
128 | 'errorMode', | |
129 | 'isMinimal', | |
130 | 'onEnd', | |
131 | 'onEndUrl', | |
132 | 'pathPrefix', | |
133 | 'qrid', | |
134 | 'buttons', | |
135 | ); | |
6a488035 TO |
136 | } |
137 | ||
138 | /** | |
139 | * Redirect to the web-based queue-runner and evaluate all tasks in a queue. | |
140 | */ | |
141 | public function runAllViaWeb() { | |
142 | $_SESSION['queueRunners'][$this->qrid] = serialize($this); | |
143 | $url = CRM_Utils_System::url($this->pathPrefix . '/runner', 'reset=1&qrid=' . urlencode($this->qrid)); | |
144 | CRM_Utils_System::redirect($url); | |
4523a2f5 | 145 | // TODO: evaluate items incrementally via AJAX polling, cleanup session |
6a488035 TO |
146 | } |
147 | ||
148 | /** | |
149 | * Immediately run all tasks in a queue (until either reaching the end | |
150 | * of the queue or encountering an error) | |
151 | * | |
152 | * If the runner has an onEndUrl, then this function will not return | |
153 | * | |
4523a2f5 TO |
154 | * @return mixed |
155 | * TRUE if all tasks complete normally; otherwise, an array describing the | |
156 | * failed task | |
6a488035 TO |
157 | */ |
158 | public function runAll() { | |
159 | $taskResult = $this->formatTaskResult(TRUE); | |
160 | while ($taskResult['is_continue']) { | |
161 | // setRaiseException should't be necessary here, but there's a bug | |
162 | // somewhere which causes this setting to be lost. Observed while | |
163 | // upgrading 4.0=>4.2. This preference really shouldn't be a global | |
03e04002 | 164 | // setting -- it should be more of a contextual/stack-based setting. |
6a488035 TO |
165 | // This should be appropriate because queue-runners are not used with |
166 | // basic web pages -- they're used with CLI/REST/AJAX. | |
167 | $errorScope = CRM_Core_TemporaryErrorScope::useException(); | |
168 | $taskResult = $this->runNext(); | |
169 | $errorScope = NULL; | |
170 | } | |
171 | ||
172 | if ($taskResult['numberOfItems'] == 0) { | |
173 | $result = $this->handleEnd(); | |
174 | if (!empty($result['redirect_url'])) { | |
175 | CRM_Utils_System::redirect($result['redirect_url']); | |
176 | } | |
177 | return TRUE; | |
178 | } | |
179 | else { | |
180 | return $taskResult; | |
181 | } | |
182 | } | |
183 | ||
184 | /** | |
185 | * Take the next item from the queue and attempt to run it. | |
186 | * | |
4523a2f5 TO |
187 | * Individual tasks may also throw exceptions -- caller should watch for |
188 | * exceptions. | |
6a488035 | 189 | * |
4523a2f5 TO |
190 | * @param bool $useSteal |
191 | * Whether to steal active locks. | |
6a488035 | 192 | * |
4523a2f5 TO |
193 | * @return array |
194 | * - is_error => bool, | |
195 | * - is_continue => bool, | |
196 | * - numberOfItems => int, | |
197 | * - 'last_task_title' => $, | |
198 | * - 'exception' => $ | |
6a488035 TO |
199 | */ |
200 | public function runNext($useSteal = FALSE) { | |
201 | if ($useSteal) { | |
202 | $item = $this->queue->stealItem(); | |
203 | } | |
204 | else { | |
205 | $item = $this->queue->claimItem(); | |
206 | } | |
207 | ||
208 | if ($item) { | |
209 | $this->lastTaskTitle = $item->data->title; | |
210 | ||
211 | $exception = NULL; | |
212 | try { | |
9901c22f | 213 | CRM_Core_Error::debug_log_message("Running task: " . $this->lastTaskTitle); |
6a488035 TO |
214 | $isOK = $item->data->run($this->getTaskContext()); |
215 | if (!$isOK) { | |
216 | $exception = new Exception('Task returned false'); | |
4523a2f5 | 217 | } |
0db6c3e1 TO |
218 | } |
219 | catch (Exception$e) { | |
6a488035 TO |
220 | $isOK = FALSE; |
221 | $exception = $e; | |
4523a2f5 | 222 | } |
6a488035 TO |
223 | |
224 | if ($isOK) { | |
225 | $this->queue->deleteItem($item); | |
226 | } | |
227 | else { | |
228 | $this->releaseErrorItem($item); | |
229 | } | |
230 | ||
231 | return $this->formatTaskResult($isOK, $exception); | |
232 | } | |
233 | else { | |
234 | return $this->formatTaskResult(FALSE, new Exception('Failed to claim next task')); | |
235 | } | |
236 | } | |
237 | ||
238 | /** | |
239 | * Take the next item from the queue and attempt to run it. | |
240 | * | |
4523a2f5 TO |
241 | * Individual tasks may also throw exceptions -- caller should watch for |
242 | * exceptions. | |
6a488035 | 243 | * |
4523a2f5 TO |
244 | * @param bool $useSteal |
245 | * Whether to steal active locks. | |
6a488035 | 246 | * |
4523a2f5 TO |
247 | * @return array |
248 | * - is_error => bool, | |
249 | * - is_continue => bool, | |
250 | * - numberOfItems => int) | |
6a488035 TO |
251 | */ |
252 | public function skipNext($useSteal = FALSE) { | |
253 | if ($useSteal) { | |
254 | $item = $this->queue->stealItem(); | |
255 | } | |
256 | else { | |
257 | $item = $this->queue->claimItem(); | |
258 | } | |
259 | ||
260 | if ($item) { | |
261 | $this->lastTaskTitle = $item->data->title; | |
262 | $this->queue->deleteItem($item); | |
263 | return $this->formatTaskResult(TRUE); | |
264 | } | |
265 | else { | |
266 | return $this->formatTaskResult(FALSE, new Exception('Failed to claim next task')); | |
267 | } | |
268 | } | |
269 | ||
e0ef6999 | 270 | /** |
4523a2f5 TO |
271 | * Release an item in keeping with the error mode. |
272 | * | |
273 | * @param object $item | |
274 | * The item previously produced by Queue::claimItem. | |
e0ef6999 | 275 | */ |
6a488035 TO |
276 | protected function releaseErrorItem($item) { |
277 | switch ($this->errorMode) { | |
278 | case self::ERROR_CONTINUE: | |
279 | $this->queue->deleteItem($item); | |
280 | case self::ERROR_ABORT: | |
281 | default: | |
282 | $this->queue->releaseItem($item); | |
283 | } | |
284 | } | |
285 | ||
286 | /** | |
4523a2f5 TO |
287 | * @return array |
288 | * - is_error => bool, | |
289 | * - is_continue => bool, | |
290 | * - numberOfItems => int, | |
291 | * - redirect_url => string | |
6a488035 TO |
292 | */ |
293 | public function handleEnd() { | |
294 | if (is_callable($this->onEnd)) { | |
295 | call_user_func($this->onEnd, $this->getTaskContext()); | |
296 | } | |
297 | // Don't remove queueRunner until onEnd succeeds | |
298 | if (!empty($_SESSION['queueRunners'][$this->qrid])) { | |
299 | unset($_SESSION['queueRunners'][$this->qrid]); | |
300 | } | |
301 | ||
302 | // Fallback; web UI does redirect in Javascript | |
303 | $result = array(); | |
304 | $result['is_error'] = 0; | |
305 | $result['numberOfItems'] = 0; | |
306 | $result['is_continue'] = 0; | |
307 | if (!empty($this->onEndUrl)) { | |
308 | $result['redirect_url'] = $this->onEndUrl; | |
309 | } | |
310 | return $result; | |
311 | } | |
312 | ||
313 | /** | |
4523a2f5 | 314 | * Format a result record which describes whether the task completed. |
6a488035 | 315 | * |
4523a2f5 TO |
316 | * @param bool $isOK |
317 | * TRUE if the task completed successfully. | |
318 | * @param Exception|NULL $exception | |
319 | * If applicable, an unhandled exception that arose during execution. | |
320 | * | |
321 | * @return array | |
322 | * (is_error => bool, is_continue => bool, numberOfItems => int) | |
6a488035 | 323 | */ |
4523a2f5 | 324 | public function formatTaskResult($isOK, $exception = NULL) { |
6a488035 TO |
325 | $numberOfItems = $this->queue->numberOfItems(); |
326 | ||
327 | $result = array(); | |
328 | $result['is_error'] = $isOK ? 0 : 1; | |
329 | $result['exception'] = $exception; | |
330 | $result['last_task_title'] = isset($this->lastTaskTitle) ? $this->lastTaskTitle : ''; | |
331 | $result['numberOfItems'] = $this->queue->numberOfItems(); | |
332 | if ($result['numberOfItems'] <= 0) { | |
333 | // nothing to do | |
334 | $result['is_continue'] = 0; | |
335 | } | |
336 | elseif ($isOK) { | |
337 | // more tasks remain, and this task succeeded | |
338 | $result['is_continue'] = 1; | |
339 | } | |
340 | elseif ($this->errorMode == CRM_Queue_Runner::ERROR_CONTINUE) { | |
341 | // more tasks remain, and we can disregard this error | |
342 | $result['is_continue'] = 1; | |
343 | } | |
344 | else { | |
345 | // more tasks remain, but we can't disregard the error | |
346 | $result['is_continue'] = 0; | |
347 | } | |
348 | ||
349 | return $result; | |
350 | } | |
351 | ||
e0ef6999 EM |
352 | /** |
353 | * @return CRM_Queue_TaskContext | |
354 | */ | |
6a488035 TO |
355 | protected function getTaskContext() { |
356 | if (!is_object($this->taskCtx)) { | |
357 | $this->taskCtx = new CRM_Queue_TaskContext(); | |
358 | $this->taskCtx->queue = $this->queue; | |
359 | // $this->taskCtx->log = CRM_Core_Config::getLog(); | |
360 | $this->taskCtx->log = CRM_Core_Error::createDebugLogger(); | |
361 | } | |
362 | return $this->taskCtx; | |
363 | } | |
96025800 | 364 | |
6a488035 | 365 | } |