chipslays/queue

Simple implement queue processing in PHP.

1.1.2 2021-11-17 12:59 UTC

This package is auto-updated.

Last update: 2024-10-17 20:11:48 UTC


README

Packagist Version GitHub

Simple implement queue processing in PHP.

Installation

$ composer require chipslays/queue

Usage

Client

We push something to queue.

use Chipslays\Queue\Queue;
use Chipslays\Queue\Drivers\File;

require __DIR__ . '/vendor/autoload.php';

$driver = new File([
    'storage' => __DIR__ . '/storage/',
]);

$queue = new Queue($driver);
$queue->add('payment', ['user_id' => 1, 'amount' => 10]);

Worker

We have worker, who get value from queue and starts processing.

worker.php
use Chipslays\Queue\Queue;
use Chipslays\Queue\Drivers\File;

require __DIR__ . '/vendor/autoload.php';

$driver = new File([
    'storage' => __DIR__ . '/storage/',
]);

$queue = new Queue($driver);

while (true) {
    if (!$item = $queue->next('payment')) {
        continue;
    }

    echo 'channel: ' . $item->getChannel() . PHP_EOL;
    echo 'id: ' . $item->getId() . PHP_EOL;
    echo 'data: ' . print_r($item->getData(), true) . PHP_EOL;

    // also can be getting by magic getter: $item->id, $item->channel, $item->data
}

Cron

Or instead loop worker, we can use a cron job.

cron-worker.php
use Chipslays\Queue\Queue;
use Chipslays\Queue\Drivers\File;

require __DIR__ . '/vendor/autoload.php';

$driver = new File([
    'storage' => __DIR__ . '/storage/',
]);

$queue = new Queue($driver);

if (!$item = $queue->next('payment')) {
    exit;
}

echo 'channel: ' . $item->getChannel() . PHP_EOL;
echo 'id: ' . $item->getId() . PHP_EOL;
echo 'data: ' . print_r($item->getData(), true) . PHP_EOL;

// also can be getting by magic getter: $item->id, $item->channel, $item->data

Queue

Base class for queue manipulate.

Methods

__construct

/**
 * @param DriverInterface $driver
 */
public function __construct(DriverInterface $driver);

Flat File (FileSystem) driver:

use Chipslays\Queue\Queue;
use Chipslays\Queue\Drivers\File;

require __DIR__ . '/vendor/autoload.php';

$driver = new File([
    'storage' => __DIR__ . '/storage/',
]);

$queue = new Queue($driver);

add

/**
 * Add item to queue.
 *
 * Returns the `id` of the added item.
 *
 * @param string $channel
 * @param array $data
 * @param int $sort
 * @return string
 */
public function add(string $channel, array $data, int $sort = QUEUE_DEFAULT_SORT): string;

Example:

use Chipslays\Queue\Queue;

$queue = new Queue($driver);
$id = $queue->add('payment', ['key' => 'value']);
echo $queue->position('payment', $id); // e.g. 1

get

/**
 * Get item by ID.
 *
 * @param string $channel
 * @param string $id
 * @return Item|null
 */
public function get(string $channel, string $id): ?Item;

Example:

use Chipslays\Queue\Queue;

$queue = new Queue($driver);
$id = $queue->add('payment', ['key' => 'value']);
$item = $queue->get('payment', $id));

echo 'channel: ' . $item->getChannel() . PHP_EOL;
echo 'id: ' . $item->getId() . PHP_EOL;
echo 'data: ' . print_r($item->getData(), true) . PHP_EOL;

// also can be getting by magic getter: $item->id, $item->channel, $item->data

first

 /**
 * Get first item in queue.
 *
 * If queue is empty or `channel` not exists returns `null`.
 *
 * @param string $channel
 * @return Item|null
 */
public function first(string $channel): ?Item;

Example:

use Chipslays\Queue\Queue;

$queue = new Queue($driver);
$item = $queue->first('payment');

if (!$item) {
    return;
}

echo 'channel: ' . $item->getChannel() . PHP_EOL;
echo 'id: ' . $item->getId() . PHP_EOL;
echo 'data: ' . print_r($item->getData(), true) . PHP_EOL;

// also can be getting by magic getter: $item->id, $item->channel, $item->data

next

/**
 * Get next item in queue.
 *
 * @param string $channel
 * @return Item|null
 */
public function next(string $channel): ?Item;

Example:

use Chipslays\Queue\Queue;

$queue = new Queue($driver);

// somewhere in client code...
$queue->add('payment', ['currency' => 'EUR', 'amount' => 10]);

// somewhere in worker/cron code...
if (!$item = $queue->next('payment')) {
    return;
}

echo 'channel: ' . $item->getChannel() . PHP_EOL;
echo 'id: ' . $item->getId() . PHP_EOL;
echo 'data: ' . print_r($item->getData(), true) . PHP_EOL;

// also can be getting by magic getter: $item->id, $item->channel, $item->data

delete

/**
 * Delete item from queue.
 *
 * Returns `true` on success delete and `false` on fail.
 *
 * @param string|Item $channel e.g. Can be passed as result from `first` method.
 * @param string $id
 * @return boolean
 */
public function delete($channel, string $id = null): bool;

Example:

use Chipslays\Queue\Queue;

$queue = new Queue($driver);
$item = $queue->first('payment');

if (!$item) {
    return;
}

// Delete by pass received item from `first` method.
$queue->delete($item);

// Delete by `channel` and `id`.
$queue->delete($item->channel, $item->id);

list

/**
 * Get list of queue items.
 *
 * Returns array of `id's`, if `channel` not exists returns `null`.
 *
 * @param string $channel
 * @return array|null
 */
public function list(string $channel): ?array;

Example:

use Chipslays\Queue\Queue;

$queue = new Queue($driver);
print_r($queue->list('payment'));

// Array
//
//     [0] => item_id_1
//     [1] => item_id_2
//     [2] => item_id_3
// )

NOTE: For each driver, the name of the id may be different!

count

/**
 * Get count of items in queue.
 *
 * Returns count, if `channel` not exists returns 0.
 *
 * @param string $channel
 * @return integer
 */
public function count(string $channel): int;

Example:

use Chipslays\Queue\Queue;

$queue = new Queue($driver);
echo $queue->count('payment'); // e.g. 32

position

/**
 * Get item position in queue.
 *
 * Return position, if `channel` or `id` not exists returns 0.
 *
 * @param string $channel
 * @param string $id
 * @return int
 */
public function position(string $channel, string $id): int;

Example:

use Chipslays\Queue\Queue;

$queue = new Queue($driver);
$id = $queue->add('payment', ['key' => 'value']);
echo $queue->position('payment', $id); // e.g. 1