https://github.com/elusivecodes/fyrequeue
FyreQueue is a free, open-source queue library for PHP.
https://github.com/elusivecodes/fyrequeue
php queue redis worker
Last synced: 10 months ago
JSON representation
FyreQueue is a free, open-source queue library for PHP.
- Host: GitHub
- URL: https://github.com/elusivecodes/fyrequeue
- Owner: elusivecodes
- License: mit
- Created: 2022-06-08T10:01:49.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2024-11-17T00:55:37.000Z (over 1 year ago)
- Last Synced: 2024-11-17T01:47:53.434Z (over 1 year ago)
- Topics: php, queue, redis, worker
- Language: PHP
- Homepage:
- Size: 116 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# FyreQueue
**FyreQueue** is a free, open-source queue library for *PHP*.
## Table Of Contents
- [Installation](#installation)
- [Basic Usage](#basic-usage)
- [Methods](#methods)
- [Queues](#queues)
- [Redis](#redis)
- [Workers](#workers)
- [Messages](#messages)
- [Commands](#commands)
- [Stats](#stats)
- [Worker](#worker)
## Installation
**Using Composer**
```
composer require fyre/queue
```
In PHP:
```php
use Fyre\Queue\QueueManager;
```
## Basic Usage
- `$container` is a [*Container*](https://github.com/elusivecodes/FyreContainer).
- `$config` is a [*Config*](https://github.com/elusivecodes/FyreConfig).
```php
$queueManager = new QueueManager($container, $config);
```
Default configuration options will be resolved from the "*Queue*" key in the [*Config*](https://github.com/elusivecodes/FyreConfig).
**Autoloading**
It is recommended to bind the *QueueManager* to the [*Container*](https://github.com/elusivecodes/FyreContainer) as a singleton.
```php
$container->singleton(QueueManager::class);
```
Any dependencies will be injected automatically when loading from the [*Container*](https://github.com/elusivecodes/FyreContainer).
```php
$queueManager = $container->use(QueueManager::class);
```
## Methods
**Build**
Build a [*Queue*](#queues).
- `$options` is an array containing configuration options.
```php
$queue = $queueManager->build($options);
```
[*Queue*](#queues) dependencies will be resolved automatically from the [*Container*](https://github.com/elusivecodes/FyreContainer).
**Clear**
Clear all instances and configs.
```php
$queueManager->clear();
```
**Get Config**
Set a [*Queue*](#queues) config.
- `$key` is a string representing the [*Queue*](#queues) key.
```php
$config = $queueManager->getConfig($key);
```
Alternatively, if the `$key` argument is omitted an array containing all configurations will be returned.
```php
$config = $queueManager->getConfig();
```
**Has Config**
Determine whether a [*Queue*](#queues) config exists.
- `$key` is a string representing the [*Queue*](#queues) key, and will default to `QueueManager::DEFAULT`.
```php
$hasConfig = $queueManager->hasConfig($key);
```
**Is Loaded**
Determine whether a [*Queue*](#queues) instance is loaded.
- `$key` is a string representing the [*Queue*](#queues) key, and will default to `QueueManager::DEFAULT`.
```php
$isLoaded = $queueManager->isLoaded($key);
```
**Push**
Push a job to a [*Queue*](#queues).
- `$className` is a string representing the job class.
- `$arguments` is an array containing arguments that will be passed to the job.
- `$options` is an array containing options for the [*Message*](#messages).
- `config` is a string representing the configuration key, and will default to `QueueManager::DEFAULT`.
- `queue` is a string representing the [queue](#queues) name, and will default to `QueueManager::DEFAULT`.
- `method` is a string representing the class method, and will default to "*run*".
- `delay` is a number representing the number of seconds before the job should run, and will default to *0*.
- `expires` is a number representing the number of seconds after which the job will expire, and will default to *0*.
- `retry` is a boolean indicating whether the job should be retried if it fails, and will default to *true*.
- `maxRetries` is a number indicating the maximum number of times the job should be retried, and will default to *5*.
- `unique` is a boolean indicating whether the job should be unique, and will default to *false*.
```php
$queueManager->push($className, $arguments, $options);
```
Job dependencies will be resolved automatically from the [*Container*](https://github.com/elusivecodes/FyreContainer).
**Set Config**
Set the [*Queue*](#queues) config.
- `$key` is a string representing the [*Queue*](#queues) key.
- `$options` is an array containing configuration options.
```php
$queueManager->setConfig($key, $options);
```
**Unload**
Unload a [*Queue*](#queues).
- `$key` is a string representing the [*Queue*](#queues) key, and will default to `QueueManager::DEFAULT`.
```php
$queueManager->unload($key);
```
**Use**
Load a shared [*Queue*](#queues) instance.
- `$key` is a string representing the [*Queue*](#queues) key, and will default to *"default"*.
```php
$queue = $queueManager->use($key);
```
[*Queue*](#queues) dependencies will be resolved automatically from the [*Container*](https://github.com/elusivecodes/FyreContainer).
## Queues
You can load a specific queue by specifying the `className` option of the `$options` variable above.
Custom queues can be created by extending `\Fyre\Queue\Queue`, ensuring all below methods are implemented.
**Clear**
Clear all items from the queue.
- `$queueName` is a string representing the queue name, and will default to `QueueManager::DEFAULT`.
```php
$queue->clear($queueName);
```
**Complete**
Mark a job as completed.
- `$message` is a [*Message*](#message).
```php
$queue->complete($message);
```
**Fail**
Mark a job as failed.
- `$message` is a [*Message*](#message).
```php
$queue->fail($message);
```
**Pop**
Pop the last item off the queue.
- `$queueName` is a string representing the queue name.
```php
$message = $queue->pop($queueName);
```
**Push**
Push a job onto the queue.
- `$message` is a [*Message*](#message).
```php
$queue->push($message);
```
**Queues**
Get all the active queues.
```php
$queues = $queue->queues();
```
**Reset**
Reset the queue statistics.
- `$queueName` is a string representing the queue name, and will default to `QueueManager::DEFAULT`.
```php
$queue->reset($queueName);
```
**Stats**
Get the statistics for a queue.
- `$queueName` is a string representing the queue name, and will default to `QueueManager::DEFAULT`.
```php
$stats = $queue->stats($queueName);
```
### Redis
The Redis queue can be loaded using custom configuration.
- `$options` is an array containing configuration options.
- `className` must be set to `\Fyre\Queue\Handlers\RedisQueue`.
- `host` is a string representing the Redis host, and will default to "*127.0.0.1*".
- `password` is a string representing the Redis password
- `port` is a number indicating the Redis port, and will default to *6379*.
- `database` is a string representing the Redis database.
- `timeout` is a number indicating the connection timeout.
- `persist` is a boolean indicating whether to use a persistent connection, and will default to *true*.
- `tls` is a boolean indicating whether to use a tls connection, and will default to *true*.
- `ssl` is an array containing SSL options.
- `key` is a string representing the path to the key file.
- `cert` is a string representing the path to the certificate file.
- `ca` is a string representing the path to the certificate authority file.
```php
$container->use(Config::class)->set('Queue.redis', $options);
```
## Workers
Workers are long running tasks that will consume and execute jobs from the queue.
```php
use Fyre\Queue\Worker;
```
- `$container` is a [*Container*](https://github.com/elusivecodes/FyreContainer).
- `$queueManager` is a *QueueManager*.
- `$eventManager` is an [*EventManager*](https://github.com/elusivecodes/FyreEvent).
- `$options` is an array containing configuration options.
- `config` is a string representing the configuration key, and will default to `QueueManager::DEFAULT`.
- `queue` is a string representing the queue name, and will default to `QueueManager::DEFAULT`.
- `maxJobs` is a number representing the maximum number of jobs to execute, and will default to *0*.
- `maxRuntime` is a number representing the maximum number of seconds the worker should run, and will default to *0*.
- `rest` is a number representing the number of microseconds to rest after processing a job, and will default to *10000*.
- `sleep` is a number representing the number of microseconds to sleep if no jobs are in the queue, and will default to *100000*.
```php
$worker = new Worker($container, $queueManager, $eventManager, $options);
```
**Run**
Run the worker.
```php
$worker->run();
```
## Messages
Messages are used internally to pass data between the [*Queue*](#queues) and [*Worker*](#workers).
```php
use Fyre\Queue\Message;
```
- `$options` is an array containing options for the message.
- `className` is a string representing the job class.
- `arguments` is an array containing arguments that will be passed to the job.
- `config` is a string representing the configuration key, and will default to `QueueManager::DEFAULT`.
- `queue` is a string representing the queue name, and will default to `QueueManager::DEFAULT`.
- `method` is a string representing the class method, and will default to "*run*".
- `delay` is a number representing the number of seconds before the job should run, and will default to *0*.
- `expires` is a number representing the number of seconds after which the job will expire, and will default to *0*.
- `retry` is a boolean indicating whether the job should be retried if it fails, and will default to *true*.
- `maxRetries` is a number indicating the maximum number of times the job should be retried, and will default to *5*.
- `unique` is a boolean indicating whether the job should be unique, and will default to *false*.
```php
$message = new Message($options);
```
**Get After**
Get the timestamp when the message can be sent.
```php
$after = $message->getTimestamp();
```
**Get Config**
Get the message config.
```php
$config = $message->getConfig();
```
**Get Hash**
Get the message hash.
```php
$hash = $message->getHash();
```
**Get Queue**
Get the message queue.
```php
$queueName = $message->getQueue();
```
**Is Expired**
Determine whether the message has expired.
```php
$isExpired = $message->isExpired();
```
**Is Ready**
Determine whether the message is ready.
```php
$isReady = $message->isReady();
```
**Is Unique**
Determine whether the message is unique.
```php
$isUnique = $message->isUnique();
```
**Is Valid**
Determine whether the message is valid.
```php
$isValid = $message->isValid();
```
**Should Retry**
Determine whether the message should be retried.
```php
$shouldretry = $message->shouldRetry();
```
## Commands
### Stats
Display stats for the queue.
- `--config` is a the configuration key, and will default to `QueueManager::DEFAULT`.
- `--queue` is a the [queue](#queues) name, and will default to `QueueManager::DEFAULT`.
```php
$commandRunner->run('queue:stats', ['--config', 'default', '--queue', 'default']);
```
### Worker
Start a background queue worker.
- `--config` is a the configuration key, and will default to `QueueManager::DEFAULT`.
- `--queue` is a the [queue](#queues) name, and will default to `QueueManager::DEFAULT`.
- `--max-jobs` is the maximum number of jobs to execute, and will default to *0*.
- `--max-runtime` is the maximum number of seconds the worker should run, and will default to *0*.
```php
$commandRunner->run('queue:worker', ['--config', 'default', '--queue', 'default', '--max-jobs', '99', '--max-runtime', '60']);
```