dev/drupal#98 Fix masquerade issue caused by drupal update change
[civicrm-core.git] / CRM / Queue / Task.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * A task is an item that can be enqueued and later executed
14 */
15 class CRM_Queue_Task {
16
17 /**
18 * Task was performed successfully.
19 */
20 const TASK_SUCCESS = 1;
21
22 /**
23 * Task failed and should not be retried.
24 */
25 const TASK_FAIL = 2;
26
27 /**
28 * @var mixed, serializable
29 */
30 public $callback;
31
32 /**
33 * @var array, serializable
34 */
35 public $arguments;
36
37 /**
38 * @var string, NULL-able
39 */
40 public $title;
41
42 /**
43 * @param mixed $callback
44 * Serializable, a callable PHP item; must accept at least one argument
45 * (CRM_Queue_TaskContext).
46 * @param array $arguments
47 * Serializable, extra arguments to pass to the callback (in order).
48 * @param string $title
49 * A printable string which describes this task.
50 */
51 public function __construct($callback, $arguments, $title = NULL) {
52 $this->callback = $callback;
53 $this->arguments = $arguments;
54 $this->title = $title;
55 }
56
57 /**
58 * Perform the task.
59 *
60 * @param array $taskCtx
61 * Array with keys:
62 * - log: object 'Log'
63 *
64 * @throws Exception
65 * @return bool, TRUE if task completes successfully
66 */
67 public function run($taskCtx) {
68 $args = $this->arguments;
69 array_unshift($args, $taskCtx);
70
71 if (is_callable($this->callback)) {
72 $result = call_user_func_array($this->callback, $args);
73 return $result;
74 }
75 else {
76 throw new Exception('Failed to call callback: ' . print_r($this->callback));
77 }
78 }
79
80 }