Merge pull request #22679 from mattwire/dummyreturn
[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
29 * serializable
30 */
31 public $callback;
32
33 /**
34 * @var array
35 * serializable
36 */
37 public $arguments;
38
39 /**
40 * @var string|null
41 */
42 public $title;
43
44 /**
45 * @param mixed $callback
46 * Serializable, a callable PHP item; must accept at least one argument
47 * (CRM_Queue_TaskContext).
48 * @param array $arguments
49 * Serializable, extra arguments to pass to the callback (in order).
50 * @param string $title
51 * A printable string which describes this task.
52 */
53 public function __construct($callback, $arguments, $title = NULL) {
54 $this->callback = $callback;
55 $this->arguments = $arguments;
56 $this->title = $title;
57 }
58
59 /**
60 * Perform the task.
61 *
62 * @param \CRM_Queue_TaskContext $taskCtx
63 * @throws Exception
64 * @return bool
65 * TRUE if task completes successfully.
66 * FALSE or exception if task fails.
67 */
68 public function run($taskCtx) {
69 $args = $this->arguments;
70 array_unshift($args, $taskCtx);
71
72 if (is_callable($this->callback)) {
73 $result = call_user_func_array($this->callback, $args);
74 return $result;
75 }
76 else {
77 throw new Exception('Failed to call callback: ' . print_r($this->callback));
78 }
79 }
80
81 }