Simple Task Queue

The Apex\Svc\Queue class provides an easy to use interface to execute and manage tasks on a separate process so as to not slow down the current request. This page explains how to add, manage and purge tasks within the queue. Please note, the tasks will be executed on the same server the task is added on. If you have a need to have the tasks executed on a separate server, please visist the /boot/cluster.yml File page of this section.

Add Task

You may add a new task to the queue for processing via the Queue::add() method, for example:

<?php

use APex\Svc\Queue;


class MyClass
{

    #[Inject(Queue::class)]
    private Queue $queue;

    /**
    * Process
     */
    public function process():void
    {

        // Add task
        $task = $this->queue->add(
            App\MyPackage\Tasks\MyTask::class;,
            ['var1' => 'value1', 'var2' => 'value2']
        );
    }
}

The above will execute the MyTask::process() method within next next 60 seconds assuming the necessary crontab job has been enabled. The process() method only takes one array as an argument, which is the array that is passed as the second argument to the Queue::add() method. Please view the Queue::add() page of the documentation for details on this method.

List Pending Tasks

You may retrive an iterator of all pending tasks via the Queue::list() method, for example:

$tasks = $this->queue->list();
foreach ($tasks as $task) {
    echo "Class: $task->class_ame\n";
}

Purge Tasks

You may purge and delete all pending tasks via the Queue::purge() method, for example:

$this->queue->pruge();

Once run, all pendint asks will be removed from the queue and will not be executed.