Skip to content
This repository was archived by the owner on Oct 24, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,21 @@ use Altmetric\ReliableQueue;
use Altmetric\ChunkedReliableQueue;
use Altmetric\PriorityReliableQueue;

$queue = new ReliableQueue('unique-worker-name', 'to-do-queue', $redis, $logger);
$queue = new ReliableQueue('unique-worker-name', 'to-do-queue', $redis);
$queue[] = 'some-work';
$queue[] = 'some-more-work';

foreach ($queue as $work) {
// Perform some action on each piece of work in the to-do-queue
}

$queue = new ChunkedReliableQueue('unique-worker-name', 100, 'to-do-queue', $redis, $logger);
$queue = new ChunkedReliableQueue('unique-worker-name', 100, 'to-do-queue', $redis);

foreach ($queue as $chunk) {
// $chunk will be an array of up to 100 pieces of work
}

$queue = new PriorityReliableQueue('unique-worker-name', array('critical-queue', 'default-queue', 'low-priority-queue'), $redis, $logger);
$queue = new PriorityReliableQueue('unique-worker-name', array('critical-queue', 'default-queue', 'low-priority-queue'), $redis);

foreach ($queue as $name => $work) {
// $work will be popped from the queue $name in the priority order given
Expand All @@ -42,7 +42,7 @@ foreach ($queue as $name => $work) {

## API Documentation

### `public ReliableQueue::__construct(string $name, string $queue, Redis $redis, LoggerInterface $logger)`
### `public ReliableQueue::__construct(string $name, string $queue, Redis $redis[, LoggerInterface $logger])`

```php
$queue = new \Altmetric\ReliableQueue('unique-worker-name', 'to-do-queue', $redis, $logger);
Expand All @@ -55,7 +55,7 @@ Instantiate a new reliable queue object with the following arguments:
* `$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
* `$logger`: an optional
[`Psr\Log\LoggerInterface`](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md)-compliant
logger.

Expand Down Expand Up @@ -86,7 +86,7 @@ $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)`
### `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);
Expand All @@ -100,7 +100,7 @@ Instantiate a new chunked, reliable queue object with the following arguments:
* `$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
* `$logger`: an optional
[`Psr\Log\LoggerInterface`](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md)-compliant
logger.

Expand All @@ -118,7 +118,7 @@ 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)`
### `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);
Expand All @@ -129,7 +129,7 @@ Instantiate a new priority-ordered, reliable queue object with the following arg
* `$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
* `$logger`: an optional
[`Psr\Log\LoggerInterface`](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md)-compliant
logger.

Expand Down
10 changes: 8 additions & 2 deletions src/ChunkedReliableQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use Redis;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;

class ChunkedReliableQueue implements \Iterator
{
Expand All @@ -15,15 +16,20 @@ class ChunkedReliableQueue implements \Iterator
private $logger;
private $value;

public function __construct($name, $size, $queue, Redis $redis, LoggerInterface $logger)
public function __construct($name, $size, $queue, Redis $redis, LoggerInterface $logger = null)
{
$this->name = $name;
$this->size = $size;
$this->queue = $queue;
$this->redis = $redis;
$this->logger = $logger;
$this->workingQueue = "{$queue}.working_on.{$name}";
$this->reliableQueue = new ReliableQueue($name, $queue, $redis, $logger);

if ($logger === null) {
$this->logger = new NullLogger();
} else {
$this->logger = $logger;
}
}

public function rewind()
Expand Down
10 changes: 8 additions & 2 deletions src/PriorityReliableQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use Redis;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;

class PriorityReliableQueue implements \Iterator
{
Expand All @@ -15,12 +16,17 @@ class PriorityReliableQueue implements \Iterator
private $workingQueues;
private $workingQueue;

public function __construct($name, array $queues, Redis $redis, LoggerInterface $logger)
public function __construct($name, array $queues, Redis $redis, LoggerInterface $logger = null)
{
$this->name = $name;
$this->queues = $queues;
$this->redis = $redis;
$this->logger = $logger;

if ($logger === null) {
$this->logger = new NullLogger();
} else {
$this->logger = $logger;
}

$numberOfQueues = count($queues);
foreach ($queues as $index => $queue) {
Expand Down
10 changes: 8 additions & 2 deletions src/ReliableQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use Redis;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;

class ReliableQueue implements \Iterator, \ArrayAccess
{
Expand All @@ -13,13 +14,18 @@ class ReliableQueue implements \Iterator, \ArrayAccess
private $logger;
private $value;

public function __construct($name, $queue, Redis $redis, LoggerInterface $logger)
public function __construct($name, $queue, Redis $redis, LoggerInterface $logger = null)
{
$this->name = $name;
$this->queue = $queue;
$this->redis = $redis;
$this->logger = $logger;
$this->workingQueue = "{$queue}.working_on.{$name}";

if ($logger === null) {
$this->logger = new NullLogger();
} else {
$this->logger = $logger;
}
}

public function rewind()
Expand Down