Download the PHP package williamxsp/code-igniter-queue-worker without Composer
On this page you can find all versions of the php package williamxsp/code-igniter-queue-worker. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download williamxsp/code-igniter-queue-worker
More information about williamxsp/code-igniter-queue-worker
Files in williamxsp/code-igniter-queue-worker
Package code-igniter-queue-worker
Short Description Queue and Worker system for CI3. Based on yidas/codeigniter-queue-worker, it uses Symfony Process to handle the job.
License MIT
Informations about the package code-igniter-queue-worker
CodeIgniter Queue Worker
CodeIgniter 3 Daemon Queue Worker Management Controller
This Queue Worker extension is collected into 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
-
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
- Demonstration
- Introduction
- Requirements
- Installation
- Configuration
- How to Design a Worker
- 1. Build Initializer
- 2. Build Worker
- 3. Build Listener
- Porperties Setting
- Public Properties
- How to Design a Worker
- Usage
- Running Queue Worker
- Worker
- Listener
- Running in Background
- Launcher
- Process Status
- Running Queue Worker
DEMONSTRATION
Running a listener (Daemon) with 2~5 workers setting added per 3 seconds:
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:
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
This library requires the following:
- PHP CLI 5.4.0+
- CodeIgniter 3.0.0+
INSTALLATION
Run Composer in your Codeigniter project under the folder \application
:
composer require yidas/codeigniter-queue-worker
Check Codeigniter application/config/config.php
:
You could customize the vendor path into
$config['composer_autoload']
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:
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
1. Build Initializer
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:
As above,
myjobs
library is defined by your own application which handles your job processes.
2. Build Worker
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:
3. Build Listener
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:
Porperties Setting
You could customize your worker by defining properties.
Public Properties
Property | Type | Deafult | Description |
---|---|---|---|
$debug | boolean | true | Debug mode |
$logPath | string | null | Log file path |
$phpCommand | string | 'php' | PHP CLI command for current environment |
$listenerSleep | integer | 3 | Time interval of listen frequency on idle |
$workerSleep | integer | 0 | Time interval of worker processes frequency |
$workerMaxNum | integer | 5 | Number of max workers |
$workerStartNum | integer | 1 | Number of workers at start, less than or equal to $workerMaxNum |
$workerWaitSeconds | integer | 10 | Waiting time between worker started and next worker starting |
$workerHeathCheck | boolean | true | Enable worker health check for listener |
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 runlisten
orwork
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
Worker
To process new jobs from the queue, you could simply run Worker:
As your worker processor handleWork()
, the worker will continue to run (return true
) until the job queue is empty (return false
).
Listener
To start a listener to manage workers, you could simply run Listener:
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
This library supports running Listener or Worker permanently in the background, it provides you the ability to run Worker as service.
Launcher
To run Listener or Worker in the background, you could call Launcher to launch process:
By default, Launcher would launch listen
process, you could also launch work
by giving parameter:
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:
Then, when you launch the listener again, Launcher would prevent repeated running:
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
After launching a listener, you could check the listener service by command ps aux|grep php
:
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.