Skip to content

Commit 22e3f21

Browse files
committed
Beta version
1 parent f8abc2d commit 22e3f21

File tree

4 files changed

+597
-2
lines changed

4 files changed

+597
-2
lines changed

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2018 Nick Tsai
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 221 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,221 @@
1-
# codeigniter-worker
2-
Developing
1+
<p align="center">
2+
<a href="https://codeigniter.com/" target="_blank">
3+
<img src="https://codeigniter.com/assets/images/ci-logo-big.png" height="100px">
4+
</a>
5+
<h1 align="center">CodeIgniter Queue Worker</h1>
6+
<br>
7+
</p>
8+
9+
CodeIgniter 3 Queue Worker Management Controller
10+
11+
[![Latest Stable Version](https://poser.pugx.org/yidas/codeigniter-queue-worker/v/stable?format=flat-square)](https://packagist.org/packages/yidas/codeigniter-queue-worker)
12+
[![Latest Unstable Version](https://poser.pugx.org/yidas/codeigniter-queue-worker/v/unstable?format=flat-square)](https://packagist.org/packages/yidas/codeigniter-queue-worker)
13+
[![License](https://poser.pugx.org/yidas/codeigniter-queue-worker/license?format=flat-square)](https://packagist.org/packages/yidas/codeigniter-queue-worker)
14+
15+
This Queue Worker extension is collected into [yidas/codeigniter-pack](https://github.com/yidas/codeigniter-pack) which is a complete solution for Codeigniter framework.
16+
17+
Features
18+
--------
19+
20+
- ***Multi-Processing** implementation on native PHP-CLI*
21+
22+
- *Easy way to manage **multiple workers/processes***
23+
24+
- *Standard Base Controller for inheritance*
25+
26+
---
27+
28+
OUTLINE
29+
-------
30+
31+
- [Demonstration](#demonstration)
32+
- [Requirements](#requirements)
33+
- [Installation](#installation)
34+
- [Configuration](#configuration)
35+
- [How to Design a Worker](#how-to-design-a-worker)
36+
- [1. Build Initializer](#1-build-initializer)
37+
- [2. Build Listener](#2-build-listener)
38+
- [3. Build Worker](#3-build-worker)
39+
- [Porperties Setting](#porperties-setting)
40+
- [Public Properties](#public-properties)
41+
- [Usage](#usage)
42+
43+
---
44+
45+
DEMONSTRATION
46+
-------------
47+
48+
49+
```
50+
$ php ./index.php worker_controller/listener
51+
```
52+
53+
Check log:
54+
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+
```
63+
---
64+
65+
REQUIREMENTS
66+
------------
67+
This library requires the following:
68+
69+
- PHP CLI 5.4.0+
70+
- CodeIgniter 3.0.0+
71+
72+
---
73+
74+
INSTALLATION
75+
------------
76+
77+
Run Composer in your Codeigniter project under the folder `\application`:
78+
79+
composer require yidas/codeigniter-queue-worker
80+
81+
Check Codeigniter `application/config/config.php`:
82+
83+
```php
84+
$config['composer_autoload'] = TRUE;
85+
```
86+
87+
> You could customize the vendor path into `$config['composer_autoload']`
88+
89+
---
90+
91+
CONFIGURATION
92+
-------------
93+
94+
You need to design porcesses or set properties for your own worker inherited from this library, there are common interfaces as following:
95+
96+
```php
97+
use yidas\queue\worker\Controller as WorkerController;
98+
99+
class My_worker extends WorkerController
100+
{
101+
// Initializer
102+
protected function init() {}
103+
104+
// Listener
105+
protected function listenerCallback() {}
106+
107+
// Worker
108+
protected function workerCallback() {}
109+
}
110+
```
111+
112+
### How to Design a Worker
113+
114+
#### 1. Build Initializer
115+
116+
```php
117+
protected void init()
118+
```
119+
120+
*Example Code:*
121+
```php
122+
class My_worker extends \yidas\queue\worker\Controller
123+
{
124+
protected function init()
125+
{
126+
// Optional autoload
127+
$this->load->library('myqueue');
128+
129+
// Optional shared properties setting
130+
$this->static = 'static value';
131+
}
132+
// ...
133+
```
134+
135+
#### 2. Build Listener
136+
137+
```php
138+
protected boolean listenerCallback(object $static=null)
139+
```
140+
141+
*Example Code:*
142+
```php
143+
class My_worker extends \yidas\queue\worker\Controller
144+
{
145+
protected function listenerCallback()
146+
{
147+
// `true` for task existing
148+
return $this->myqueue->exists();
149+
}
150+
// ...
151+
```
152+
153+
#### 3. Build Worker
154+
155+
```php
156+
protected boolean workerCallback(object $static=null)
157+
```
158+
159+
*Example Code:*
160+
```php
161+
class My_worker extends \yidas\queue\worker\Controller
162+
{
163+
protected function workerCallback()
164+
{
165+
// `false` for task not found
166+
return $this->myqueue->processTask();
167+
}
168+
// ...
169+
```
170+
171+
### Porperties Setting
172+
173+
You could customize your worker by defining properties.
174+
175+
```php
176+
use yidas\queue\worker\Controller as WorkerController;
177+
178+
class My_worker extends WorkerController
179+
{
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;
183+
184+
// Enable text log writen into specified file
185+
public $logPath = 'tmp/my-worker.log';
186+
}
187+
```
188+
189+
#### Public Properties
190+
191+
|Property |Type |Deafult |Description|
192+
|:-- |:-- |:-- |:-- |
193+
|$debug |boolean |true |Debug mode |
194+
|$logPath |string |null |Log file path|
195+
|$phpCommand |string |'php' |PHP CLI command for current environment|
196+
|$listenerSleep |integer |3 |Time interval of listen frequency on idle|
197+
|$workerSleep |integer |0 |Time interval of worker processes|
198+
|$workerMaxNum |integer |5 |Number of max workers|
199+
|$workerStartNum |integer |1 |Number of workers at start, less than or equal to $workerMaxNum|
200+
|$workerWaitSeconds|integer |10 |Waiting time between worker started and next worker starting|
201+
202+
203+
---
204+
205+
USAGE
206+
-----
207+
208+
After configurating a worker, this worker controller is ready to go:
209+
210+
```
211+
$ php ./index.php my_worker/listener
212+
```
213+
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.
215+
216+
Each worker would continuously process worker callback funciton till returning `false`, which means that there are no task detected from the worker.
217+
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.
219+
220+
221+

composer.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "yidas/codeigniter-queue-worker",
3+
"description": "CodeIgniter 3 Queue Worker Management Controller",
4+
"keywords": ["codeIgniter", "queue", "worker", "listener", "worker-manage"],
5+
"homepage": "https://github.com/yidas/codeigniter-queue-worker",
6+
"type": "library",
7+
"license": "MIT",
8+
"support": {
9+
"issues": "https://github.com/yidas/codeigniter-queue-worker/issues",
10+
"source": "https://github.com/yidas/codeigniter-queue-worker"
11+
},
12+
"require": {
13+
"php": ">=5.4.0"
14+
},
15+
"autoload": {
16+
"psr-4": {
17+
"yidas\\queue\\worker\\": "src/"
18+
}
19+
}
20+
}

0 commit comments

Comments
 (0)