/queue

System V message queue in php.

Primary LanguagePHP

Queue

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: