PHPackages                             pinkcrab/queue - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Queues &amp; Workers](/categories/queues)
4. /
5. pinkcrab/queue

ActiveLibrary[Queues &amp; Workers](/categories/queues)

pinkcrab/queue
==============

A simple queue system for the Perique Framework, with a simple interface to allow for use with other queue systems. Comes with a built in driver for the woocommerce action scheduler

2.1.0(1y ago)1380MITPHPPHP &gt;=7.4.0CI passing

Since Sep 21Pushed 1y agoCompare

[ Source](https://github.com/Pink-Crab/Perique-Queue)[ Packagist](https://packagist.org/packages/pinkcrab/queue)[ Docs](https://pinkcrab.co.uk)[ RSS](/packages/pinkcrab-queue/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (9)Dependencies (18)Versions (14)Used By (0)

[![logo](/docs/Perique-Queue-Card.jpg "PinkCrab Perique Queue")](/docs/Perique-Queue-Card.jpg)

Perique Queue
=============

[](#perique-queue)

A queue abstraction for the PinkCrab Perique Plugin Framework. Comes with a built in Action Scheduler implementation, but can be extended to be run with anything.

[![Latest Stable Version](https://camo.githubusercontent.com/435aba283c7deb2a4b0f8d4e5a0e7dd469cc4ac73fd611f62f46a884d8c319c0/68747470733a2f2f706f7365722e707567782e6f72672f70696e6b637261622f71756575652f76)](https://packagist.org/packages/pinkcrab/queue) [![Total Downloads](https://camo.githubusercontent.com/fea2aaf577f0a0cdcf929e9b5afcf04a895cb8ec320380b3f95f6ce39cdf35cc/68747470733a2f2f706f7365722e707567782e6f72672f70696e6b637261622f71756575652f646f776e6c6f616473)](https://packagist.org/packages/pinkcrab/queue) [![Latest Unstable Version](https://camo.githubusercontent.com/6f18eeb5e83a8ad718d18c3c5d72f5c37a31a2b07f49e025399fe1e738fb6f73/68747470733a2f2f706f7365722e707567782e6f72672f70696e6b637261622f71756575652f762f756e737461626c65)](https://packagist.org/packages/pinkcrab/queue) [![License](https://camo.githubusercontent.com/c929ea9ed0a32457f48636edbaa8979e43d35a9ee0fb7499d0e65e8412fdeede/68747470733a2f2f706f7365722e707567782e6f72672f70696e6b637261622f71756575652f6c6963656e7365)](https://packagist.org/packages/pinkcrab/queue) [![PHP Version Require](https://camo.githubusercontent.com/665afb902f02063112922d054feabc0fcbda41b493c6d1fd38529948b87304b2/68747470733a2f2f706f7365722e707567782e6f72672f70696e6b637261622f71756575652f726571756972652f706870)](https://packagist.org/packages/pinkcrab/queue)[![GitHub contributors](https://camo.githubusercontent.com/1a044e82d6d7f8c2c49bae33978eec05e69aae3ce4ba3b8f4fcfb2efa2268fb8/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f636f6e7472696275746f72732f50696e6b2d437261622f506572697175652d51756575653f6c6162656c3d436f6e7472696275746f7273)](https://camo.githubusercontent.com/1a044e82d6d7f8c2c49bae33978eec05e69aae3ce4ba3b8f4fcfb2efa2268fb8/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f636f6e7472696275746f72732f50696e6b2d437261622f506572697175652d51756575653f6c6162656c3d436f6e7472696275746f7273)[![GitHub issues](https://camo.githubusercontent.com/64f95c23186442223795957c3bfd8a030099d6c9e4082753620c467ba22c612d/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6973737565732d7261772f50696e6b2d437261622f506572697175652d5175657565)](https://camo.githubusercontent.com/64f95c23186442223795957c3bfd8a030099d6c9e4082753620c467ba22c612d/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6973737565732d7261772f50696e6b2d437261622f506572697175652d5175657565)

[![WP6.3 [PHP7.4-8.2] Tests](https://github.com/Pink-Crab/Perique-Queue/actions/workflows/WP_6_3.yaml/badge.svg)](https://github.com/Pink-Crab/Perique-Queue/actions/workflows/WP_6_3.yaml)[![WP6.4 [PHP7.4-8.2] Tests](https://github.com/Pink-Crab/Perique-Queue/actions/workflows/WP_6_4.yaml/badge.svg)](https://github.com/Pink-Crab/Perique-Queue/actions/workflows/WP_6_4.yaml)[![WP6.5 [PHP7.4-8.2] Tests](https://github.com/Pink-Crab/Perique-Queue/actions/workflows/WP_6_5.yaml/badge.svg)](https://github.com/Pink-Crab/Perique-Queue/actions/workflows/WP_6_5.yaml)[![WP6.6 [PHP7.4-8.2] Tests](https://github.com/Pink-Crab/Perique-Queue/actions/workflows/WP_6_6.yaml/badge.svg)](https://github.com/Pink-Crab/Perique-Queue/actions/workflows/WP_6_6.yaml)

[![codecov](https://camo.githubusercontent.com/79e1174b3bb207256e5f73efa0d2bfada6f1d50370b1680b8c92a8138a84a725/68747470733a2f2f636f6465636f762e696f2f67682f50696e6b2d437261622f506572697175652d51756575652f6272616e63682f6d61737465722f67726170682f62616467652e7376673f746f6b656e3d3073577250444e5a4d74)](https://codecov.io/gh/Pink-Crab/Perique-Queue)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/3dc812ba218e51438946a1bb3ab6d276560906c0f8ddadff69b446db527d7bcb/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f50696e6b2d437261622f506572697175652d51756575652f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/Pink-Crab/Perique-Queue/?branch=master)[![Maintainability](https://camo.githubusercontent.com/aeb0b6960598acecca4f5371ed0965584d20d5f1c36d6e20a55329d8c9d18818/68747470733a2f2f6170692e636f6465636c696d6174652e636f6d2f76312f6261646765732f36616261663465393334643830613136333462322f6d61696e7461696e6162696c697479)](https://codeclimate.com/github/Pink-Crab/Perique-Queue/maintainability)

Why?
----

[](#why)

I needed a queue abstraction for the PinkCrab Perique Plugin Framework, and I wanted to be able to use it with the Action Scheduler, but also with a custom queue implementation. So I created this.

Installation
------------

[](#installation)

### Composer

[](#composer)

```
composer require pinkcrab/queue
```

Usage
-----

[](#usage)

### Setup Module

[](#setup-module)

As with all Perique Modules, adding `Queue` to the Application is as simple as adding it to the `App_Factory`:

```
$factory = (new App_Factory(__DIR__))
    ->module( \PinkCrab\Queue\Module\Perique_Queue::class )
    ->default_setup()
    ->boot();
```

#### Action Scheduler

[](#action-scheduler)

Out of the box Perique Queue uses the Action Scheduler to run the queue. This is the recommended way to use the queue, as it is the most reliable and performant. Nothing further is required during setup to use the Action Scheduler.

> If the site has WooCommerce or any other plugin which includes the Action Scheduler, this will be used instead of the Perique Queue version and no changes are required.

For more details on setting up the module or creating custom drivers, please [see the Module Docs](docs/queue-driver.md) for more details.

### Events

[](#events)

To add an operation to the queue, a class which implements `PinkCrab\Queue\Event\Event` must be created. To make this process a little easier, we have 3 abstract classes which can be extended to create the event.

- [`PinkCrab\Queue\Event\Async_Event`](./docs/events.md#async-event) - A simple event which will be run as soon as the queue is processed.
- [`PinkCrab\Queue\Event\Delayed_Event`](./docs/events.md#delayed-event) - A simple event which will be run after a delay.
- [`PinkCrab\Queue\Event\Recurring_Event`](./docs/events.md#recurring-event) - A simple event which will be run after a delay, and then again after a delay.

Please [see the Events Docs](./docs/events.md) for more details.

### Dispatching Events and Interacting with the Queue

[](#dispatching-events-and-interacting-with-the-queue)

The `Queue_Service` is the main class for interacting with the queue. It can be injected into any class which is created via the DI\_Container.

```
use PinkCrab\Queue\Dispatch\Queue_Service;

class My_Class {

    public function __construct( Queue_Service $queue ) {
        $this->queue = $queue;
    }

    public function dispatch_event() {
        $this->queue->dispatch( new My_Event() );
    }

    public function get_next_event() {
        $event = $this->queue->find_next( new My_Event() );
    }

    public function cancel_next_event() {
        $this->queue->cancel_next( new My_Event() );
    }

    public function is_event_pending() {
        $pending = $this->queue->is_scheduled( new My_Event() );
    }
}
```

> By injecting the `Queue_Service` as a dependency, this will allow mocking the service much easier in tests.

You can read more about the [`Queue_Service` here](./docs/queue-service.md).

### Event Listeners

[](#event-listeners)

As the Queue just trigger WordPress Actions, you can just use the standard WordPress Action hooks to listen for the events.

```
add_action( 'my_event', function( $event ) {
    // Do something with the event.
}, 10, 1 );
```

But we have a custom listener which you can use if you want to create controller like classes. As with the general concept behind Perique, these are designed to be added to be added to the registration class list, and then constructed and processed via the Registration Process and the DI\_Container.

To make use of this, you can easily extend from the `Abstract_Listener` class, and then add the class to the registration class list.

```
