Skip to content

Commit 14a8e82

Browse files
committed
Init project
1 parent 852b405 commit 14a8e82

File tree

8 files changed

+454
-2
lines changed

8 files changed

+454
-2
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/vendor
2+
composer.phar
3+
composer.lock

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) 2020 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: 182 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,182 @@
1-
# php-worker-dispatcher
2-
Developing
1+
<p align="center">
2+
<a href="https://codeigniter.com/" target="_blank">
3+
<img src="https://www.php.net/images/logos/php-logo-bigger.png" height="60px">
4+
</a>
5+
<h1 align="center">PHP Worker Dispatcher</h1>
6+
<br>
7+
</p>
8+
9+
PHP multi-processing task dispatcher with managing workers
10+
11+
[![Latest Stable Version](https://poser.pugx.org/yidas/worker-dispatcher/v/stable?format=flat-square)](https://packagist.org/packages/yidas/worker-dispatcher)
12+
[![License](https://poser.pugx.org/yidas/worker-dispatcher/license?format=flat-square)](https://packagist.org/packages/yidas/worker-dispatcher)
13+
14+
15+
Features
16+
--------
17+
18+
- ***Multi-Processing** implementation on native PHP-CLI*
19+
20+
- ***Tasks Dispatching** to each worker process*
21+
22+
- ***Elegent Interface** for setup and use*
23+
24+
---
25+
26+
OUTLINE
27+
-------
28+
29+
- [Demonstration](#demonstration)
30+
- [Introduction](#introduction)
31+
- [Requirements](#requirements)
32+
- [Installation](#installation)
33+
- [Usage](#usage)
34+
- [Option](#option)
35+
- [callbacks.process](#callbacksprocess)
36+
- [callbacks.task](#callbackstask)
37+
38+
---
39+
40+
DEMONSTRATION
41+
-------------
42+
43+
Use multi-processing to dispatch tasks with generating workers based on CPU cores:
44+
45+
```php
46+
\yidas\WorkerDispatcher::run([
47+
'tasks' => ["R4NEJ1", "F5KH83", "..."],
48+
'callbacks' => [
49+
// The callback is for each forked process with decentralized tasks
50+
'task' => function ($config, $workderId, $task) {
51+
// $task is one of the `tasks` assigned to each worker, ex. "F5KH83" for $workderId is 2
52+
$token = $task;
53+
$result = file_get_contents("https://example/v1/register-by-token/{$token}");
54+
},
55+
],
56+
]);
57+
```
58+
59+
Use multi-processing to digest jobs from queue:
60+
61+
```php
62+
\yidas\WorkerDispatcher::run([
63+
'tasks' => false,
64+
'callbacks' => [
65+
// The callback is for each forked process
66+
'process' => function ($config, $workderId, $task) {
67+
// Get and handle each job from queue in inifite loop (You need to define your own function)
68+
while (true) {
69+
$result = handleOneJobFromQueue();
70+
if ($result === null) {
71+
break;
72+
}
73+
}
74+
},
75+
],
76+
]);
77+
```
78+
79+
---
80+
81+
INTRODUCTION
82+
------------
83+
84+
This library is implemented by PHP PCNTL control, which provides a main PHP-CLI to fork multiple child processes to share tasks, and even can use for high concurrency application with infinite loop setting.
85+
86+
<img src="https://raw.githubusercontent.com/yidas/php-worker-dispatcher/master/img/introduction.png" />
87+
88+
---
89+
90+
REQUIREMENTS
91+
------------
92+
93+
This library requires the following:
94+
95+
- Unix/Linux Environment
96+
- PHP CLI 5.4.0+
97+
98+
---
99+
100+
INSTALLATION
101+
------------
102+
103+
Run Composer in your project:
104+
105+
composer require yidas/php-worker-dispatcher ~1.0.0
106+
107+
Then you could use the class after Composer is loaded on your PHP project:
108+
109+
```php
110+
require __DIR__ . '/vendor/autoload.php';
111+
112+
use yidas\WorkerDispatcher;
113+
```
114+
115+
---
116+
117+
USAGE
118+
-----
119+
120+
Calling the `run()` method statically with options as argument, WorkerDispatcher will start to dispatch tasks (if any), and then fork the number of workers according to the environment or settings, and wait for all forked processes to complete or terminate the main process.
121+
122+
The setting example with all options is as following:
123+
124+
```php
125+
\yidas\WorkerDispatcher::run([
126+
'debug' => true,
127+
'workers' => 4,
128+
'config' => ['uri' => "/v1/resource"],
129+
'tasks' => ["R4NEJ1", "F5KH83", "..."],
130+
'callbacks' => [
131+
'process' => function ($config, $workerId, $tasks) {
132+
echo "The number of tasks in forked process - {$workerId}: " . count($tasks[$workerId - 1]) . "\n";
133+
},
134+
'task' => function ($config, $workerId, $task) {
135+
echo "Forked process - {$workerId}: Request to {$config['uri']} with token {$task}\n";
136+
},
137+
],
138+
]);
139+
```
140+
141+
### Options
142+
143+
|Option |Type |Deafult |Description|
144+
|:-- |:-- |:-- |:-- |
145+
|debug |boolean |false |Debug mode |
146+
|workers |integer |(auto) |The number of workers(processes) to fork. <br>(The default is the same as the number of CPU cores)|
147+
|config |multitype|null |The custom variable used to bring in the callback function|
148+
|tasks |multitype|array |For dispatching to each forked process. *<br>- Array: Each value of array will be dispatched to all forked processes. <br>- Integer: The number of loops dispatched to all forked processes. <br>- false: Perform finite loop.*|
149+
|[callbacks.process](#callbacksprocess) |callable |nul |Callback function called after each forked process is created|
150+
|[callbacks.task](#callbackstask) |callable |nul |Callback function called in each task loop of each forked process|
151+
152+
153+
#### callbacks.process
154+
155+
Callback function called after each forked process is created
156+
157+
```php
158+
function (multitype $config, integer $workerId, array $tasks)
159+
```
160+
161+
|Argument |Type |Deafult |Description|
162+
|:-- |:-- |:-- |:-- |
163+
|$config |multitype|null |The custom variable used to bring in the callback function|
164+
|$workerId |integer |(auto) |The sequence number of the worker(processes) in current function (Start from 1)|
165+
|$tasks |multitype|array |Tasks array list for the worker(processes) in current function|
166+
167+
#### callbacks.task
168+
169+
Callback function called in each task loop of each forked process
170+
171+
```php
172+
function (multitype $config, integer $workerId, multitype $task)
173+
```
174+
175+
|Argument |Type |Deafult |Description|
176+
|:-- |:-- |:-- |:-- |
177+
|$config |multitype|null |The custom variable used to bring in the callback function|
178+
|$workerId |integer |(auto) |The sequence number of the worker(processes) in current function (Start from 1)|
179+
|$tasks |multitype|array |The value of each tasks array list for each worker(processes) in current function|
180+
181+
182+

composer.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "yidas/worker-dispatcher",
3+
"description": "PHP multi-processing task dispatcher with managing workers",
4+
"keywords": ["PHP", "Multiprocessing", "pcntl", "task", "worker", "worker-dispatcher", "worker-manage"],
5+
"homepage": "https://github.com/yidas/php-worker-dispatcher",
6+
"type": "library",
7+
"license": "MIT",
8+
"support": {
9+
"issues": "https://github.com/yidas/php-worker-dispatcher/issues",
10+
"source": "https://github.com/yidas/php-worker-dispatcher"
11+
},
12+
"require": {
13+
"php": ">=5.4"
14+
},
15+
"autoload": {
16+
"classmap": ["src/"]
17+
}
18+
}

demo.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
// PHP-CLI only
4+
if (strtolower(php_sapi_name()) != 'cli') {
5+
die("PHP-CLI environment only");
6+
}
7+
8+
// Composer
9+
$composerPath = __DIR__ . '/vendor/autoload.php';
10+
if (!file_exists($composerPath)) {
11+
die("Composer is not installed, please install Composer.\n");
12+
}
13+
require $composerPath;
14+
15+
/**
16+
* Demo
17+
*/
18+
19+
use yidas\WorkerDispatcher;
20+
21+
// CLI option
22+
$tasks = isset($argv[1]) ? (int) $argv[1] : ["R4NEJ1", "F5KH83", "..."];
23+
24+
\yidas\WorkerDispatcher::run([
25+
'debug' => true,
26+
'workers' => 4,
27+
'config' => ['uri' => "/v1/resource"],
28+
'tasks' => $tasks,
29+
'callbacks' => [
30+
'process' => function ($config, $workerId, $tasks) {
31+
// var_dump($tasks);
32+
echo "The number of tasks in forked process - {$workerId}: " . count($tasks[$workerId - 1]) . "\n";
33+
},
34+
'task' => function ($config, $workerId, $task) {
35+
echo "Forked process - {$workerId}: Request to {$config['uri']} with token {$task}\n";
36+
},
37+
],
38+
]);

img/introduction.drawio

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<mxfile host="app.diagrams.net" modified="2020-07-22T11:22:33.882Z" agent="5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36" version="13.5.0" etag="0jKpSUMRW3tL55tWe6Hq" type="device"><diagram id="635cb6e3-ab18-6265-92dc-40cb7cfe9734" name="Page-1">7ZrZbts4FIafxkDnIgUlavNlY3cDWqDTtGjmakBLjEREIjUUvfXph7QoSzKp2HVsp0tuDPKI63fOzy0ZwUmxestRmX1kCc5HLkhWIzgdue7YC+SvMqxrgw+j2pByktQmpzXckO9YG4G2zkmCq15BwVguSNk3xoxSHIueDXHOlv1idyzv91qiFBuGmxjlpvUbSURWWyM3bO3vMEmzpmcnGNdfCtQU1jOpMpSwZccEX4/ghDMm6lSxmuBcsWu41PXeDHzdDoxjKg6pAHEIYgeOZzj0IIhnV37dwgLlcz1ZPVCxbmafcjYvzY503wvMBV7Z3IBmTQvtTGWEYFZgwdeynK7lepqWjo5Qs1q2qCHUtqyD2Q0bF2v3ptumWwIyoSHYgfgBjtBsHCEHghmeXTne0UAG2R5PqNGLcyCRJsxOSgQcTWQQ7vFEngSBu5+AFHapkvGc5+trjuJ7LEd0vcyIwDclitW3pVwcpS0Thex46sikxEYTnOjcAQDBAMBBcTkNJB1KrhlKrgWjcw6MprQmGZFLsQs+cRbjqhq5Qa6ozbhMpSr14hvj95j/ZfBuyYH9mCvB2f126T4b6sDvobao1rGx9s+A2lzWv2RYGui8mGFJF7A7+VPDrdTOjEtME5ViVP5guiCc0ULRkSZVocJCEJoajpBwhI32hOWyHpxSRmXJ6zuS5zsmlJOUKsnIXuSY4LVCTeSu+0p/KEiSqG6s7u0HwHZfVZk0R1Wl0zErSKzTTLZAhPKODy4kNycMzSAYW4IAniEIAiMI3kiHmyJ79unDPnX6Pt020fGp71u2ojO4NPqzllB5kn66NXT8Z7G2hfXFWDvQgP0REfog6y+okosZmJKqRCLOHo39pJB/8AwQnICpcf8wj611ePagbe7DXdNmkwcKbvUbbQx3jIobPRHLrfngu9ugnK4O2CUgsN1YTuD6v79+WH1d/He7+rzwi+Bd9G8C3lvcr51qKMlR4wJCfTU1JHsiZTXkGUM/lrud8WowvCaB3eOTezn9WCGa65IJiCav1COTim4VjCoEu1zwiojbTvofFaEvfZ2brnTAbjLrJkPl4G+7mU4tlW2rbXJNvXpwODHes4x3m4rNeYz3h49APMVD6yG0e7N7MHpgA+E4R4Is+kO1eVD38ImRzX1EBwsc94PFHe8EQT1FXcvtPIrtNOR5Ow15Ow3VDIyGNgG1nfbxMWbei4eECr2fQ6d+uKNT54J3XSvDA94xf32dDpxQuxIdDrBnmT5SpuZ1elCm8CeV6aHb6dlkGp5ApkdK7hh5n0am3oEyDZ5legKZmk8kv5pMXWh5NLyoTM2nj99Epns1GO2Xc/SUMvWh14+V5hXnsTKFIDqRTGW2/YN2Xbz9rwD4+n8=</diagram></mxfile>

img/introduction.png

169 KB
Loading

0 commit comments

Comments
 (0)