PHPackages                             glebsky/simple-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. glebsky/simple-queue

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

glebsky/simple-queue
====================

Simple queues for your php application

1.0.0(3y ago)15MITPHPPHP ^7.0

Since Mar 27Pushed 3y ago1 watchersCompare

[ Source](https://github.com/Glebsky/SimpleQueue)[ Packagist](https://packagist.org/packages/glebsky/simple-queue)[ RSS](/packages/glebsky-simple-queue/feed)WikiDiscussions main Synced today

READMEChangelog (1)Dependencies (1)Versions (2)Used By (0)

Simple Queue
============

[](#simple-queue)

[![Simple Queue](./docs/logo.png)](./docs/logo.png)

[![Latest Stable Version](https://camo.githubusercontent.com/c757af19ea3f39b1660f86b8ad3e40019ddd940bbba1163b6e3b6e0e455e649b/68747470733a2f2f706f7365722e707567782e6f72672f676c6562736b792f73696d706c652d71756575652f76)](https://packagist.org/packages/glebsky/simple-queue)[![Total Downloads](https://camo.githubusercontent.com/272f5dcf07766d521aa2f585c0043166ab2a504891169cd789b60b42ae59588d/68747470733a2f2f706f7365722e707567782e6f72672f676c6562736b792f73696d706c652d71756575652f646f776e6c6f616473)](https://packagist.org/packages/glebsky/simple-queue)[![Stable](https://camo.githubusercontent.com/4472c1a63984d15ab305db5648ec13133cdea9eeaf5f859eb3aaafafe822cf95/68747470733a2f2f706f7365722e707567782e6f72672f676c6562736b792f73696d706c652d71756575652f762f756e737461626c65)](https://packagist.org/packages/glebsky/simple-queue)[![License](https://camo.githubusercontent.com/9ad861b30f9b7d7127b94c56faa363d2543a5bddae5a5477387d7391c85ef886/68747470733a2f2f706f7365722e707567782e6f72672f676c6562736b792f73696d706c652d71756575652f6c6963656e7365)](https://packagist.org/packages/glebsky/simple-queue)[![PHP Version](https://camo.githubusercontent.com/a14d4b7214e2f92fb45d4033b28912e3a702e362108f95d758ed4cbe1f46be86/68747470733a2f2f62616467656e2e6e65742f7061636b61676973742f7068702f676c6562736b792f73696d706c652d7175657565)](https://packagist.org/packages/glebsky/simple-queue)

Simple queues and simple queue handling for your PHP project

 [Ukrainian](./docs/READMEUA.md) | [English](README.md) | [Russian](./docs/READMERU.md)

---

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

[](#installation)

The library is installed via composer.

```
composer require glebsky/simple-queue

```

Configuration
-------------

[](#configuration)

You need to implement the `TransportInterface` interface.

You can use MySql, Redis or any other storage that suits you.

The data that is present in `Message` and your class must be based on this data.

```
$id - int
$status - int
$created_at - timestamp
$updated_at - timestamp
$attempts - int
$queue  - string
$job - string
$body - string
$priority - int
$error - string
```

> For example, you can refer to `example/DBTranspot.php` which works on PDO basis.

> You can also use the ready-made `Transport` class `Glebsky\SimpleQueue\Transports\PDOTransport` which is based on PDO and works with SQL databases.
>
> To create a table of queues in the Database, the `migrate` method is present in this class

Usage
-----

[](#usage)

#### Create a task

[](#create-a-task)

You need to create your own Job class to implement the interface `JobInterface`

- `public $queueName` - property in this class You can assign a queue to a particular job. If this property is not added, the queue will not be assigned.
- `public $priority` - property in this class You can set the priority in numbers, the higher the number, the higher the priority.

`handle` method In this class will be executed when processing the queue. This is where you put the code you need.

> An example class can be found in `example/TestJob.php`

#### Adding a task to the queue

[](#adding-a-task-to-the-queue)

The `Queue` class is used to add a task to the queue. We need to create an instance of our `TransportInterface` and connect it to the `Queue` class. Then we create our task - `TestJob` and add the task to the queue using the `dispatch`method

```
$transport = new DBTransport(); // create transport object
$queue = new Queue($transport); // add $transport to queue
$job = new TestJob('testmail@gmail.com','Subject','Message text'); //  create job
$job->queueName = 'example_queue'; // you can change queue name
$job->priority = 3; // you can change priority
$result = $queue->dispatch($job); // send job to queue
```

#### Queue Processing

[](#queue-processing)

The queue is processed using the `Worker` class.

```
$transport = new DBTransport();
$worker = new Worker($transport);
$worker->run();
```

You can create your own PHP script where tasks will be handled, and you can run this script via CLI.

```
// Worker.php
