Skip to content

Commit a042e76

Browse files
committed
Prepare for version 1.0.0
1 parent 22e3f21 commit a042e76

File tree

2 files changed

+487
-77
lines changed

2 files changed

+487
-77
lines changed

README.md

Lines changed: 159 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -19,47 +19,64 @@ Features
1919

2020
- ***Multi-Processing** implementation on native PHP-CLI*
2121

22-
- *Easy way to manage **multiple workers/processes***
22+
- ***Dynamically Workers dispatching** management*
2323

24-
- *Standard Base Controller for inheritance*
24+
- ***Running in background permanently** without extra libraries*
25+
26+
- ***Process Uniqueness Guarantee** feature by Launcher*
2527

2628
---
2729

2830
OUTLINE
2931
-------
3032

3133
- [Demonstration](#demonstration)
34+
- [Introduction](#introduction)
3235
- [Requirements](#requirements)
3336
- [Installation](#installation)
3437
- [Configuration](#configuration)
3538
- [How to Design a Worker](#how-to-design-a-worker)
3639
- [1. Build Initializer](#1-build-initializer)
37-
- [2. Build Listener](#2-build-listener)
38-
- [3. Build Worker](#3-build-worker)
40+
- [2. Build Worker](#2-build-worker)
41+
- [3. Build Listener](#3-build-listener)
3942
- [Porperties Setting](#porperties-setting)
4043
- [Public Properties](#public-properties)
4144
- [Usage](#usage)
45+
- [Running Queue Worker](#running-queue-worker)
46+
- [Worker](#worker)
47+
- [Listener](#listener)
48+
- [Running in Background](#running-in-background)
49+
- [Launcher](#launcher)
50+
- [Process Status](#process-status)
4251

4352
---
4453

4554
DEMONSTRATION
4655
-------------
4756

57+
Running a listener with 2~5 workers setting added per 3 seconds:
4858

4959
```
50-
$ php ./index.php worker_controller/listener
60+
$ php index.php job_controller/listen
61+
2018-10-06 14:36:28 - Queue Listener - Job detect
62+
2018-10-06 14:36:28 - Queue Listener - Start dispatch
63+
2018-10-06 14:36:28 - Queue Listener - Dispatch Worker #1 (PID: 13254)
64+
2018-10-06 14:36:28 - Queue Listener - Dispatch Worker #2 (PID: 13256)
65+
2018-10-06 14:36:31 - Queue Listener - Dispatch Worker #3 (PID: 13266)
66+
2018-10-06 14:36:34 - Queue Listener - Job empty
67+
2018-10-06 14:36:34 - Queue Listener - Stop dispatch, total cost: 6.00s
5168
```
5269

53-
Check log:
70+
---
71+
72+
INTRODUCTION
73+
------------
74+
75+
This library provides a Queue Worker total solution for Codeigniter 3 framework, which includes Listener and Worker for processing new jobs from queue. You may integrate your application queue (such as Redis) with Queue Worker Controller.
76+
77+
Listener could continue to run for detecting new jobs until it is manually stopped or you close your terminal. On the other hand
78+
, Worker could continue to run for processing new jobs until there is no job left.
5479

55-
```
56-
Worker Manager start assignment at: 2018-09-08 17:49:15
57-
Worker #1 create at: 2018-09-08 17:49:15
58-
Worker #2 create at: 2018-09-08 17:49:25
59-
Worker #1 close at: 2018-09-08 17:49:28 | cost: 13.51s
60-
Worker #2 close at: 2018-09-08 17:49:29 | cost: 4.93s
61-
Worker Manager stop assignment at: 2018-09-08 17:49:30, total cost: 15.02s
62-
```
6380
---
6481

6582
REQUIREMENTS
@@ -91,7 +108,7 @@ $config['composer_autoload'] = TRUE;
91108
CONFIGURATION
92109
-------------
93110

94-
You need to design porcesses or set properties for your own worker inherited from this library, there are common interfaces as following:
111+
You need to design handlers for your own worker inherited from this library, there are common interfaces as following:
95112

96113
```php
97114
use yidas\queue\worker\Controller as WorkerController;
@@ -101,14 +118,16 @@ class My_worker extends WorkerController
101118
// Initializer
102119
protected function init() {}
103120

104-
// Listener
105-
protected function listenerCallback() {}
106-
107121
// Worker
108-
protected function workerCallback() {}
122+
protected function handleWork() {}
123+
124+
// Listener
125+
protected function handleListen() {}
109126
}
110127
```
111128

129+
These handlers are supposed to be designed for detecting the same job queue, but for different purpose. For example, 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.
130+
112131
### How to Design a Worker
113132

114133
#### 1. Build Initializer
@@ -117,57 +136,73 @@ class My_worker extends WorkerController
117136
protected void init()
118137
```
119138

139+
The `init()` method is the constructor of worker controller, it provides you with an interface for defining initializartion such as Codeigniter library loading.
140+
120141
*Example Code:*
121142
```php
122143
class My_worker extends \yidas\queue\worker\Controller
123144
{
124145
protected function init()
125146
{
126147
// Optional autoload
127-
$this->load->library('myqueue');
148+
$this->load->library('myjobs');
128149

129150
// Optional shared properties setting
130151
$this->static = 'static value';
131152
}
132153
// ...
133154
```
134155

135-
#### 2. Build Listener
156+
#### 2. Build Worker
136157

137158
```php
138-
protected boolean listenerCallback(object $static=null)
159+
protected boolean handleWork(object $static=null)
139160
```
140161

162+
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.
163+
141164
*Example Code:*
142165
```php
143166
class My_worker extends \yidas\queue\worker\Controller
144167
{
145-
protected function listenerCallback()
168+
protected function handleWork()
146169
{
147-
// `true` for task existing
148-
return $this->myqueue->exists();
170+
$job = $this->myjobs->popJob();
171+
172+
// `false` for job not found, which would close the worker itself.
173+
if (!$job)
174+
return false;
175+
176+
$this->myjobs->processJob($job);
177+
178+
// `true` for job existing, which would keep handling.
179+
return true;
149180
}
150181
// ...
151182
```
152183

153-
#### 3. Build Worker
184+
#### 3. Build Listener
154185

155186
```php
156-
protected boolean workerCallback(object $static=null)
187+
protected boolean handleListen(object $static=null)
157188
```
158189

190+
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.
191+
159192
*Example Code:*
160193
```php
161194
class My_worker extends \yidas\queue\worker\Controller
162195
{
163-
protected function workerCallback()
196+
protected function handleListen()
164197
{
165-
// `false` for task not found
166-
return $this->myqueue->processTask();
198+
// `true` for job existing, which leads to dispatch worker(s).
199+
// `false` for job not found, which would keep detecting new job
200+
return $this->myjobs->exists();
167201
}
168202
// ...
169203
```
170204

205+
171206
### Porperties Setting
172207

173208
You could customize your worker by defining properties.
@@ -177,11 +212,10 @@ use yidas\queue\worker\Controller as WorkerController;
177212

178213
class My_worker extends WorkerController
179214
{
180-
// Set for that a listener only create a worker
181-
// set to 1 could prevent race condition depended on your queue structure
182-
public $workerMaxNum = 1;
215+
// Setting for that a listener could fork up to 10 workers
216+
public $workerMaxNum = 10;
183217

184-
// Enable text log writen into specified file
218+
// Enable text log writen into specified file for listener and worker
185219
public $logPath = 'tmp/my-worker.log';
186220
}
187221
```
@@ -194,28 +228,112 @@ class My_worker extends WorkerController
194228
|$logPath |string |null |Log file path|
195229
|$phpCommand |string |'php' |PHP CLI command for current environment|
196230
|$listenerSleep |integer |3 |Time interval of listen frequency on idle|
197-
|$workerSleep |integer |0 |Time interval of worker processes|
231+
|$workerSleep |integer |0 |Time interval of worker processes frequency|
198232
|$workerMaxNum |integer |5 |Number of max workers|
199233
|$workerStartNum |integer |1 |Number of workers at start, less than or equal to $workerMaxNum|
200234
|$workerWaitSeconds|integer |10 |Waiting time between worker started and next worker starting|
201-
235+
|$workerHeathCheck |boolean |true |Enable worker health check for listener|
202236

203237
---
204238

205239
USAGE
206240
-----
207241

208-
After configurating a worker, this worker controller is ready to go:
242+
There are 3 actions for usage:
243+
244+
- `listen` A listener to manage and dispatch jobs by forking workers.
245+
- `work` A worker to process and solve jobs from queue.
246+
- `launch` A launcher to run `listen` or `work` process in background and keep it running uniquely.
247+
248+
You could run above actions by using Codeigniter 3 PHP-CLI command after configuring a Queue Worker controller.
249+
250+
### Running Queue Worker
251+
252+
#### Worker
253+
254+
To process new jobs from the queue, you could simply run Worker:
255+
256+
```
257+
$ php index.php myjob/work
258+
```
259+
260+
As your worker processor `handleWork()`, the worker will continue to run (return `true`) until the job queue is empty (return `false`).
261+
262+
263+
#### Listener
264+
265+
To start a listener to manage workers, you could simply run Listener:
209266

210267
```
211-
$ php ./index.php my_worker/listener
268+
$ php index.php myjob/listen
212269
```
213270

214-
Listener would continuously process listener callback funciton, it would assign works by forking workers while the callback return `true` which means that there has task(s) detected.
271+
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`).
272+
273+
Listener manage Workers by forking each Worker into running process, it implements Multi-Processes which could dramatically improve job queue performance.
274+
275+
### Running in Background
276+
277+
This library supports running Listener or Worker permanently in the background, it provides you the ability to run Worker as service.
278+
279+
#### Launcher
280+
281+
To run Listener or Worker in the background, you could call Launcher to launch process:
282+
283+
```
284+
$ php index.php myjob/launch
285+
```
286+
287+
By default, Launcher would launch `listen` process, you could also launch `work` by giving parameter:
288+
289+
```
290+
$ php index.php myjob/launch/worker
291+
```
292+
293+
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:
294+
295+
```ps
296+
$ php index.php myjob/launch
297+
Success to launch process `listen`: myjob/listen.
298+
Called command: php /srv/ci-project/index.php myjob/listen > /dev/null &
299+
------
300+
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
301+
user 14650 0.0 0.7 327144 29836 pts/3 R+ 15:43 0:00 php /srv/ci-project/index.php myjob/listen
302+
```
303+
304+
Then, when you launch the listener again, Launcher would prevent repeated running:
305+
306+
```ps
307+
$ php index.php myjob/launch
308+
Skip: Same process `listen` is running: myjob/listen.
309+
------
310+
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
311+
user 14650 0.4 0.9 337764 36616 pts/3 S 15:43 0:00 php /srv/ci-project/index.php myjob/listen
312+
```
313+
314+
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.
315+
316+
#### Process Status
317+
318+
After launching a listener, you could check the listener service by command `ps aux|grep php`:
319+
320+
```ps
321+
...
322+
www-data 2278 0.7 1.0 496852 84144 ? S Sep25 37:29 php-fpm: pool www
323+
www-data 3129 0.0 0.4 327252 31064 ? S Sep10 0:34 php /srv/ci-project/index.php myjob/listen
324+
...
325+
```
326+
327+
According to above, you could manage listener and workers such as killing listener by command `kill 3129`.
328+
329+
Workers would run while listener detected job, the running worker processes would also show in `ps aux|grep php`.
330+
331+
> Manually, you could also use an `&` (an ampersand) at the end of the listener or worker to run in the background.
332+
333+
334+
215335

216-
Each worker would continuously process worker callback funciton till returning `false`, which means that there are no task detected from the worker.
217336

218-
The worker could be called by CLI, for example `$ php ./index.php my_worker/worker`, which the listener is calling the same CLI to fork a worker.
219337

220338

221339

0 commit comments

Comments
 (0)