(NFC) (dev/core#878) Simplify copyright header (CRM/*)
[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 /**
28 * @var mixed, serializable
29 */
4523a2f5 30 public $callback;
6a488035
TO
31
32 /**
33 * @var array, serializable
34 */
4523a2f5 35 public $arguments;
6a488035
TO
36
37 /**
38 * @var string, NULL-able
39 */
4523a2f5 40 public $title;
6a488035
TO
41
42 /**
4523a2f5
TO
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.
6a488035 50 */
4523a2f5
TO
51 public function __construct($callback, $arguments, $title = NULL) {
52 $this->callback = $callback;
6a488035 53 $this->arguments = $arguments;
4523a2f5 54 $this->title = $title;
6a488035
TO
55 }
56
57 /**
fe482240 58 * Perform the task.
6a488035 59 *
4523a2f5
TO
60 * @param array $taskCtx
61 * Array with keys:
62 * - log: object 'Log'
6a488035 63 *
c490a46a 64 * @throws Exception
6a488035
TO
65 * @return bool, TRUE if task completes successfully
66 */
4523a2f5 67 public function run($taskCtx) {
6a488035
TO
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 }
96025800 79
6a488035 80}