https://github.com/altmetric/reliable-queue
A PHP library for reliable queueing backed by Redis
https://github.com/altmetric/reliable-queue
queueing redis
Last synced: 13 days ago
JSON representation
A PHP library for reliable queueing backed by Redis
- Host: GitHub
- URL: https://github.com/altmetric/reliable-queue
- Owner: altmetric
- License: mit
- Archived: true
- Created: 2016-09-05T20:07:19.000Z (over 9 years ago)
- Default Branch: main
- Last Pushed: 2023-09-25T10:04:21.000Z (over 2 years ago)
- Last Synced: 2025-10-11T05:41:41.999Z (4 months ago)
- Topics: queueing, redis
- Language: PHP
- Homepage: https://packagist.org/packages/altmetric/reliable-queue
- Size: 25.4 KB
- Stars: 2
- Watchers: 1
- Forks: 2
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Reliable Queue [](https://travis-ci.org/altmetric/reliable-queue)
A PHP library for reliable queueing backed by [Redis](http://redis.io/).
**Current version:** 0.4.0
**Supported PHP versions:** 5.3, 5.4, 5.5, 5.6, 7
## Installation
```shell
$ composer require altmetric/reliable-queue
```
## Usage
```php
$work) {
// $work will be popped from the queue $name in the priority order given
}
```
## API Documentation
### `public ReliableQueue::__construct(string $name, string $queue, Redis $redis, LoggerInterface $logger)`
```php
$queue = new \Altmetric\ReliableQueue('unique-worker-name', 'to-do-queue', $redis, $logger);
```
Instantiate a new reliable queue object with the following arguments:
* `$name`: a unique `string` name for this worker so that we can pick up any
unfinished work in the event of a crash;
* `$queue`: the `string` key of the list in Redis to use as the queue;
* `$redis`: a [`Redis`](https://github.com/phpredis/phpredis) client object for
communication with Redis;
* `$logger`: a
[`Psr\Log\LoggerInterface`](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md)-compliant
logger.
The returned object implements both the
[`Iterator`](http://php.net/manual/en/class.iterator.php) (and therefore
[`Traversable`](http://php.net/manual/en/class.traversable.php)) and
[`ArrayAccess`](http://php.net/manual/en/class.arrayaccess.php) interface in
PHP.
This means that it can be iterated over with `foreach`, yielding the queue name
and a value on every iteration. Internally, the library will block for new work
but this is invisible from a client's perspective.
```php
foreach ($queue as $key => $work) {
// $key will be the queue key name in Redis
// $work will be the value popped from the queue
}
```
You can also modify the queue as if it were an array by using the typical array
operations:
```php
$queue[] = 'work'; // enqueues work
$queue[1]; // returns work at index 1 if it exists
$queue[1] = 'work'; // sets work to index 1 in the queue
unset($queue[1]); // remove work at index 1 from the queue
```
### `public ChunkedReliableQueue::__construct(string $name, int $size, string $queue, Redis $redis, LoggerInterface $logger)`
```php
$queue = new \Altmetric\ChunkedReliableQueue('unique-worker-name', 100, 'to-do-queue', $redis, $logger);
```
Instantiate a new chunked, reliable queue object with the following arguments:
* `$name`: a unique `string` name for this worker so that we can pick up any
unfinished work in the event of a crash;
* `$size`: an integer maximum size of chunk to return on each iteration;
* `$queue`: the `string` key of the list in Redis to use as the queue;
* `$redis`: a [`Redis`](https://github.com/phpredis/phpredis) client object for
communication with Redis;
* `$logger`: a
[`Psr\Log\LoggerInterface`](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md)-compliant
logger.
The returned object implements the
[`Iterator`](http://php.net/manual/en/class.iterator.php) (and therefore
[`Traversable`](http://php.net/manual/en/class.traversable.php)) interface in
PHP.
This means that it can be iterated over with `foreach`, yielding the queue name
and an array of up to `$size` elements on every iteration. Internally, the
library will block for new work but this is invisible from a client's
perspective.
If the queue contains sufficient items, the chunk of work will contain at most
`$size` elements but if there is not enough work, it may return less (but
always at least 1 value).
### `public PriorityReliableQueue:__construct(string $name, array $queues, Redis $redis, LoggerInterface $logger)`
```php
$queue = new \Altmetric\PriorityReliableQueue('unique-worker-name', array('critical-queue', 'default-queue', 'low-priority-queue'), $redis, $logger);
```
Instantiate a new priority-ordered, reliable queue object with the following arguments:
* `$name`: a unique `string` name for this worker so that we can pick up any unfinished work in the event of a crash;
* `$queues`: an `array` of `string` keys of lists in Redis to use as queues given in priority order;
* `$redis`: a [`Redis`](https://github.com/phpredis/phpredis) client object for communication with Redis;
* `$logger`: a
[`Psr\Log\LoggerInterface`](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md)-compliant
logger.
The returned object implements the
[`Iterator`](http://php.net/manual/en/class.iterator.php) (and therefore
[`Traversable`](http://php.net/manual/en/class.traversable.php)) interface in
PHP.
This means that it can be iterated over with `foreach`, yielding the queue name
and a value on every iteration. Queues will be checked randomly for work based
on their priority order given in `$queues` meaning that the first queue will be
checked more often than the second, the second more than the third and so on.
Internally, the library will repeatedly poll for new work but this is invisible
from a client's perspective.
```php
foreach ($queue as $key => $work) {
// $key will be the queue key name in Redis
// $work will be the value popped from the queue
}
```
## References
* [Pattern: Reliable queue](http://redis.io/commands/rpoplpush#pattern-reliable-queue)
## Acknowledgements
* Thanks to [James Adam](https://github.com/lazyatom) for suggesting a way to
test the randomness of the priority queue.
## License
Copyright © 2016-2017 Altmetric LLP
Distributed under the MIT License.