3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
13 * The QueueTrait provides helper methods for adding new tasks to a queue.
15 trait CRM_Extension_Upgrader_QueueTrait
{
17 abstract public function getExtensionKey();
20 * @var \CRM_Queue_Queue
25 * @var \CRM_Queue_TaskContext
30 * Adapter that lets you add normal (non-static) member functions to the queue.
32 * While working through a task-queue, the _queueAdapter is called statically. It looks up
33 * the appropriate object and invokes the expected method.
36 * CRM_Extension_Upgrader::_queueAdapter($ctx, 'org.example.myext', 'methodName', 'arg1', 'arg2');
39 public static function _queueAdapter(CRM_Queue_TaskContext
$ctx, string $extensionKey, string $method, ...$args) {
40 /** @var static $upgrader */
41 $upgrader = \CRM_Extension_System
::singleton()->getMapper()->getUpgrader($extensionKey);
42 if ($upgrader->ctx
!== NULL) {
43 throw new \
RuntimeException(sprintf("Cannot execute task for %s (%s::%s) - task already active.", $extensionKey, get_class($upgrader), $method));
46 $upgrader->ctx
= $ctx;
47 $upgrader->queue
= $ctx->queue
;
49 return call_user_func_array([$upgrader, $method], $args);
51 $upgrader->ctx
= NULL;
55 public function addTask(string $title, string $funcName, ...$options) {
56 return $this->prependTask($title, $funcName, ...$options);
60 * Enqueue a task based on a method in this class.
62 * The task is weighted so that it is processed as part of the currently-pending revision.
64 * After passing the $funcName, you can also pass parameters that will go to
65 * the function. Note that all params must be serializable.
67 public function prependTask(string $title, string $funcName, ...$options) {
68 $task = new CRM_Queue_Task(
69 [get_class($this), '_queueAdapter'],
70 array_merge([$this->getExtensionKey(), $funcName], $options),
73 return $this->queue
->createItem($task, ['weight' => -1]);
77 * Enqueue a task based on a method in this class.
79 * The task has a default weight.
83 protected function appendTask(string $title, string $funcName, ...$options) {
84 $task = new CRM_Queue_Task(
85 [get_class($this), '_queueAdapter'],
86 array_merge([$this->getExtensionKey(), $funcName], $options),
89 return $this->queue
->createItem($task);
92 // ******** Basic getters/setters ********
95 * @return \CRM_Queue_Queue
97 public function getQueue(): \CRM_Queue_Queue
{
102 * @param \CRM_Queue_Queue $queue
104 public function setQueue(\CRM_Queue_Queue
$queue): void
{
105 $this->queue
= $queue;