You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
15
16
+
> This library only provides worker controller, you need to implement your own queue driver with handler/process in it.
17
+
17
18
Features
18
19
--------
19
20
20
21
-***Multi-Processing** implementation on native PHP-CLI*
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
+
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.
77
+
78
+
PHP is a lack of support for multithreading at the core language level, this library implements multithreading by managing multiprocessing.
79
+
80
+
For more concepts, the following diagram shows the implementation structure of this library:
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.
84
+
Listener (Daemon) could continue to run for detecting new jobs until it is manually stopped or you close your terminal. On the other hand
85
+
, Worker could continue to run for processing new jobs until there is no job left, which the workers could be called by Listener.
86
+
87
+
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.
You need to design handlers for your own worker inherited from this library, there are common interfaces as following:
120
+
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:
112
121
113
122
```php
114
123
use yidas\queue\worker\Controller as WorkerController;
@@ -126,7 +135,7 @@ class My_worker extends WorkerController
126
135
}
127
136
```
128
137
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.
138
+
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.
130
139
131
140
### How to Design a Worker
132
141
@@ -144,15 +153,14 @@ class My_worker extends \yidas\queue\worker\Controller
144
153
{
145
154
protected function init()
146
155
{
147
-
// Optional autoload
156
+
// Optional autoload (Load your own libraries or models)
148
157
$this->load->library('myjobs');
149
-
150
-
// Optional shared properties setting
151
-
$this->static = 'static value';
152
158
}
153
159
// ...
154
160
```
155
161
162
+
> As above, `myjobs` library is defined by your own application which handles your job processes.
163
+
156
164
#### 2. Build Worker
157
165
158
166
```php
@@ -167,15 +175,17 @@ class My_worker extends \yidas\queue\worker\Controller
167
175
{
168
176
protected function handleWork()
169
177
{
178
+
// Your own method to get a job from your queue in the application
170
179
$job = $this->myjobs->popJob();
171
180
172
-
// `false` for job not found, which would close the worker itself.
181
+
// return `false` for job not found, which would close the worker itself.
173
182
if (!$job)
174
183
return false;
175
184
185
+
// Your own method to process a job
176
186
$this->myjobs->processJob($job);
177
187
178
-
// `true` for job existing, which would keep handling.
188
+
// return `true` for job existing, which would keep handling.
179
189
return true;
180
190
}
181
191
// ...
@@ -195,8 +205,9 @@ class My_worker extends \yidas\queue\worker\Controller
195
205
{
196
206
protected function handleListen()
197
207
{
198
-
// `true` for job existing, which leads to dispatch worker(s).
199
-
// `false` for job not found, which would keep detecting new job
208
+
// Your own method to detect job existence
209
+
// return `true` for job existing, which leads to dispatch worker(s).
210
+
// return `false` for job not found, which would keep detecting new job
200
211
return $this->myjobs->exists();
201
212
}
202
213
// ...
@@ -241,7 +252,7 @@ USAGE
241
252
242
253
There are 3 actions for usage:
243
254
244
-
-`listen` A listener to manage and dispatch jobs by forking workers.
255
+
-`listen` A listener (Daemon) to manage and dispatch jobs by forking workers.
245
256
-`work` A worker to process and solve jobs from queue.
246
257
-`launch` A launcher to run `listen` or `work` process in background and keep it running uniquely.
0 commit comments