h4kuna/queue

System V message queue in php.

v0.1.6 2023-09-27 11:09 UTC

This package is auto-updated.

Last update: 2024-09-30 01:17:33 UTC


README

This use native php queue msg_* functions.

Installation by composer

composer require h4kuna/queue

How to use

Base is QueueFactory.

use h4kuna\Queue;

/** @var Queue\QueueFactory $queue */
$queueFactory = new Queue\QueueFactory();

/** @var Queue\Queue $queue */
$queue = $queueFactory->create('my-queue');

$queue->producer()->send('Hello');

$queue->consumer()->receive()->message === 'Hello'

The messages can to have different types.

$queue->producer()->send('Hello', 2);
$queue->consumer()->tryReceive() === NULL // non blocking read
$queue->consumer()->receive(2)->message === 'Hello'

Or read all types

$queue->producer()->send('Hello', 2);
$queue->consumer()->receive(Queue\Config::TYPE_ALL)->message === 'Hello'

Limitations

This is not server, it can run as only instance on current computer.

Catch receive error

The queue has backup implementation by Backup.

use h4kuna\Queue;

/** @var Queue\QueueFactory $queue */
$queueFactory = new Queue\QueueFactory();

/** @var Queue\Queue $queue */
$queue = $queueFactory->create('my-queue');

$queue->restore(); // restore from backup, after restart
do {
    try {
        $message = $queue->consumer()->receive();
        // ... 
    } catch (Queue\ReceiveException $e) {
        // log error
        // stop process if code is 22
    }
} while(true);

List of queues

ipcs -q

Error codes for receive: