fyre/queue

A Queue library.

v2.0.11 2024-06-29 06:55 UTC

This package is auto-updated.

Last update: 2024-08-29 07:23:46 UTC


README

FyreQueue is a free, open-source queue library for PHP.

Table Of Contents

Installation

Using Composer

composer require fyre/queue

In PHP:

use Fyre\Queue\QueueManager;

Methods

Clear

Clear all instances and configs.

QueueManager::clear();

Get Config

Set a Queue config.

  • $key is a string representing the Queue key.
$config = QueueManager::getConfig($key);

Alternatively, if the $key argument is omitted an array containing all configurations will be returned.

$config = QueueManager::getConfig();

Get Key

Get the key for a Queue instance.

$key = QueueManager::getKey($queue);

Has Config

Check if a Queue config exists.

  • $key is a string representing the Queue key, and will default to QueueManager::DEFAULT.
$hasConfig = QueueManager::hasConfig($key);

Is Loaded

Check if a Queue instance is loaded.

  • $key is a string representing the Queue key, and will default to QueueManager::DEFAULT.
$isLoaded = QueueManager::isLoaded($key);

Load

Load a Queue.

  • $options is an array containing configuration options.
$queue = QueueManager::load($options);

Push

Push a job to a Queue.

  • $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.
    • config is a string representing the configuration key, and will default to "default".
    • queue is a string representing the Queue name, and will default to "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.
QueueManager::push($className, $arguments, $options);

Set Config

Set the Queue config.

  • $key is a string representing the Queue key.
  • $options is an array containing configuration options.
QueueManager::setConfig($key, $options);

Alternatively, a single array can be provided containing key/value of configuration options.

QueueManager::setConfig($config);

Unload

Unload a Queue.

  • $key is a string representing the Queue key, and will default to QueueManager::DEFAULT.
$unloaded = QueueManager::unload($key);

Use

Load a shared Queue instance.

  • $key is a string representing the Queue key, and will default to "default".
$queue = QueueManager::use($key);

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.
$queue->clear($queueName);

Get Listener

Get the queue Listener.

$queue->getListener();

Pop

Pop the last item off the queue.

  • $queueName is a string representing the queue name.
$message = $queue->pop($queueName);

Push

Push an item onto the queue.

  • $queueName is a string representing the queue name.
  • $message is a Message.
$queue->push($queueName, $message);

Redis

The Redis queue can be loaded using custom configuration.

  • $key is a string representing the queue key.
  • $options is an array containing configuration options.
    • className must be set to \Fyre\Queue\Handlers\RedisQueue.
    • listener is a string representing the Listener class, and will default to \Fyre\Queue\Listener.
    • 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.
QueueManager::setConfig($key, $options);

$queue = QueueManager::use($key);

Workers

Workers are long running tasks that will consume and execute jobs from the queue.

use Fyre\Queue\Worker;
  • $options is an array containing configuration options.
    • config is a string representing the configuration key, and will default to "default".
    • queue is a string representing the queue name, and will default to "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.
$worker = new Worker($options);

Run

Run the worker.

$worker->run();

Listeners

You can use a specific listener by specifying the listener option of the $options variable above.

Custom listener can be created by extending \Fyre\Queue\Listener, ensuring all below methods are implemented.

Exception

Handle a message exception.

  • $message is the Message.
  • $exception is the exception.
$listener->exception($message, $exception);

Failure

Handle a failed message.

$listener->failure($message);

Invalid

Handle an invalid message.

$listener->invalid($message);

Start

Handle a start message.

$listener->start($message);

Success

Handle a success message.

$listener->success($message);

Messages

Messages are used internally to pass data between the Queue, Worker and Listener.

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 "default".
    • queue is a string representing the queue name, and will default to "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.
$message = new Message($options);

Get Arguments

Get the message arguments.

$arguments = $message->getArguments();

Get Callback

Get the message callback.

$callback = $message->getCallback();

Get Config

Get the message config.

$config = $message->getConfig();

Get Hash

Get the message hash.

$hash = $message->getHash();

Is Expired

Determine if the message has expired.

$isExpired = $message->isExpired();

Is Ready

Determine if the message is ready.

$isReady = $message->isReady();

Is Unique

Determine if the message is unique.

$isUnique = $message->isUnique();

Is Valid

Determine if the message is valid.

$isValid = $message->isValid();