PHPackages                             zotlo/phalcon-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. zotlo/phalcon-queue

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

zotlo/phalcon-queue
===================

Dashboard and code-driven configuration for Phalcon 5 queues.

1.1.5(1y ago)01.2k↓76.5%2MITPHPPHP ^8.1

Since Sep 22Pushed 1mo ago3 watchersCompare

[ Source](https://github.com/Zotlo/phalcon-queue)[ Packagist](https://packagist.org/packages/zotlo/phalcon-queue)[ RSS](/packages/zotlo-phalcon-queue/feed)WikiDiscussions main Synced 2w ago

READMEChangelog (10)Dependencies (4)Versions (16)Used By (0)

Phalcon Queue
=============

[](#phalcon-queue)

`zotlo/phalcon-queue` is a queue worker library for Phalcon 5. It lets you dispatch jobs (classes or plain closures) from your application and processes them in the background with a supervisor that automatically scales worker processes up and down.

Supported storage backends:

- **MySQL**
- **Redis**
- **SQLite**

Requirements
------------

[](#requirements)

- PHP 8.1+
- Phalcon 5.x
- PHP extensions: `pdo`, `redis`, `pcntl`, `sysvmsg`, `sockets`
- POSIX operating system (Linux, macOS)

Install
-------

[](#install)

```
composer require zotlo/phalcon-queue

```

Configure
---------

[](#configure)

Register the ServiceProvider in your CLI bootstrap (`cli.php`).

```
$di->register(new Phalcon\Queue\ServiceProvider());
```

Define supervisors in your Phalcon config. It is possible to define more than one queue.

```
$di->setShared('config', function () {
    return new \Phalcon\Config\Config([
        'queues'   => [
            'adapter'     => 'mysql',    # mysql, redis, sqlite
            'dbIndex'     => 1,          # Redis database index (only redis)
            'supervisors' => [
                [
                    'queue'           => 'default', # Queue Name
                    'balance'         => 'auto',    # Balance Strategy (auto, simple)
                    'processes'       => 5,         # Maximum Process
                    'tries'           => 0,         # Job Maximum Tries (0 = unlimited)
                    'timeout'         => 90,        # Job Timeout (seconds)
                    'balanceMaxShift' => 5,         # Max processes to start/stop per scaling cycle
                    'balanceCooldown' => 3,         # Seconds between scaling checks
                    'debug'           => false      # Worker debug logging
                ],
                [
                    'queue'           => 'another-queue',
                    'balance'         => 'simple',
                    'processes'       => 5,
                    'tries'           => 0,
                    'timeout'         => 90,
                    'balanceMaxShift' => 5,
                    'balanceCooldown' => 3,
                    'debug'           => false
                ]
            ]
        ]
    ]);
});
```

Depending on the adapter, the matching service must also be registered in the DI:

- `mysql` / `sqlite`: a `db` service returning a `Phalcon\Db\Adapter\Pdo` adapter
- `redis`: a `redis` service returning a connected `\Redis` (phpredis) instance

Running Workers
---------------

[](#running-workers)

Each queue is driven by a master process that spawns and supervises its workers:

```
php cli.php Queue run default

```

The master scales the worker pool between 1 and `processes` according to the `balance` strategy:

- **auto**: scales workers based on pending/processing job counts, moving at most `balanceMaxShift` processes per `balanceCooldown` seconds.
- **simple**: drains the queue as quickly as possible.

### Graceful Exit

[](#graceful-exit)

The master handles `SIGINT`, `SIGTERM` and `SIGHUP` so that it exits gracefully: workers finish their current job before the process stops (e.g. a `C-c` keystroke in a shell, or a supervisord stop).

### Supervisord

[](#supervisord)

In production, run one master per queue under supervisord:

```
[program:phalcon-queue]
process_name = phalcon-queue
command = /usr/bin/php PHALCON_CLI_PATH/cli.php Queue run default
autostart = true
autorestart = true
user = root
stopsignal = SIGTERM
stopwaitsecs = 30
startretries = 3

[program:phalcon-queue-another]
process_name = another-queue
command = /usr/bin/php PHALCON_CLI_PATH/cli.php Queue run another-queue
autostart = true
autorestart = true
user = root
stopsignal = SIGTERM
stopwaitsecs = 30
startretries = 3

```

You must start each queue you define in the config.

Usage
-----

[](#usage)

The first step is to define and implement the **Job** to be managed.

```
