folded/queue

Queue job management for your web app.

v0.1.0 2020-10-11 17:32 UTC

This package is auto-updated.

Last update: 2024-10-17 16:50:34 UTC


README

Queue job management for your web app.

Build Status Maintainability TODOs

Summary

About

I created this library to be able to easily create jobs to enqueue and retrieve in a later time. I use it for tasks that I don't want to block on my PHP requests (like sending an email after an account has been created, which I can send later).

Folded is a constellation of packages to help you setting up a web app easily, using ready to plug in packages.

Features

  • Can add jobs to a queue
  • Can retrieve jobs from a queue
  • Can check if there is jobs to process in a queue
  • Can set the queue driver
    • file
  • Can set the type of job retreival
    • FIFO: First In, First Out
    • FILO: First In, Last Out

Requirements

  • PHP version >= 7.4.0
  • Composer installed

Installation

1. Install the package

In your root folder, run this command:

composer required folded/queue

2. Add the bootstrap code

As early as possible, and by making sure the code is ran before add or get jobs in a queue:

use function Folded\setQueueDriver;
use function Folded\setQueueType;

setQueueDriver("file", [
  "folder" =>
]);

setQueueType("fifo");

Check the examples for a complete list of drivers and type of queue, as well as constants to avoid writting the queue drivers and type by hand.

Examples

1. Add a job to a queue

In this example, we will add a job to a queue.

use function Folded\addJobToQueue;

addJobToQueue("account-created", [
  "email" => "john@doe.com",
]);

If you used the "file" driver, you will see a new default.job file containing your job.

If you want to organize in multiple queue identified by a special name, use the third parameter:

use function Folded\addJobToQueue;

addJobToQueue("account-created", [
  "email" => "john@doe.com",
], "emails");

If you use the "file" driver, you will see a new emails.job file named after the name of your queue, and containing your job.

2. Get a job from a queue

In this example, we will get the first job in our queue.

use function Folded\getJobFromQueue;

$job = getJobFromQueue();

echo "job type is {$job->getType()}";

var_dump($job->getPayload());

As you can see, you will get an instance of Folded\QueueJob when getting the job quuee.

By default, you retreive jobs from the "default" queue. If you use the "file" driver, the job is taken from the "default.job" queue file.

When a job is taken, it is removed from the queue.

If you need to get a job from a named queue, use the first parameter:

use function Folded\getJobFromQueue;

$job = getJobFromQueue("emails");

echo "job type is {$job->getType()}";

var_dump($job->getPayload());

For instance, if you use the "file" driver, this will take the job from the "emails.job" queue file.

3. Check if there is a job to process from a queue

In this example, we will check if there is a job to process from a queue.

use function Folded\hasJobFromQueue;

if (hasJobFromQueue()) {
  // can get the job queue
}

By default, this method will check if a job is available from the default queue. if you need to check from a named queue, use the first parameter:

use function Folded\hasJobFromQueue;

if (hasJobFromQueue("emails")) {
  // can get the job queue
}

4. Set the queue driver

In this example, we will set the queue driver.

use function Folded\setQueueDriver;

setQueueDriver("file");

Here is a list of supported queue drivers:

  • file

If you do not want to write the queue driver by hand, you can use constants instead:

use function Folded\setQueueDriver;
use const QUEUE_DRIVER_FILE;

setQueueDriver(QUEUE_DRIVER_FILE);

Here is a list of available constants:

  • QUEUE_DRIVER_FILE

5. Set the queue type

In this example, we will set the kind of job retreival.

use function Folded\setQueueType;

setQueueType("fifo");

Here is a list of supported queue types:

  • fifo: First In, First Out
  • filo: First In, Last Out

If you do not want to write the queue type by hand, you can use constants instead:

  • QUEUE_TYPE_FIFO
  • QUEUE_TYPE_FILO

So the previous code snippet becomes:

use function Folded\setQueueType;
use const Folded\QUEUE_TYPE_FIFO;

setQueueType(QUEUE_TYPE_FIFO);

Version support