Merge pull request #21525 from eileenmcnaughton/cont_dep
[civicrm-core.git] / CRM / Queue / Task.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
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 |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
b44e3f84 13 * A task is an item that can be enqueued and later executed
6a488035
TO
14 */
15class CRM_Queue_Task {
16
389bcebf 17 /**
fe482240 18 * Task was performed successfully.
389bcebf 19 */
4523a2f5 20 const TASK_SUCCESS = 1;
6a488035 21
389bcebf 22 /**
fe482240 23 * Task failed and should not be retried.
389bcebf 24 */
4523a2f5 25 const TASK_FAIL = 2;
6a488035
TO
26
27 /**
51dda21e
SL
28 * @var mixed
29 * serializable
6a488035 30 */
4523a2f5 31 public $callback;
6a488035
TO
32
33 /**
51dda21e
SL
34 * @var array
35 * serializable
6a488035 36 */
4523a2f5 37 public $arguments;
6a488035
TO
38
39 /**
51dda21e 40 * @var string|null
6a488035 41 */
4523a2f5 42 public $title;
6a488035
TO
43
44 /**
4523a2f5
TO
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.
6a488035 52 */
4523a2f5
TO
53 public function __construct($callback, $arguments, $title = NULL) {
54 $this->callback = $callback;
6a488035 55 $this->arguments = $arguments;
4523a2f5 56 $this->title = $title;
6a488035
TO
57 }
58
59 /**
fe482240 60 * Perform the task.
6a488035 61 *
4523a2f5
TO
62 * @param array $taskCtx
63 * Array with keys:
64 * - log: object 'Log'
6a488035 65 *
c490a46a 66 * @throws Exception
6a488035
TO
67 * @return bool, TRUE if task completes successfully
68 */
4523a2f5 69 public function run($taskCtx) {
6a488035
TO
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 }
96025800 81
6a488035 82}