PHPackages                             mirays/codeigniter-queue-worker - 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. mirays/codeigniter-queue-worker

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

mirays/codeigniter-queue-worker
===============================

CodeIgniter 3 Queue Worker Management Controller

2.0.1(2y ago)011MITPHPPHP &gt;=7.4

Since Sep 12Pushed 2y agoCompare

[ Source](https://github.com/MirayS/codeigniter-queue-worker)[ Packagist](https://packagist.org/packages/mirays/codeigniter-queue-worker)[ Docs](https://github.com/yidas/codeigniter-queue-worker)[ RSS](/packages/mirays-codeigniter-queue-worker/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (2)DependenciesVersions (3)Used By (0)

 [ ![](https://camo.githubusercontent.com/6be7ce837dfe93166c2ad6ca8e0b9b03998c76c0bb55bac4c64b0f9c92e36b0b/68747470733a2f2f75706c6f61642e77696b696d656469612e6f72672f77696b6970656469612f7a682f372f37632f436f646549676e697465722e706e67) ](https://codeigniter.com/userguide3/)

CodeIgniter Queue Worker
========================

[](#codeigniter-queue-worker)

CodeIgniter 3 Daemon Queue Worker Management Controller

[![License](https://camo.githubusercontent.com/d2728ee76886f402eef2b465adb98946914c7b65ce5829368b7fb385e1d4c5fe/68747470733a2f2f706f7365722e707567782e6f72672f79696461732f636f646569676e697465722d71756575652d776f726b65722f6c6963656e73653f666f726d61743d666c61742d737175617265)](https://packagist.org/packages/yidas/codeigniter-queue-worker)

This Queue Worker extension is collected into [yidas/codeigniter-pack](https://github.com/yidas/codeigniter-pack) which is a complete solution for Codeigniter framework.

> This library only provides worker controller, you need to implement your own queue driver with handler/process in it.

Features
--------

[](#features)

- ***Multi-Processing** implementation on native PHP-CLI*
- ***Dynamically Workers dispatching (Daemon)** management*
- ***Running in background permanently** without extra libraries*
- ***Process Uniqueness Guarantee** feature by Launcher*

---

OUTLINE
-------

[](#outline)

- [Demonstration](#demonstration)
- [Introduction](#introduction)
- [Requirements](#requirements)
- [Installation](#installation)
- [Configuration](#configuration)
    - [How to Design a Worker](#how-to-design-a-worker)
        - [1. Build Initializer](#1-build-initializer)
        - [2. Build Worker](#2-build-worker)
        - [3. Build Listener](#3-build-listener)
    - [Porperties Setting](#porperties-setting)
        - [Public Properties](#public-properties)
- [Usage](#usage)
    - [Running Queue Worker](#running-queue-worker)
        - [Worker](#worker)
        - [Listener](#listener)
    - [Running in Background](#running-in-background)
        - [Launcher](#launcher)
        - [Process Status](#process-status)

---

DEMONSTRATION
-------------

[](#demonstration)

Running a listener (Daemon) with 2~5 workers setting added per 3 seconds:

```
$ php index.php job_controller/listen
2018-10-06 14:36:28 - Queue Listener - Job detect
2018-10-06 14:36:28 - Queue Listener - Start dispatch
2018-10-06 14:36:28 - Queue Listener - Dispatch Worker #1 (PID: 13254)
2018-10-06 14:36:28 - Queue Listener - Dispatch Worker #2 (PID: 13256)
2018-10-06 14:36:31 - Queue Listener - Dispatch Worker #3 (PID: 13266)
2018-10-06 14:36:34 - Queue Listener - Job empty
2018-10-06 14:36:34 - Queue Listener - Stop dispatch, total cost: 6.00s

```

---

INTRODUCTION
------------

[](#introduction)

This library provides a Daemon Queue Worker total solution for Codeigniter 3 framework with Multi-Processes implementation, it includes Listener (Daemon) and Worker for processing new jobs from queue. You may integrate your application queue (such as Redis) with Queue Worker Controller.

PHP is a lack of support for multithreading at the core language level, this library implements multithreading by managing multiprocessing.

For more concepts, the following diagram shows the implementation structure of this library:

[![](https://raw.githubusercontent.com/yidas/codeigniter-queue-worker/master/img/introduction-structure.png)](https://raw.githubusercontent.com/yidas/codeigniter-queue-worker/master/img/introduction-structure.png)

Listener (Daemon) could continue to run for detecting new jobs until it is manually stopped or you close your terminal. On the other hand , Worker could continue to run for processing new jobs until there is no job left, which the workers could be called by Listener.

Launcher is suitable for launching a listener process, which the running Listener process could be unique that the second launch would detect existent listener and do NOT launch again.

---

REQUIREMENTS
------------

[](#requirements)

This library requires the following:

- PHP CLI 5.4.0+
- CodeIgniter 3.0.0+

---

INSTALLATION
------------

[](#installation)

Run Composer in your Codeigniter project under the folder `\application`:

```
composer require yidas/codeigniter-queue-worker

```

Check Codeigniter `application/config/config.php`:

```
$config['composer_autoload'] = TRUE;
```

> You could customize the vendor path into `$config['composer_autoload']`

---

CONFIGURATION
-------------

[](#configuration)

First, create a controller that extends the working controller, and then use your own queue driver to design your own handler to implement the worker controller. There are common interfaces as following:

```
use yidas\queue\worker\Controller as WorkerController;

class My_worker extends WorkerController
{
    // Initializer
    protected function init() {}

    // Worker
    protected function handleWork() {}

    // Listener
    protected function handleListen() {}
}
```

These handlers are supposed to be designed for detecting the same job queue, but for different purpose. For example, if you are using Redis as message queue, Listener and Worker detect the same Redis list queue, Listener only do dispatching jobs by forking Worker, while Worker continue to takes out jobs and do the processing until job queue is empty.

### How to Design a Worker

[](#how-to-design-a-worker)

#### 1. Build Initializer

[](#1-build-initializer)

```
protected void init()
```

The `init()` method is the constructor of worker controller, it provides you with an interface for defining initializartion such as Codeigniter library loading.

*Example Code:*

```
class My_worker extends \yidas\queue\worker\Controller
{
    protected function init()
    {
        // Optional autoload (Load your own libraries or models)
        $this->load->library('myjobs');
    }
// ...
```

> As above, `myjobs` library is defined by your own application which handles your job processes. [Example code of myjobs with Redis](https://github.com/yidas/codeigniter-queue-worker/blob/master/examples/myjobs/MyjobsWithRedis.php)

#### 2. Build Worker

[](#2-build-worker)

```
protected boolean handleWork(object $static=null)
```

The `handleWork()` method is a processor for Worker that continue to take out jobs and do the processing. When this method returns `false`, that means the job queue is empty and the worker will close itself.

*Example Code:*

```
class My_worker extends \yidas\queue\worker\Controller
{
    protected function handleWork()
    {
        // Your own method to get a job from your queue in the application
        $job = $this->myjobs->popJob();

        // return `false` for job not found, which would close the worker itself.
        if (!$job)
            return false;

        // Your own method to process a job
        $this->myjobs->processJob($job);

        // return `true` for job existing, which would keep handling.
        return true;
    }
// ...
```

#### 3. Build Listener

[](#3-build-listener)

```
protected boolean handleListen(object $static=null)
```

The `handleListen()` method is a processor for Listener that dispatches workers to handle jobs while it detects new job by returning `true`. When this method returns `false`, that means the job queue is empty and the listener will stop dispatching.

*Example Code:*

```
class My_worker extends \yidas\queue\worker\Controller
{
    protected function handleListen()
    {
        // Your own method to detect job existence
        // return `true` for job existing, which leads to dispatch worker(s).
        // return `false` for job not found, which would keep detecting new job
        return $this->myjobs->exists();
    }
// ...
```

### Porperties Setting

[](#porperties-setting)

You could customize your worker by defining properties.

```
use yidas\queue\worker\Controller as WorkerController;

class My_worker extends WorkerController
{
    // Setting for that a listener could fork up to 10 workers
    public $workerMaxNum = 10;

    // Enable text log writen into specified file for listener and worker
    public $logPath = 'tmp/my-worker.log';
}
```

#### Public Properties

[](#public-properties)

PropertyTypeDeafultDescription$debugbooleantrueDebug mode$logPathstringnullLog file path$phpCommandstring'php'PHP CLI command for current environment$listenerSleepinteger3Time interval of listen frequency on idle$workerSleepinteger0Time interval of worker processes frequency$workerMaxNuminteger4Number of max workers$workerStartNuminteger1Number of workers at start, less than or equal to $workerMaxNum$workerWaitSecondsinteger10Waiting time between worker started and next worker starting$workerHeathCheckbooleantrueEnable worker health check for listener---

USAGE
-----

[](#usage)

There are 3 actions for usage:

- `listen` A listener (Daemon) to manage and dispatch jobs by forking workers.
- `work` A worker to process and solve jobs from queue.
- `launch` A launcher to run `listen` or `work` process in background and keep it running uniquely.

You could run above actions by using Codeigniter 3 PHP-CLI command after configuring a Queue Worker controller.

### Running Queue Worker

[](#running-queue-worker)

#### Worker

[](#worker)

To process new jobs from the queue, you could simply run Worker:

```
$ php index.php myjob/work

```

As your worker processor `handleWork()`, the worker will continue to run (return `true`) until the job queue is empty (return `false`).

#### Listener

[](#listener)

To start a listener to manage workers, you could simply run Listener:

```
$ php index.php myjob/listen

```

As your listener processor `handleListen()`, the listener will dispatch workers when detecting new jobs (return `true`) until the job queue is empty with stopping dispatching and listening for next new jobs (return `false`).

Listener manage Workers by forking each Worker into running process, it implements Multi-Processes which could dramatically improve job queue performance.

### Running in Background

[](#running-in-background)

This library supports running Listener or Worker permanently in the background, it provides you the ability to run Worker as service.

#### Launcher

[](#launcher)

To run Listener or Worker in the background, you could call Launcher to launch process:

```
$ php index.php myjob/launch

```

By default, Launcher would launch `listen` process, you could also launch `work` by giving parameter:

```
$ php index.php myjob/launch/worker

```

Launcher could keep launching process running uniquely, which prevents multiple same listeners or workers running at the same time. For example, the first time to launch a listener:

```
$ php index.php myjob/launch
Success to launch process `listen`: myjob/listen.
Called command: php /srv/ci-project/index.php myjob/listen > /dev/null &
------
USER   PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
user 14650  0.0  0.7 327144 29836 pts/3    R+   15:43   0:00 php /srv/ci-project/index.php myjob/listen
```

Then, when you launch the listener again, Launcher would prevent repeated running:

```
$ php index.php myjob/launch
Skip: Same process `listen` is running: myjob/listen.
------
USER   PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
user 14650  0.4  0.9 337764 36616 pts/3    S   15:43   0:00 php /srv/ci-project/index.php myjob/listen
```

For uniquely work scenario, you may use database as application queue, which would lead to race condition if there are multiple workers handling the same jobs. Unlike memcache list, database queue should be processed by only one worker at the same time.

#### Process Status

[](#process-status)

After launching a listener, you could check the listener service by command `ps aux|grep php`:

```
...
www-data  2278  0.7  1.0 496852 84144 ?        S    Sep25  37:29 php-fpm: pool www
www-data  3129  0.0  0.4 327252 31064 ?        S    Sep10   0:34 php /srv/ci-project/index.php myjob/listen
...
```

According to above, you could manage listener and workers such as killing listener by command `kill 3129`.

Workers would run while listener detected job, the running worker processes would also show in `ps aux|grep php`.

> Manually, you could also use an `&` (an ampersand) at the end of the listener or worker to run in the background.

###  Health Score

21

—

LowBetter than 18% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity5

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity44

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

2

Last Release

979d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/13977f5ea780190f1ae576e2ce9c2ab0b3d9331e384a34a26a9fa896b471a3ee?d=identicon)[MirayS](/maintainers/MirayS)

---

Top Contributors

[![yidas](https://avatars.githubusercontent.com/u/12604195?v=4)](https://github.com/yidas "yidas (12 commits)")

---

Tags

codeigniterqueuelistenerworkerworker-manage

### Embed Badge

![Health badge](/badges/mirays-codeigniter-queue-worker/health.svg)

```
[![Health](https://phpackages.com/badges/mirays-codeigniter-queue-worker/health.svg)](https://phpackages.com/packages/mirays-codeigniter-queue-worker)
```

###  Alternatives

[yidas/codeigniter-queue-worker

CodeIgniter 3 Queue Worker Management Controller

9665.2k2](/packages/yidas-codeigniter-queue-worker)[swarrot/swarrot

A simple lib to consume RabbitMQ queues

3654.4M8](/packages/swarrot-swarrot)[clue/mq-react

Mini Queue, the lightweight in-memory message queue to concurrently do many (but not too many) things at once, built on top of ReactPHP

144691.7k4](/packages/clue-mq-react)[tarantool/queue

PHP bindings for Tarantool Queue.

64136.2k4](/packages/tarantool-queue)[foxxmd/laravel-elasticbeanstalk-queue-worker

Deploy your Laravel application as a queue worker on AWS ElasticBeanstalk

5493.5k](/packages/foxxmd-laravel-elasticbeanstalk-queue-worker)[riverline/worker-bundle

Symfony2 Bundle to use queue with workers

1077.6k](/packages/riverline-worker-bundle)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
