Luminova Framework

PHP Luminova: Job Queue Management Module

Last updated: 2024-08-28 23:29:54

Luminova's queue class is a flexible and efficient solution for managing asynchronous tasks in (FIFO) order.

The Queue Class in Luminova provides a mechanism for managing asynchronous tasks, to handle background processes efficiently. Asynchronous processing is essential for improving application responsiveness and scalability, as it enables tasks to be executed independently of the main application flow.

Luminova's Queue Class facilitates the organization and execution of tasks in a first-in, first-out (FIFO) manner. This means that tasks are processed in the order they are added to the queue, ensuring fairness and predictability in task execution.

  • Class namespace: \Luminova\Utils\Queue

Eexamples

Initialize a new Queue instance.

$queue = new Queue();

Enqueueing tasks directly during initialization.

$initialTasks = [
    function() {
        // Task 1: Perform another operation
    },
    "Task 2"
];
$queue = new Queue($initialTasks);

Define tasks as closures and enqueue them.

$queue->push(function() {
    // Task 1: Perform some operation
});

Alternatively, enqueue a string task.

$queue->push("Task 2");

You can enqueue any callable item.

$queue->push([$object, 'methodName']);

Run the queue and execute all enqueued tasks.

$queue->run();

Remove a specific task from the queue.

$queue->remove("Task 3");

// Remove a task by index
$queue->remove($initialTasks[1]);

Get the current task and create a new instance.

$current = $queue->current();

Get the next task and create a new instance.

$next = $queue->next();

Get the last task and create a new instance.

$lastTask = $queue->last();

Methods

constructor

public __construct(array|null $jobs = null): mixed

Parameters:

ParameterTypeDescription
$jobsarray|nullArray of jobs to initialize the queue.

push

Push a closure or item to the queue.

public push(\Closure|string|callable $item): void

Parameters:

ParameterTypeDescription
$item\Closure|string|callableThe item to enqueue.

run

Run the queue by executing all jobs.

public run(callable|null $callback = null): void

And execute a callback function

Parameters:

ParameterTypeDescription
$callbackcallable|nullOptional Callback function to execute after running the queue.

hasQueue

Check if the queue has registered callable jobs.

public hasQueue(): bool

Return Value:

bool - True if the queue has registered callable jobs.


size

Get the size of the queue.

public size(): int

Return Value:

boo - The size of the queue.


delete

Delete the queue.

public delete(): void

remove

Remove an job from the queue.

public remove(mixed $job): void

Parameters:

ParameterTypeDescription
$jobmixedThe job name to remove.

free

Free resources

public free(): void

getInstance

Get the current job from queue and return a new instance.

public getInstance(int $index): \Luminova\Utils\Queue

Parameters:

ParameterTypeDescription
$indexintCurrent job index.

Return Value:

Queue - A new Queue instance.


current

Get the current job from queue and return a new instance.

public current(): \Luminova\Utils\Queue

Return Value:

Queue - A new Queue instance.


next

Get the next job from queue and return a new instance.

public next(): \Luminova\Utils\Queue

Return Value:

Queue - A new Queue instance.


last

Get the last job from queue and return a new instance.

public last(): \Luminova\Utils\Queue

Return Value:

Queue - A new Queue instance.