Merge pull request #17878 from civicrm/5.28
[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 array $taskCtx
63 * Array with keys:
64 * - log: object 'Log'
65 *
66 * @throws Exception
67 * @return bool, TRUE if task completes successfully
68 */
69 public function run($taskCtx) {
70 $args = $this->arguments;
71 array_unshift($args, $taskCtx);
72
73 if (is_callable($this->callback)) {
74 $result = call_user_func_array($this->callback, $args);
75 return $result;
76 }
77 else {
78 throw new Exception('Failed to call callback: ' . print_r($this->callback));
79 }
80 }
81
82 }