@@ -19,418 +19,418 @@ |
||
19 | 19 | class QueuedTasksTableTest extends TestCase |
20 | 20 | { |
21 | 21 | |
22 | - /** |
|
23 | - * |
|
24 | - * @var \Queue\Model\Table\QueuedTasksTable |
|
25 | - */ |
|
26 | - protected $QueuedTasks; |
|
27 | - |
|
28 | - /** |
|
29 | - * Fixtures |
|
30 | - * |
|
31 | - * @var array |
|
32 | - */ |
|
33 | - public $fixtures = [ |
|
34 | - 'plugin.Queue.QueuedTasks' |
|
35 | - ]; |
|
36 | - |
|
37 | - /** |
|
38 | - * setUp method |
|
39 | - * |
|
40 | - * @return void |
|
41 | - */ |
|
42 | - public function setUp() |
|
43 | - { |
|
44 | - parent::setUp(); |
|
45 | - $config = TableRegistry::getTableLocator()->exists('QueuedTasks') ? [] : [ |
|
46 | - 'className' => QueuedTasksTable::class |
|
47 | - ]; |
|
48 | - $this->QueuedTasks = TableRegistry::getTableLocator()->get('QueuedTasks', $config); |
|
49 | - } |
|
50 | - |
|
51 | - /** |
|
52 | - * Basic Instance test |
|
53 | - * |
|
54 | - * @return void |
|
55 | - */ |
|
56 | - public function testQueueInstance() |
|
57 | - { |
|
58 | - $this->assertInstanceOf(QueuedTasksTable::class, $this->QueuedTasks); |
|
59 | - } |
|
60 | - |
|
61 | - /** |
|
62 | - * Test the basic create and length evaluation functions. |
|
63 | - * |
|
64 | - * @return void |
|
65 | - */ |
|
66 | - public function testCreateAndCount() |
|
67 | - { |
|
68 | - // at first, the queue should contain 0 items. |
|
69 | - $this->assertSame(0, $this->QueuedTasks->getLength()); |
|
70 | - |
|
71 | - // create a job |
|
72 | - $this->assertTrue((bool)$this->QueuedTasks->createJob('test1', [ |
|
73 | - 'some' => 'random', |
|
74 | - 'test' => 'data' |
|
75 | - ])); |
|
76 | - |
|
77 | - // test if queue Length is 1 now. |
|
78 | - $this->assertSame(1, $this->QueuedTasks->getLength()); |
|
79 | - |
|
80 | - // create some more jobs |
|
81 | - $this->assertTrue((bool)$this->QueuedTasks->createJob('test2', [ |
|
82 | - 'some' => 'random', |
|
83 | - 'test' => 'data2' |
|
84 | - ])); |
|
85 | - $this->assertTrue((bool)$this->QueuedTasks->createJob('test2', [ |
|
86 | - 'some' => 'random', |
|
87 | - 'test' => 'data3' |
|
88 | - ])); |
|
89 | - $this->assertTrue((bool)$this->QueuedTasks->createJob('test3', [ |
|
90 | - 'some' => 'random', |
|
91 | - 'test' => 'data4' |
|
92 | - ])); |
|
93 | - |
|
94 | - // overall queueLength shpould now be 4 |
|
95 | - $this->assertSame(4, $this->QueuedTasks->getLength()); |
|
96 | - |
|
97 | - // there should be 1 task of type 'test1', one of type 'test3' and 2 of type 'test2' |
|
98 | - $this->assertSame(1, $this->QueuedTasks->getLength('test1')); |
|
99 | - $this->assertSame(2, $this->QueuedTasks->getLength('test2')); |
|
100 | - $this->assertSame(1, $this->QueuedTasks->getLength('test3')); |
|
101 | - } |
|
102 | - |
|
103 | - /** |
|
104 | - * Test the basic create and fetch functions. |
|
105 | - * |
|
106 | - * @return void |
|
107 | - */ |
|
108 | - public function testCreateAndFetch() |
|
109 | - { |
|
110 | - $this->_needsConnection(); |
|
111 | - |
|
112 | - // $capabilities is a list of tasks the worker can run. |
|
113 | - $capabilities = [ |
|
114 | - 'task1' => [ |
|
115 | - 'name' => 'task1', |
|
116 | - 'timeout' => 100, |
|
117 | - 'retries' => 2 |
|
118 | - ] |
|
119 | - ]; |
|
120 | - $testData = [ |
|
121 | - 'x1' => 'y1', |
|
122 | - 'x2' => 'y2', |
|
123 | - 'x3' => 'y3', |
|
124 | - 'x4' => 'y4' |
|
125 | - ]; |
|
126 | - |
|
127 | - // start off empty. |
|
128 | - $this->assertSame([], $this->QueuedTasks->find() |
|
129 | - ->toArray()); |
|
130 | - // at first, the queue should contain 0 items. |
|
131 | - $this->assertSame(0, $this->QueuedTasks->getLength()); |
|
132 | - // there are no jobs, so we cant fetch any. |
|
133 | - $this->assertNull($this->QueuedTasks->requestJob($capabilities)); |
|
134 | - // insert one job. |
|
135 | - $this->assertTrue((bool)$this->QueuedTasks->createJob('task1', $testData)); |
|
136 | - |
|
137 | - // fetch and check the first job. |
|
138 | - $job = $this->QueuedTasks->requestJob($capabilities); |
|
139 | - $this->assertSame(1, $job->id); |
|
140 | - $this->assertSame('task1', $job->task); |
|
141 | - $this->assertSame(0, $job->failed); |
|
142 | - $this->assertNull($job->completed); |
|
143 | - $this->assertSame($testData, unserialize($job->data)); |
|
144 | - |
|
145 | - // after this job has been fetched, it may not be reassigned. |
|
146 | - $result = $this->QueuedTasks->requestJob($capabilities); |
|
147 | - $this->assertNull($result); |
|
148 | - |
|
149 | - // queue length is still 1 since the first job did not finish. |
|
150 | - $this->assertSame(1, $this->QueuedTasks->getLength()); |
|
151 | - |
|
152 | - // Now mark Task1 as done |
|
153 | - $this->assertTrue($this->QueuedTasks->markJobDone($job)); |
|
154 | - |
|
155 | - // Should be 0 again. |
|
156 | - $this->assertSame(0, $this->QueuedTasks->getLength()); |
|
157 | - } |
|
158 | - |
|
159 | - /** |
|
160 | - * Test the delivery of jobs in sequence, skipping fetched but not completed tasks. |
|
161 | - * |
|
162 | - * @return void |
|
163 | - */ |
|
164 | - public function testSequence() |
|
165 | - { |
|
166 | - $this->_needsConnection(); |
|
167 | - |
|
168 | - // $capabilities is a list of tasks the worker can run. |
|
169 | - $capabilities = [ |
|
170 | - 'task1' => [ |
|
171 | - 'name' => 'task1', |
|
172 | - 'timeout' => 100, |
|
173 | - 'retries' => 2 |
|
174 | - ] |
|
175 | - ]; |
|
176 | - // at first, the queue should contain 0 items. |
|
177 | - $this->assertSame(0, $this->QueuedTasks->getLength()); |
|
178 | - // create some more jobs |
|
179 | - foreach (range(0, 9) as $num) { |
|
180 | - $this->assertTrue((bool)$this->QueuedTasks->createJob('task1', [ |
|
181 | - 'tasknum' => $num |
|
182 | - ])); |
|
183 | - } |
|
184 | - // 10 jobs in the queue. |
|
185 | - $this->assertSame(10, $this->QueuedTasks->getLength()); |
|
186 | - |
|
187 | - // jobs should be fetched in the original sequence. |
|
188 | - $array = []; |
|
189 | - foreach (range(0, 4) as $num) { |
|
190 | - $this->QueuedTasks->clearKey(); |
|
191 | - $array[$num] = $this->QueuedTasks->requestJob($capabilities); |
|
192 | - $jobData = unserialize($array[$num]['data']); |
|
193 | - $this->assertSame($num, $jobData['tasknum']); |
|
194 | - } |
|
195 | - // now mark them as done |
|
196 | - foreach (range(0, 4) as $num) { |
|
197 | - $this->assertTrue($this->QueuedTasks->markJobDone($array[$num])); |
|
198 | - $this->assertSame(9 - $num, $this->QueuedTasks->getLength()); |
|
199 | - } |
|
200 | - |
|
201 | - // jobs should be fetched in the original sequence. |
|
202 | - foreach (range(5, 9) as $num) { |
|
203 | - $job = $this->QueuedTasks->requestJob($capabilities); |
|
204 | - $jobData = unserialize($job->data); |
|
205 | - $this->assertSame($num, $jobData['tasknum']); |
|
206 | - $this->assertTrue($this->QueuedTasks->markJobDone($job)); |
|
207 | - $this->assertSame(9 - $num, $this->QueuedTasks->getLength()); |
|
208 | - } |
|
209 | - } |
|
210 | - |
|
211 | - /** |
|
212 | - * Test creating Jobs to run close to a specified time, and strtotime parsing. |
|
213 | - * Using toUnixString() function to convert Time object to timestamp, instead of strtotime |
|
214 | - * |
|
215 | - * @return null |
|
216 | - */ |
|
217 | - public function testNotBefore() |
|
218 | - { |
|
219 | - $this->assertTrue((bool)$this->QueuedTasks->createJob('task1', null, '+ 1 Min')); |
|
220 | - $this->assertTrue((bool)$this->QueuedTasks->createJob('task1', null, '+ 1 Day')); |
|
221 | - $this->assertTrue((bool)$this->QueuedTasks->createJob('task1', null, '2009-07-01 12:00:00')); |
|
222 | - $data = $this->QueuedTasks->find('all')->toArray(); |
|
223 | - $this->assertWithinRange((new Time('+ 1 Min'))->toUnixString(), $data[0]['not_before']->toUnixString(), 60); |
|
224 | - $this->assertWithinRange((new Time('+ 1 Day'))->toUnixString(), $data[1]['not_before']->toUnixString(), 60); |
|
225 | - $this->assertWithinRange((new Time('2009-07-01 12:00:00'))->toUnixString(), $data[2]['not_before']->toUnixString(), 60); |
|
226 | - } |
|
227 | - |
|
228 | - /** |
|
229 | - * Test Job reordering depending on 'notBefore' field. |
|
230 | - * Jobs with an expired not_before field should be executed before any other job without specific timing info. |
|
231 | - * |
|
232 | - * @return void |
|
233 | - */ |
|
234 | - public function testNotBeforeOrder() |
|
235 | - { |
|
236 | - $this->_needsConnection(); |
|
237 | - |
|
238 | - $capabilities = [ |
|
239 | - 'task1' => [ |
|
240 | - 'name' => 'task1', |
|
241 | - 'timeout' => 100, |
|
242 | - 'retries' => 2 |
|
243 | - ], |
|
244 | - 'dummytask' => [ |
|
245 | - 'name' => 'dummytask', |
|
246 | - 'timeout' => 100, |
|
247 | - 'retries' => 2 |
|
248 | - ] |
|
249 | - ]; |
|
250 | - $this->assertTrue((bool)$this->QueuedTasks->createJob('dummytask')); |
|
251 | - $this->assertTrue((bool)$this->QueuedTasks->createJob('dummytask')); |
|
252 | - // create a task with it's execution target some seconds in the past, so it should jump to the top of the testCreateAndFetchlist. |
|
253 | - $this->assertTrue((bool)$this->QueuedTasks->createJob('task1', [ |
|
254 | - 'three' |
|
255 | - ], '- 3 Seconds')); |
|
256 | - $this->assertTrue((bool)$this->QueuedTasks->createJob('task1', [ |
|
257 | - 'two' |
|
258 | - ], '- 5 Seconds')); |
|
259 | - $this->assertTrue((bool)$this->QueuedTasks->createJob('task1', [ |
|
260 | - 'one' |
|
261 | - ], '- 7 Seconds')); |
|
262 | - |
|
263 | - // when using requestJob, the jobs we just created should be delivered in this order, NOT the order in which they where created. |
|
264 | - $expected = [ |
|
265 | - [ |
|
266 | - 'name' => 'task1', |
|
267 | - 'data' => [ |
|
268 | - 'one' |
|
269 | - ] |
|
270 | - ], |
|
271 | - [ |
|
272 | - 'name' => 'task1', |
|
273 | - 'data' => [ |
|
274 | - 'two' |
|
275 | - ] |
|
276 | - ], |
|
277 | - [ |
|
278 | - 'name' => 'task1', |
|
279 | - 'data' => [ |
|
280 | - 'three' |
|
281 | - ] |
|
282 | - ], |
|
283 | - [ |
|
284 | - 'name' => 'dummytask', |
|
285 | - 'data' => null |
|
286 | - ], |
|
287 | - [ |
|
288 | - 'name' => 'dummytask', |
|
289 | - 'data' => null |
|
290 | - ] |
|
291 | - ]; |
|
292 | - |
|
293 | - foreach ($expected as $item) { |
|
294 | - $this->QueuedTasks->clearKey(); |
|
295 | - $tmp = $this->QueuedTasks->requestJob($capabilities); |
|
296 | - |
|
297 | - $this->assertSame($item['name'], $tmp['task']); |
|
298 | - $this->assertEquals($item['data'], unserialize($tmp['data'])); |
|
299 | - } |
|
300 | - } |
|
301 | - |
|
302 | - /** |
|
303 | - * Job Rate limiting. |
|
304 | - * Do not execute jobs of a certain type more often than once every X seconds. |
|
305 | - * |
|
306 | - * @return void |
|
307 | - */ |
|
308 | - public function testRateLimit() |
|
309 | - { |
|
310 | - $this->_needsConnection(); |
|
311 | - |
|
312 | - $capabilities = [ |
|
313 | - 'task1' => [ |
|
314 | - 'name' => 'task1', |
|
315 | - 'timeout' => 101, |
|
316 | - 'retries' => 2, |
|
317 | - 'rate' => 2 |
|
318 | - ], |
|
319 | - 'dummytask' => [ |
|
320 | - 'name' => 'dummytask', |
|
321 | - 'timeout' => 101, |
|
322 | - 'retries' => 2 |
|
323 | - ] |
|
324 | - ]; |
|
325 | - |
|
326 | - // clear out the rate history |
|
327 | - $this->QueuedTasks->rateHistory = []; |
|
328 | - |
|
329 | - $data1 = [ |
|
330 | - 'key' => 1 |
|
331 | - ]; |
|
332 | - $this->assertTrue((bool)$this->QueuedTasks->createJob('task1', $data1)); |
|
333 | - $data2 = [ |
|
334 | - 'key' => 2 |
|
335 | - ]; |
|
336 | - $this->assertTrue((bool)$this->QueuedTasks->createJob('task1', $data2)); |
|
337 | - $data3 = [ |
|
338 | - 'key' => 3 |
|
339 | - ]; |
|
340 | - $this->assertTrue((bool)$this->QueuedTasks->createJob('task1', $data3)); |
|
341 | - $this->assertTrue((bool)$this->QueuedTasks->createJob('dummytask')); |
|
342 | - $this->assertTrue((bool)$this->QueuedTasks->createJob('dummytask')); |
|
343 | - $this->assertTrue((bool)$this->QueuedTasks->createJob('dummytask')); |
|
344 | - $this->assertTrue((bool)$this->QueuedTasks->createJob('dummytask')); |
|
345 | - |
|
346 | - // At first we get task1-1. |
|
347 | - $this->QueuedTasks->clearKey(); |
|
348 | - $tmp = $this->QueuedTasks->requestJob($capabilities); |
|
349 | - $this->assertSame('task1', $tmp['task']); |
|
350 | - $this->assertSame($data1, unserialize($tmp['data'])); |
|
351 | - |
|
352 | - // The rate limit should now skip over task1-2 and fetch a dummytask. |
|
353 | - $this->QueuedTasks->clearKey(); |
|
354 | - $tmp = $this->QueuedTasks->requestJob($capabilities); |
|
355 | - $this->assertSame('dummytask', $tmp['task']); |
|
356 | - $this->assertFalse(unserialize($tmp['data'])); |
|
357 | - |
|
358 | - usleep(100000); |
|
359 | - // and again. |
|
360 | - $this->QueuedTasks->clearKey(); |
|
361 | - $tmp = $this->QueuedTasks->requestJob($capabilities); |
|
362 | - $this->assertSame('dummytask', $tmp['task']); |
|
363 | - $this->assertFalse(unserialize($tmp['data'])); |
|
364 | - |
|
365 | - // Then some time passes |
|
366 | - sleep(2); |
|
367 | - |
|
368 | - // Now we should get task1-2 |
|
369 | - $this->QueuedTasks->clearKey(); |
|
370 | - $tmp = $this->QueuedTasks->requestJob($capabilities); |
|
371 | - $this->assertSame('task1', $tmp['task']); |
|
372 | - $this->assertSame($data2, unserialize($tmp['data'])); |
|
373 | - |
|
374 | - // and again rate limit to dummytask. |
|
375 | - $this->QueuedTasks->clearKey(); |
|
376 | - $tmp = $this->QueuedTasks->requestJob($capabilities); |
|
377 | - $this->assertSame('dummytask', $tmp['task']); |
|
378 | - $this->assertFalse(unserialize($tmp['data'])); |
|
379 | - |
|
380 | - // Then some more time passes |
|
381 | - sleep(2); |
|
382 | - |
|
383 | - // Now we should get task1-3 |
|
384 | - $this->QueuedTasks->clearKey(); |
|
385 | - $tmp = $this->QueuedTasks->requestJob($capabilities); |
|
386 | - $this->assertSame('task1', $tmp['task']); |
|
387 | - $this->assertSame($data3, unserialize($tmp['data'])); |
|
388 | - |
|
389 | - // and again rate limit to dummytask. |
|
390 | - $this->QueuedTasks->clearKey(); |
|
391 | - $tmp = $this->QueuedTasks->requestJob($capabilities); |
|
392 | - $this->assertSame('dummytask', $tmp['task']); |
|
393 | - $this->assertFalse(unserialize($tmp['data'])); |
|
394 | - |
|
395 | - // and now the queue is empty |
|
396 | - $this->QueuedTasks->clearKey(); |
|
397 | - $tmp = $this->QueuedTasks->requestJob($capabilities); |
|
398 | - $this->assertNull($tmp); |
|
399 | - } |
|
400 | - |
|
401 | - /** |
|
402 | - * |
|
403 | - * @return void |
|
404 | - */ |
|
405 | - public function testIsQueued() |
|
406 | - { |
|
407 | - $result = $this->QueuedTasks->isQueued('foo-bar'); |
|
408 | - $this->assertFalse($result); |
|
409 | - |
|
410 | - $queuedJob = $this->QueuedTasks->newEntity([ |
|
411 | - 'key' => 'key', |
|
412 | - 'task' => 'FooBar' |
|
413 | - ]); |
|
414 | - $this->QueuedTasks->saveOrFail($queuedJob); |
|
415 | - |
|
416 | - $result = $this->QueuedTasks->isQueued('foo-bar'); |
|
417 | - $this->assertTrue($result); |
|
418 | - |
|
419 | - $queuedJob->completed = new FrozenTime(); |
|
420 | - $this->QueuedTasks->saveOrFail($queuedJob); |
|
421 | - |
|
422 | - $result = $this->QueuedTasks->isQueued('foo-bar'); |
|
423 | - $this->assertFalse($result); |
|
424 | - } |
|
425 | - |
|
426 | - /** |
|
427 | - * Helper method for skipping tests that need a real connection. |
|
428 | - * |
|
429 | - * @return void |
|
430 | - */ |
|
431 | - protected function _needsConnection() |
|
432 | - { |
|
433 | - $config = ConnectionManager::getConfig('test'); |
|
434 | - $this->skipIf(strpos($config['driver'], 'Mysql') === false, 'Only Mysql is working yet for this.'); |
|
435 | - } |
|
22 | + /** |
|
23 | + * |
|
24 | + * @var \Queue\Model\Table\QueuedTasksTable |
|
25 | + */ |
|
26 | + protected $QueuedTasks; |
|
27 | + |
|
28 | + /** |
|
29 | + * Fixtures |
|
30 | + * |
|
31 | + * @var array |
|
32 | + */ |
|
33 | + public $fixtures = [ |
|
34 | + 'plugin.Queue.QueuedTasks' |
|
35 | + ]; |
|
36 | + |
|
37 | + /** |
|
38 | + * setUp method |
|
39 | + * |
|
40 | + * @return void |
|
41 | + */ |
|
42 | + public function setUp() |
|
43 | + { |
|
44 | + parent::setUp(); |
|
45 | + $config = TableRegistry::getTableLocator()->exists('QueuedTasks') ? [] : [ |
|
46 | + 'className' => QueuedTasksTable::class |
|
47 | + ]; |
|
48 | + $this->QueuedTasks = TableRegistry::getTableLocator()->get('QueuedTasks', $config); |
|
49 | + } |
|
50 | + |
|
51 | + /** |
|
52 | + * Basic Instance test |
|
53 | + * |
|
54 | + * @return void |
|
55 | + */ |
|
56 | + public function testQueueInstance() |
|
57 | + { |
|
58 | + $this->assertInstanceOf(QueuedTasksTable::class, $this->QueuedTasks); |
|
59 | + } |
|
60 | + |
|
61 | + /** |
|
62 | + * Test the basic create and length evaluation functions. |
|
63 | + * |
|
64 | + * @return void |
|
65 | + */ |
|
66 | + public function testCreateAndCount() |
|
67 | + { |
|
68 | + // at first, the queue should contain 0 items. |
|
69 | + $this->assertSame(0, $this->QueuedTasks->getLength()); |
|
70 | + |
|
71 | + // create a job |
|
72 | + $this->assertTrue((bool)$this->QueuedTasks->createJob('test1', [ |
|
73 | + 'some' => 'random', |
|
74 | + 'test' => 'data' |
|
75 | + ])); |
|
76 | + |
|
77 | + // test if queue Length is 1 now. |
|
78 | + $this->assertSame(1, $this->QueuedTasks->getLength()); |
|
79 | + |
|
80 | + // create some more jobs |
|
81 | + $this->assertTrue((bool)$this->QueuedTasks->createJob('test2', [ |
|
82 | + 'some' => 'random', |
|
83 | + 'test' => 'data2' |
|
84 | + ])); |
|
85 | + $this->assertTrue((bool)$this->QueuedTasks->createJob('test2', [ |
|
86 | + 'some' => 'random', |
|
87 | + 'test' => 'data3' |
|
88 | + ])); |
|
89 | + $this->assertTrue((bool)$this->QueuedTasks->createJob('test3', [ |
|
90 | + 'some' => 'random', |
|
91 | + 'test' => 'data4' |
|
92 | + ])); |
|
93 | + |
|
94 | + // overall queueLength shpould now be 4 |
|
95 | + $this->assertSame(4, $this->QueuedTasks->getLength()); |
|
96 | + |
|
97 | + // there should be 1 task of type 'test1', one of type 'test3' and 2 of type 'test2' |
|
98 | + $this->assertSame(1, $this->QueuedTasks->getLength('test1')); |
|
99 | + $this->assertSame(2, $this->QueuedTasks->getLength('test2')); |
|
100 | + $this->assertSame(1, $this->QueuedTasks->getLength('test3')); |
|
101 | + } |
|
102 | + |
|
103 | + /** |
|
104 | + * Test the basic create and fetch functions. |
|
105 | + * |
|
106 | + * @return void |
|
107 | + */ |
|
108 | + public function testCreateAndFetch() |
|
109 | + { |
|
110 | + $this->_needsConnection(); |
|
111 | + |
|
112 | + // $capabilities is a list of tasks the worker can run. |
|
113 | + $capabilities = [ |
|
114 | + 'task1' => [ |
|
115 | + 'name' => 'task1', |
|
116 | + 'timeout' => 100, |
|
117 | + 'retries' => 2 |
|
118 | + ] |
|
119 | + ]; |
|
120 | + $testData = [ |
|
121 | + 'x1' => 'y1', |
|
122 | + 'x2' => 'y2', |
|
123 | + 'x3' => 'y3', |
|
124 | + 'x4' => 'y4' |
|
125 | + ]; |
|
126 | + |
|
127 | + // start off empty. |
|
128 | + $this->assertSame([], $this->QueuedTasks->find() |
|
129 | + ->toArray()); |
|
130 | + // at first, the queue should contain 0 items. |
|
131 | + $this->assertSame(0, $this->QueuedTasks->getLength()); |
|
132 | + // there are no jobs, so we cant fetch any. |
|
133 | + $this->assertNull($this->QueuedTasks->requestJob($capabilities)); |
|
134 | + // insert one job. |
|
135 | + $this->assertTrue((bool)$this->QueuedTasks->createJob('task1', $testData)); |
|
136 | + |
|
137 | + // fetch and check the first job. |
|
138 | + $job = $this->QueuedTasks->requestJob($capabilities); |
|
139 | + $this->assertSame(1, $job->id); |
|
140 | + $this->assertSame('task1', $job->task); |
|
141 | + $this->assertSame(0, $job->failed); |
|
142 | + $this->assertNull($job->completed); |
|
143 | + $this->assertSame($testData, unserialize($job->data)); |
|
144 | + |
|
145 | + // after this job has been fetched, it may not be reassigned. |
|
146 | + $result = $this->QueuedTasks->requestJob($capabilities); |
|
147 | + $this->assertNull($result); |
|
148 | + |
|
149 | + // queue length is still 1 since the first job did not finish. |
|
150 | + $this->assertSame(1, $this->QueuedTasks->getLength()); |
|
151 | + |
|
152 | + // Now mark Task1 as done |
|
153 | + $this->assertTrue($this->QueuedTasks->markJobDone($job)); |
|
154 | + |
|
155 | + // Should be 0 again. |
|
156 | + $this->assertSame(0, $this->QueuedTasks->getLength()); |
|
157 | + } |
|
158 | + |
|
159 | + /** |
|
160 | + * Test the delivery of jobs in sequence, skipping fetched but not completed tasks. |
|
161 | + * |
|
162 | + * @return void |
|
163 | + */ |
|
164 | + public function testSequence() |
|
165 | + { |
|
166 | + $this->_needsConnection(); |
|
167 | + |
|
168 | + // $capabilities is a list of tasks the worker can run. |
|
169 | + $capabilities = [ |
|
170 | + 'task1' => [ |
|
171 | + 'name' => 'task1', |
|
172 | + 'timeout' => 100, |
|
173 | + 'retries' => 2 |
|
174 | + ] |
|
175 | + ]; |
|
176 | + // at first, the queue should contain 0 items. |
|
177 | + $this->assertSame(0, $this->QueuedTasks->getLength()); |
|
178 | + // create some more jobs |
|
179 | + foreach (range(0, 9) as $num) { |
|
180 | + $this->assertTrue((bool)$this->QueuedTasks->createJob('task1', [ |
|
181 | + 'tasknum' => $num |
|
182 | + ])); |
|
183 | + } |
|
184 | + // 10 jobs in the queue. |
|
185 | + $this->assertSame(10, $this->QueuedTasks->getLength()); |
|
186 | + |
|
187 | + // jobs should be fetched in the original sequence. |
|
188 | + $array = []; |
|
189 | + foreach (range(0, 4) as $num) { |
|
190 | + $this->QueuedTasks->clearKey(); |
|
191 | + $array[$num] = $this->QueuedTasks->requestJob($capabilities); |
|
192 | + $jobData = unserialize($array[$num]['data']); |
|
193 | + $this->assertSame($num, $jobData['tasknum']); |
|
194 | + } |
|
195 | + // now mark them as done |
|
196 | + foreach (range(0, 4) as $num) { |
|
197 | + $this->assertTrue($this->QueuedTasks->markJobDone($array[$num])); |
|
198 | + $this->assertSame(9 - $num, $this->QueuedTasks->getLength()); |
|
199 | + } |
|
200 | + |
|
201 | + // jobs should be fetched in the original sequence. |
|
202 | + foreach (range(5, 9) as $num) { |
|
203 | + $job = $this->QueuedTasks->requestJob($capabilities); |
|
204 | + $jobData = unserialize($job->data); |
|
205 | + $this->assertSame($num, $jobData['tasknum']); |
|
206 | + $this->assertTrue($this->QueuedTasks->markJobDone($job)); |
|
207 | + $this->assertSame(9 - $num, $this->QueuedTasks->getLength()); |
|
208 | + } |
|
209 | + } |
|
210 | + |
|
211 | + /** |
|
212 | + * Test creating Jobs to run close to a specified time, and strtotime parsing. |
|
213 | + * Using toUnixString() function to convert Time object to timestamp, instead of strtotime |
|
214 | + * |
|
215 | + * @return null |
|
216 | + */ |
|
217 | + public function testNotBefore() |
|
218 | + { |
|
219 | + $this->assertTrue((bool)$this->QueuedTasks->createJob('task1', null, '+ 1 Min')); |
|
220 | + $this->assertTrue((bool)$this->QueuedTasks->createJob('task1', null, '+ 1 Day')); |
|
221 | + $this->assertTrue((bool)$this->QueuedTasks->createJob('task1', null, '2009-07-01 12:00:00')); |
|
222 | + $data = $this->QueuedTasks->find('all')->toArray(); |
|
223 | + $this->assertWithinRange((new Time('+ 1 Min'))->toUnixString(), $data[0]['not_before']->toUnixString(), 60); |
|
224 | + $this->assertWithinRange((new Time('+ 1 Day'))->toUnixString(), $data[1]['not_before']->toUnixString(), 60); |
|
225 | + $this->assertWithinRange((new Time('2009-07-01 12:00:00'))->toUnixString(), $data[2]['not_before']->toUnixString(), 60); |
|
226 | + } |
|
227 | + |
|
228 | + /** |
|
229 | + * Test Job reordering depending on 'notBefore' field. |
|
230 | + * Jobs with an expired not_before field should be executed before any other job without specific timing info. |
|
231 | + * |
|
232 | + * @return void |
|
233 | + */ |
|
234 | + public function testNotBeforeOrder() |
|
235 | + { |
|
236 | + $this->_needsConnection(); |
|
237 | + |
|
238 | + $capabilities = [ |
|
239 | + 'task1' => [ |
|
240 | + 'name' => 'task1', |
|
241 | + 'timeout' => 100, |
|
242 | + 'retries' => 2 |
|
243 | + ], |
|
244 | + 'dummytask' => [ |
|
245 | + 'name' => 'dummytask', |
|
246 | + 'timeout' => 100, |
|
247 | + 'retries' => 2 |
|
248 | + ] |
|
249 | + ]; |
|
250 | + $this->assertTrue((bool)$this->QueuedTasks->createJob('dummytask')); |
|
251 | + $this->assertTrue((bool)$this->QueuedTasks->createJob('dummytask')); |
|
252 | + // create a task with it's execution target some seconds in the past, so it should jump to the top of the testCreateAndFetchlist. |
|
253 | + $this->assertTrue((bool)$this->QueuedTasks->createJob('task1', [ |
|
254 | + 'three' |
|
255 | + ], '- 3 Seconds')); |
|
256 | + $this->assertTrue((bool)$this->QueuedTasks->createJob('task1', [ |
|
257 | + 'two' |
|
258 | + ], '- 5 Seconds')); |
|
259 | + $this->assertTrue((bool)$this->QueuedTasks->createJob('task1', [ |
|
260 | + 'one' |
|
261 | + ], '- 7 Seconds')); |
|
262 | + |
|
263 | + // when using requestJob, the jobs we just created should be delivered in this order, NOT the order in which they where created. |
|
264 | + $expected = [ |
|
265 | + [ |
|
266 | + 'name' => 'task1', |
|
267 | + 'data' => [ |
|
268 | + 'one' |
|
269 | + ] |
|
270 | + ], |
|
271 | + [ |
|
272 | + 'name' => 'task1', |
|
273 | + 'data' => [ |
|
274 | + 'two' |
|
275 | + ] |
|
276 | + ], |
|
277 | + [ |
|
278 | + 'name' => 'task1', |
|
279 | + 'data' => [ |
|
280 | + 'three' |
|
281 | + ] |
|
282 | + ], |
|
283 | + [ |
|
284 | + 'name' => 'dummytask', |
|
285 | + 'data' => null |
|
286 | + ], |
|
287 | + [ |
|
288 | + 'name' => 'dummytask', |
|
289 | + 'data' => null |
|
290 | + ] |
|
291 | + ]; |
|
292 | + |
|
293 | + foreach ($expected as $item) { |
|
294 | + $this->QueuedTasks->clearKey(); |
|
295 | + $tmp = $this->QueuedTasks->requestJob($capabilities); |
|
296 | + |
|
297 | + $this->assertSame($item['name'], $tmp['task']); |
|
298 | + $this->assertEquals($item['data'], unserialize($tmp['data'])); |
|
299 | + } |
|
300 | + } |
|
301 | + |
|
302 | + /** |
|
303 | + * Job Rate limiting. |
|
304 | + * Do not execute jobs of a certain type more often than once every X seconds. |
|
305 | + * |
|
306 | + * @return void |
|
307 | + */ |
|
308 | + public function testRateLimit() |
|
309 | + { |
|
310 | + $this->_needsConnection(); |
|
311 | + |
|
312 | + $capabilities = [ |
|
313 | + 'task1' => [ |
|
314 | + 'name' => 'task1', |
|
315 | + 'timeout' => 101, |
|
316 | + 'retries' => 2, |
|
317 | + 'rate' => 2 |
|
318 | + ], |
|
319 | + 'dummytask' => [ |
|
320 | + 'name' => 'dummytask', |
|
321 | + 'timeout' => 101, |
|
322 | + 'retries' => 2 |
|
323 | + ] |
|
324 | + ]; |
|
325 | + |
|
326 | + // clear out the rate history |
|
327 | + $this->QueuedTasks->rateHistory = []; |
|
328 | + |
|
329 | + $data1 = [ |
|
330 | + 'key' => 1 |
|
331 | + ]; |
|
332 | + $this->assertTrue((bool)$this->QueuedTasks->createJob('task1', $data1)); |
|
333 | + $data2 = [ |
|
334 | + 'key' => 2 |
|
335 | + ]; |
|
336 | + $this->assertTrue((bool)$this->QueuedTasks->createJob('task1', $data2)); |
|
337 | + $data3 = [ |
|
338 | + 'key' => 3 |
|
339 | + ]; |
|
340 | + $this->assertTrue((bool)$this->QueuedTasks->createJob('task1', $data3)); |
|
341 | + $this->assertTrue((bool)$this->QueuedTasks->createJob('dummytask')); |
|
342 | + $this->assertTrue((bool)$this->QueuedTasks->createJob('dummytask')); |
|
343 | + $this->assertTrue((bool)$this->QueuedTasks->createJob('dummytask')); |
|
344 | + $this->assertTrue((bool)$this->QueuedTasks->createJob('dummytask')); |
|
345 | + |
|
346 | + // At first we get task1-1. |
|
347 | + $this->QueuedTasks->clearKey(); |
|
348 | + $tmp = $this->QueuedTasks->requestJob($capabilities); |
|
349 | + $this->assertSame('task1', $tmp['task']); |
|
350 | + $this->assertSame($data1, unserialize($tmp['data'])); |
|
351 | + |
|
352 | + // The rate limit should now skip over task1-2 and fetch a dummytask. |
|
353 | + $this->QueuedTasks->clearKey(); |
|
354 | + $tmp = $this->QueuedTasks->requestJob($capabilities); |
|
355 | + $this->assertSame('dummytask', $tmp['task']); |
|
356 | + $this->assertFalse(unserialize($tmp['data'])); |
|
357 | + |
|
358 | + usleep(100000); |
|
359 | + // and again. |
|
360 | + $this->QueuedTasks->clearKey(); |
|
361 | + $tmp = $this->QueuedTasks->requestJob($capabilities); |
|
362 | + $this->assertSame('dummytask', $tmp['task']); |
|
363 | + $this->assertFalse(unserialize($tmp['data'])); |
|
364 | + |
|
365 | + // Then some time passes |
|
366 | + sleep(2); |
|
367 | + |
|
368 | + // Now we should get task1-2 |
|
369 | + $this->QueuedTasks->clearKey(); |
|
370 | + $tmp = $this->QueuedTasks->requestJob($capabilities); |
|
371 | + $this->assertSame('task1', $tmp['task']); |
|
372 | + $this->assertSame($data2, unserialize($tmp['data'])); |
|
373 | + |
|
374 | + // and again rate limit to dummytask. |
|
375 | + $this->QueuedTasks->clearKey(); |
|
376 | + $tmp = $this->QueuedTasks->requestJob($capabilities); |
|
377 | + $this->assertSame('dummytask', $tmp['task']); |
|
378 | + $this->assertFalse(unserialize($tmp['data'])); |
|
379 | + |
|
380 | + // Then some more time passes |
|
381 | + sleep(2); |
|
382 | + |
|
383 | + // Now we should get task1-3 |
|
384 | + $this->QueuedTasks->clearKey(); |
|
385 | + $tmp = $this->QueuedTasks->requestJob($capabilities); |
|
386 | + $this->assertSame('task1', $tmp['task']); |
|
387 | + $this->assertSame($data3, unserialize($tmp['data'])); |
|
388 | + |
|
389 | + // and again rate limit to dummytask. |
|
390 | + $this->QueuedTasks->clearKey(); |
|
391 | + $tmp = $this->QueuedTasks->requestJob($capabilities); |
|
392 | + $this->assertSame('dummytask', $tmp['task']); |
|
393 | + $this->assertFalse(unserialize($tmp['data'])); |
|
394 | + |
|
395 | + // and now the queue is empty |
|
396 | + $this->QueuedTasks->clearKey(); |
|
397 | + $tmp = $this->QueuedTasks->requestJob($capabilities); |
|
398 | + $this->assertNull($tmp); |
|
399 | + } |
|
400 | + |
|
401 | + /** |
|
402 | + * |
|
403 | + * @return void |
|
404 | + */ |
|
405 | + public function testIsQueued() |
|
406 | + { |
|
407 | + $result = $this->QueuedTasks->isQueued('foo-bar'); |
|
408 | + $this->assertFalse($result); |
|
409 | + |
|
410 | + $queuedJob = $this->QueuedTasks->newEntity([ |
|
411 | + 'key' => 'key', |
|
412 | + 'task' => 'FooBar' |
|
413 | + ]); |
|
414 | + $this->QueuedTasks->saveOrFail($queuedJob); |
|
415 | + |
|
416 | + $result = $this->QueuedTasks->isQueued('foo-bar'); |
|
417 | + $this->assertTrue($result); |
|
418 | + |
|
419 | + $queuedJob->completed = new FrozenTime(); |
|
420 | + $this->QueuedTasks->saveOrFail($queuedJob); |
|
421 | + |
|
422 | + $result = $this->QueuedTasks->isQueued('foo-bar'); |
|
423 | + $this->assertFalse($result); |
|
424 | + } |
|
425 | + |
|
426 | + /** |
|
427 | + * Helper method for skipping tests that need a real connection. |
|
428 | + * |
|
429 | + * @return void |
|
430 | + */ |
|
431 | + protected function _needsConnection() |
|
432 | + { |
|
433 | + $config = ConnectionManager::getConfig('test'); |
|
434 | + $this->skipIf(strpos($config['driver'], 'Mysql') === false, 'Only Mysql is working yet for this.'); |
|
435 | + } |
|
436 | 436 | } |
@@ -9,46 +9,46 @@ |
||
9 | 9 | class QueuedTasksFixture extends TestFixture |
10 | 10 | { |
11 | 11 | |
12 | - /** |
|
13 | - * Fields |
|
14 | - * |
|
15 | - * @var array |
|
16 | - */ |
|
17 | - // @codingStandardsIgnoreStart |
|
18 | - public $fields = [ |
|
19 | - 'id' => ['type' => 'integer', 'length' => 10, 'unsigned' => true, 'null' => false, 'default' => null, 'comment' => '', 'autoIncrement' => true, 'precision' => null], |
|
20 | - 'task' => ['type' => 'string', 'length' => 255, 'null' => false, 'default' => null, 'collate' => 'utf8_general_ci', 'comment' => '', 'precision' => null, 'fixed' => null], |
|
21 | - 'data' => ['type' => 'text', 'length' => 16777215, 'null' => true, 'default' => null, 'collate' => 'utf8_general_ci', 'comment' => '', 'precision' => null], |
|
22 | - 'not_before' => ['type' => 'timestamp', 'length' => null, 'null' => true, 'default' => null, 'comment' => '', 'precision' => null], |
|
23 | - 'fetched' => ['type' => 'timestamp', 'length' => null, 'null' => true, 'default' => null, 'comment' => '', 'precision' => null], |
|
24 | - 'completed' => ['type' => 'timestamp', 'length' => null, 'null' => true, 'default' => null, 'comment' => '', 'precision' => null], |
|
25 | - 'failed_count' => ['type' => 'integer', 'length' => 10, 'unsigned' => true, 'null' => false, 'default' => '0', 'comment' => '', 'precision' => null, 'autoIncrement' => null], |
|
26 | - 'failure_message' => ['type' => 'text', 'length' => null, 'null' => true, 'default' => null, 'collate' => 'utf8_general_ci', 'comment' => '', 'precision' => null], |
|
27 | - 'worker_key' => ['type' => 'string', 'fixed' => true, 'length' => 40, 'null' => true, 'default' => null, 'collate' => 'utf8_general_ci', 'comment' => '', 'precision' => null], |
|
28 | - 'created' => ['type' => 'timestamp', 'length' => null, 'null' => true, 'default' => null, 'comment' => '', 'precision' => null], |
|
29 | - '_indexes' => [ |
|
30 | - 'completed' => ['type' => 'index', 'columns' => ['completed'], 'length' => []], |
|
31 | - 'worker_key' => ['type' => 'index', 'columns' => ['worker_key'], 'length' => []], |
|
32 | - 'task' => ['type' => 'index', 'columns' => ['task'], 'length' => []], |
|
33 | - ], |
|
34 | - '_constraints' => [ |
|
35 | - 'primary' => ['type' => 'primary', 'columns' => ['id'], 'length' => []], |
|
36 | - ], |
|
37 | - '_options' => [ |
|
38 | - 'engine' => 'InnoDB', |
|
39 | - 'collation' => 'utf8_general_ci' |
|
40 | - ], |
|
41 | - ]; |
|
42 | - // @codingStandardsIgnoreEnd |
|
12 | + /** |
|
13 | + * Fields |
|
14 | + * |
|
15 | + * @var array |
|
16 | + */ |
|
17 | + // @codingStandardsIgnoreStart |
|
18 | + public $fields = [ |
|
19 | + 'id' => ['type' => 'integer', 'length' => 10, 'unsigned' => true, 'null' => false, 'default' => null, 'comment' => '', 'autoIncrement' => true, 'precision' => null], |
|
20 | + 'task' => ['type' => 'string', 'length' => 255, 'null' => false, 'default' => null, 'collate' => 'utf8_general_ci', 'comment' => '', 'precision' => null, 'fixed' => null], |
|
21 | + 'data' => ['type' => 'text', 'length' => 16777215, 'null' => true, 'default' => null, 'collate' => 'utf8_general_ci', 'comment' => '', 'precision' => null], |
|
22 | + 'not_before' => ['type' => 'timestamp', 'length' => null, 'null' => true, 'default' => null, 'comment' => '', 'precision' => null], |
|
23 | + 'fetched' => ['type' => 'timestamp', 'length' => null, 'null' => true, 'default' => null, 'comment' => '', 'precision' => null], |
|
24 | + 'completed' => ['type' => 'timestamp', 'length' => null, 'null' => true, 'default' => null, 'comment' => '', 'precision' => null], |
|
25 | + 'failed_count' => ['type' => 'integer', 'length' => 10, 'unsigned' => true, 'null' => false, 'default' => '0', 'comment' => '', 'precision' => null, 'autoIncrement' => null], |
|
26 | + 'failure_message' => ['type' => 'text', 'length' => null, 'null' => true, 'default' => null, 'collate' => 'utf8_general_ci', 'comment' => '', 'precision' => null], |
|
27 | + 'worker_key' => ['type' => 'string', 'fixed' => true, 'length' => 40, 'null' => true, 'default' => null, 'collate' => 'utf8_general_ci', 'comment' => '', 'precision' => null], |
|
28 | + 'created' => ['type' => 'timestamp', 'length' => null, 'null' => true, 'default' => null, 'comment' => '', 'precision' => null], |
|
29 | + '_indexes' => [ |
|
30 | + 'completed' => ['type' => 'index', 'columns' => ['completed'], 'length' => []], |
|
31 | + 'worker_key' => ['type' => 'index', 'columns' => ['worker_key'], 'length' => []], |
|
32 | + 'task' => ['type' => 'index', 'columns' => ['task'], 'length' => []], |
|
33 | + ], |
|
34 | + '_constraints' => [ |
|
35 | + 'primary' => ['type' => 'primary', 'columns' => ['id'], 'length' => []], |
|
36 | + ], |
|
37 | + '_options' => [ |
|
38 | + 'engine' => 'InnoDB', |
|
39 | + 'collation' => 'utf8_general_ci' |
|
40 | + ], |
|
41 | + ]; |
|
42 | + // @codingStandardsIgnoreEnd |
|
43 | 43 | |
44 | - /** |
|
45 | - * Init method |
|
46 | - * |
|
47 | - * @return void |
|
48 | - */ |
|
49 | - public function init() |
|
50 | - { |
|
51 | - $this->records = []; |
|
52 | - parent::init(); |
|
53 | - } |
|
44 | + /** |
|
45 | + * Init method |
|
46 | + * |
|
47 | + * @return void |
|
48 | + */ |
|
49 | + public function init() |
|
50 | + { |
|
51 | + $this->records = []; |
|
52 | + parent::init(); |
|
53 | + } |
|
54 | 54 | } |
@@ -18,7 +18,7 @@ |
||
18 | 18 | use RuntimeException; |
19 | 19 | use Throwable; |
20 | 20 | |
21 | -declare(ticks = 1); |
|
21 | +declare(ticks=1); |
|
22 | 22 | |
23 | 23 | /** |
24 | 24 | * Main shell to init and run queue workers. |
@@ -31,578 +31,578 @@ |
||
31 | 31 | class QueueShell extends Shell |
32 | 32 | { |
33 | 33 | |
34 | - /** |
|
35 | - * |
|
36 | - * @var string |
|
37 | - */ |
|
38 | - public $modelClass = 'Queue.QueuedTasks'; |
|
39 | - |
|
40 | - /** |
|
41 | - * |
|
42 | - * @var array|null |
|
43 | - */ |
|
44 | - protected $_taskConf; |
|
45 | - |
|
46 | - /** |
|
47 | - * |
|
48 | - * @var int |
|
49 | - */ |
|
50 | - protected $_time = 0; |
|
51 | - |
|
52 | - /** |
|
53 | - * |
|
54 | - * @var bool |
|
55 | - */ |
|
56 | - protected $_exit = false; |
|
57 | - |
|
58 | - /** |
|
59 | - * Overwrite shell initialize to dynamically load all Queue Related Tasks. |
|
60 | - * |
|
61 | - * @return void |
|
62 | - */ |
|
63 | - public function initialize() |
|
64 | - { |
|
65 | - $taskFinder = new TaskFinder(); |
|
66 | - $this->tasks = $taskFinder->allAppAndPluginTasks(); |
|
67 | - |
|
68 | - parent::initialize(); |
|
69 | - } |
|
70 | - |
|
71 | - /** |
|
72 | - * |
|
73 | - * @return void |
|
74 | - */ |
|
75 | - public function startup() |
|
76 | - { |
|
77 | - if ($this->param('quiet')) { |
|
78 | - $this->interactive = false; |
|
79 | - } |
|
80 | - |
|
81 | - parent::startup(); |
|
82 | - } |
|
83 | - |
|
84 | - /** |
|
85 | - * |
|
86 | - * @return string |
|
87 | - */ |
|
88 | - public function getDescription() |
|
89 | - { |
|
90 | - $tasks = []; |
|
91 | - foreach ($this->taskNames as $loadedTask) { |
|
92 | - $tasks[] = "\t" . '* ' . $this->_taskName($loadedTask); |
|
93 | - } |
|
94 | - $tasks = implode(PHP_EOL, $tasks); |
|
95 | - |
|
96 | - $text = <<<TEXT |
|
34 | + /** |
|
35 | + * |
|
36 | + * @var string |
|
37 | + */ |
|
38 | + public $modelClass = 'Queue.QueuedTasks'; |
|
39 | + |
|
40 | + /** |
|
41 | + * |
|
42 | + * @var array|null |
|
43 | + */ |
|
44 | + protected $_taskConf; |
|
45 | + |
|
46 | + /** |
|
47 | + * |
|
48 | + * @var int |
|
49 | + */ |
|
50 | + protected $_time = 0; |
|
51 | + |
|
52 | + /** |
|
53 | + * |
|
54 | + * @var bool |
|
55 | + */ |
|
56 | + protected $_exit = false; |
|
57 | + |
|
58 | + /** |
|
59 | + * Overwrite shell initialize to dynamically load all Queue Related Tasks. |
|
60 | + * |
|
61 | + * @return void |
|
62 | + */ |
|
63 | + public function initialize() |
|
64 | + { |
|
65 | + $taskFinder = new TaskFinder(); |
|
66 | + $this->tasks = $taskFinder->allAppAndPluginTasks(); |
|
67 | + |
|
68 | + parent::initialize(); |
|
69 | + } |
|
70 | + |
|
71 | + /** |
|
72 | + * |
|
73 | + * @return void |
|
74 | + */ |
|
75 | + public function startup() |
|
76 | + { |
|
77 | + if ($this->param('quiet')) { |
|
78 | + $this->interactive = false; |
|
79 | + } |
|
80 | + |
|
81 | + parent::startup(); |
|
82 | + } |
|
83 | + |
|
84 | + /** |
|
85 | + * |
|
86 | + * @return string |
|
87 | + */ |
|
88 | + public function getDescription() |
|
89 | + { |
|
90 | + $tasks = []; |
|
91 | + foreach ($this->taskNames as $loadedTask) { |
|
92 | + $tasks[] = "\t" . '* ' . $this->_taskName($loadedTask); |
|
93 | + } |
|
94 | + $tasks = implode(PHP_EOL, $tasks); |
|
95 | + |
|
96 | + $text = <<<TEXT |
|
97 | 97 | Simple and minimalistic job queue (or deferred-task) system. |
98 | 98 | |
99 | 99 | Available Tasks: |
100 | 100 | $tasks |
101 | 101 | TEXT; |
102 | 102 | |
103 | - return $text; |
|
104 | - } |
|
105 | - |
|
106 | - /** |
|
107 | - * Look for a Queue Task of hte passed name and try to call add() on it. |
|
108 | - * A QueueTask may provide an add function to enable the user to create new jobs via commandline. |
|
109 | - * |
|
110 | - * @return void |
|
111 | - */ |
|
112 | - public function add() |
|
113 | - { |
|
114 | - if (count($this->args) < 1) { |
|
115 | - $this->out('Please call like this:'); |
|
116 | - $this->out(' bin/cake queue add <taskname>'); |
|
117 | - $this->_displayAvailableTasks(); |
|
118 | - |
|
119 | - return; |
|
120 | - } |
|
121 | - |
|
122 | - $name = Inflector::camelize($this->args[0]); |
|
123 | - if (in_array('Queue' . $name, $this->taskNames, true)) { |
|
124 | - /** @var \Queue\Shell\Task\QueueTask|\Queue\Shell\Task\AddInterface $task */ |
|
125 | - $task = $this->{'Queue' . $name}; |
|
126 | - if (!($task instanceof AddInterface)) { |
|
127 | - $this->abort('This task does not support adding via CLI call'); |
|
128 | - } |
|
129 | - $task->add(); |
|
130 | - } else { |
|
131 | - $this->out('Error: Task not found: ' . $name); |
|
132 | - $this->_displayAvailableTasks(); |
|
133 | - } |
|
134 | - } |
|
135 | - |
|
136 | - /** |
|
137 | - * Output the task without Queue or Task |
|
138 | - * example: QueueImageTask becomes Image on display |
|
139 | - * |
|
140 | - * @param string $task Task name |
|
141 | - * @return string Cleaned task name |
|
142 | - */ |
|
143 | - protected function _taskName($task) |
|
144 | - { |
|
145 | - if (strpos($task, 'Queue') === 0) { |
|
146 | - return substr($task, 5); |
|
147 | - } |
|
148 | - |
|
149 | - return $task; |
|
150 | - } |
|
151 | - |
|
152 | - /** |
|
153 | - * Run a QueueWorker loop. |
|
154 | - * Runs a Queue Worker process which will try to find unassigned jobs in the queue |
|
155 | - * which it may run and try to fetch and execute them. |
|
156 | - * |
|
157 | - * @return int|null |
|
158 | - */ |
|
159 | - public function runworker() |
|
160 | - { |
|
161 | - try { |
|
162 | - $pid = $this->_initPid(); |
|
163 | - } catch (PersistenceFailedException $exception) { |
|
164 | - $this->err($exception->getMessage()); |
|
165 | - $limit = (int)Configure::read('Queue.maxWorkers'); |
|
166 | - if ($limit) { |
|
167 | - $this->out('Cannot start worker: Too many workers already/still running on this server (' . $limit . '/' . $limit . ')'); |
|
168 | - } |
|
169 | - |
|
170 | - return static::CODE_ERROR; |
|
171 | - } |
|
172 | - |
|
173 | - // Enable Garbage Collector (PHP >= 5.3) |
|
174 | - if (function_exists('gc_enable')) { |
|
175 | - gc_enable(); |
|
176 | - } |
|
177 | - if (function_exists('pcntl_signal')) { |
|
178 | - pcntl_signal(SIGTERM, [ |
|
179 | - &$this, |
|
180 | - '_exit' |
|
181 | - ]); |
|
182 | - pcntl_signal(SIGINT, [ |
|
183 | - &$this, |
|
184 | - '_exit' |
|
185 | - ]); |
|
186 | - pcntl_signal(SIGTSTP, [ |
|
187 | - &$this, |
|
188 | - '_exit' |
|
189 | - ]); |
|
190 | - pcntl_signal(SIGQUIT, [ |
|
191 | - &$this, |
|
192 | - '_exit' |
|
193 | - ]); |
|
194 | - } |
|
195 | - $this->_exit = false; |
|
196 | - |
|
197 | - $startTime = time(); |
|
198 | - $types = $this->_stringToArray($this->param('type')); |
|
199 | - |
|
200 | - while (!$this->_exit) { |
|
201 | - $this->out(__d('queue', 'Looking for a job.'), 1, Shell::VERBOSE); |
|
202 | - |
|
203 | - $QueuedTask = $this->QueuedTasks->requestJob($this->_getTaskConf(), $types); |
|
204 | - |
|
205 | - if ($QueuedTask) { |
|
206 | - $this->runJob($QueuedTask, $pid); |
|
207 | - } elseif (Configure::read('Queue.exitWhenNothingToDo')) { |
|
208 | - $this->out('nothing to do, exiting.'); |
|
209 | - $this->_exit = true; |
|
210 | - } else { |
|
211 | - $this->out('nothing to do, sleeping.'); |
|
212 | - sleep(Config::sleepTime()); |
|
213 | - } |
|
214 | - |
|
215 | - // check if we are over the maximum runtime and end processing if so. |
|
216 | - if (Configure::readOrFail('Queue.workerMaxRuntime') && (time() - $startTime) >= Configure::readOrFail('Queue.workerMaxRuntime')) { |
|
217 | - $this->_exit = true; |
|
218 | - $this->out('queue', 'Reached runtime of ' . (time() - $startTime) . ' Seconds (Max ' . Configure::readOrFail('Queue.workerMaxRuntime') . '), terminating.'); |
|
219 | - } |
|
220 | - if ($this->_exit || mt_rand(0, 100) > (100 - (int)Config::gcprob())) { |
|
221 | - $this->out(__d('queue', 'Performing old job cleanup.')); |
|
222 | - $this->QueuedTasks->cleanOldJobs(); |
|
223 | - } |
|
224 | - $this->hr(); |
|
225 | - } |
|
226 | - |
|
227 | - $this->_deletePid($pid); |
|
228 | - |
|
229 | - if ($this->param('verbose')) { |
|
230 | - $this->_log('endworker', $pid); |
|
231 | - } |
|
232 | - } |
|
233 | - |
|
234 | - /** |
|
235 | - * |
|
236 | - * @param \Queue\Model\Entity\QueuedTask $QueuedTask Queued task |
|
237 | - * @param string $pid PID of the process |
|
238 | - * @return void |
|
239 | - */ |
|
240 | - protected function runJob(QueuedTask $QueuedTask, $pid) |
|
241 | - { |
|
242 | - $this->out('Running Job of type "' . $QueuedTask->task . '"'); |
|
243 | - $this->_log('job ' . $QueuedTask->task . ', id ' . $QueuedTask->id, $pid, false); |
|
244 | - $taskName = 'Queue' . $QueuedTask->task; |
|
245 | - |
|
246 | - try { |
|
247 | - $this->_time = time(); |
|
248 | - |
|
249 | - $data = unserialize($QueuedTask->data); |
|
250 | - /** @var \Queue\Shell\Task\QueueTask $task */ |
|
251 | - $task = $this->{$taskName}; |
|
252 | - if (!$task instanceof QueueTaskInterface) { |
|
253 | - throw new RuntimeException('Task must implement ' . QueueTaskInterface::class); |
|
254 | - } |
|
255 | - |
|
256 | - $return = $task->run((array)$data, $QueuedTask->id); |
|
257 | - if ($return !== null) { |
|
258 | - trigger_error('run() should be void and throw exception in error case now.', E_USER_DEPRECATED); |
|
259 | - } |
|
260 | - $failureMessage = $taskName . ' failed'; |
|
261 | - } catch (Throwable $e) { |
|
262 | - $return = false; |
|
263 | - |
|
264 | - $failureMessage = get_class($e) . ': ' . $e->getMessage(); |
|
265 | - if (!($e instanceof QueueException)) { |
|
266 | - $failureMessage .= "\n" . $e->getTraceAsString(); |
|
267 | - } |
|
268 | - |
|
269 | - $this->_logError($taskName . ' (job ' . $QueuedTask->id . ')' . "\n" . $failureMessage, $pid); |
|
270 | - } catch (Exception $e) { |
|
271 | - $return = false; |
|
272 | - |
|
273 | - $failureMessage = get_class($e) . ': ' . $e->getMessage(); |
|
274 | - $this->_logError($taskName . "\n" . $failureMessage, $pid); |
|
275 | - } |
|
276 | - |
|
277 | - if ($return === false) { |
|
278 | - $this->QueuedTasks->markJobFailed($QueuedTask, $failureMessage); |
|
279 | - $failedStatus = $this->QueuedTasks->getFailedStatus($QueuedTask, $this->_getTaskConf()); |
|
280 | - $this->_log('job ' . $QueuedTask->task . ', id ' . $QueuedTask->id . ' failed and ' . $failedStatus, $pid); |
|
281 | - $this->out('Job did not finish, ' . $failedStatus . ' after try ' . $QueuedTask->failed . '.'); |
|
282 | - |
|
283 | - return; |
|
284 | - } |
|
285 | - |
|
286 | - $this->QueuedTasks->markJobDone($QueuedTask); |
|
287 | - $this->out('Job Finished.'); |
|
288 | - } |
|
289 | - |
|
290 | - /** |
|
291 | - * Manually trigger a Finished job cleanup. |
|
292 | - * |
|
293 | - * @return void |
|
294 | - */ |
|
295 | - public function clean() |
|
296 | - { |
|
297 | - if (!Configure::read('Queue.cleanupTimeout')) { |
|
298 | - $this->abort('You disabled cleanuptimout in config. Aborting.'); |
|
299 | - } |
|
300 | - |
|
301 | - $this->out('Deleting old jobs, that have finished before ' . date('Y-m-d H:i:s', time() - (int)Configure::read('Queue.cleanupTimeout'))); |
|
302 | - $this->QueuedTasks->cleanOldJobs(); |
|
303 | - } |
|
304 | - |
|
305 | - /** |
|
306 | - * Display current settings |
|
307 | - * |
|
308 | - * @return void |
|
309 | - */ |
|
310 | - public function settings() |
|
311 | - { |
|
312 | - $this->out('Current Settings:'); |
|
313 | - $conf = (array)Configure::read('Queue'); |
|
314 | - foreach ($conf as $key => $val) { |
|
315 | - if ($val === false) { |
|
316 | - $val = 'no'; |
|
317 | - } |
|
318 | - if ($val === true) { |
|
319 | - $val = 'yes'; |
|
320 | - } |
|
321 | - $this->out('* ' . $key . ': ' . print_r($val, true)); |
|
322 | - } |
|
323 | - |
|
324 | - $this->out(); |
|
325 | - } |
|
326 | - |
|
327 | - /** |
|
328 | - * Display some statistics about Finished Jobs. |
|
329 | - * |
|
330 | - * @return void |
|
331 | - */ |
|
332 | - public function stats() |
|
333 | - { |
|
334 | - $this->out('Jobs currently in the queue:'); |
|
335 | - |
|
336 | - $types = $this->QueuedTasks->getTypes()->toArray(); |
|
337 | - foreach ($types as $type) { |
|
338 | - $this->out(' ' . str_pad($type, 20, ' ', STR_PAD_RIGHT) . ': ' . $this->QueuedTasks->getLength($type)); |
|
339 | - } |
|
340 | - $this->hr(); |
|
341 | - $this->out('Total unfinished jobs: ' . $this->QueuedTasks->getLength()); |
|
342 | - $this->hr(); |
|
343 | - $this->out('Finished job statistics:'); |
|
344 | - $data = $this->QueuedTasks->getStats(); |
|
345 | - foreach ($data as $item) { |
|
346 | - $this->out(' ' . $item['task'] . ': '); |
|
347 | - $this->out(' Finished Jobs in Database: ' . $item['num']); |
|
348 | - $this->out(' Average Job existence : ' . str_pad(Number::precision($item['alltime']), 8, ' ', STR_PAD_LEFT) . 's'); |
|
349 | - $this->out(' Average Execution delay : ' . str_pad(Number::precision($item['fetchdelay']), 8, ' ', STR_PAD_LEFT) . 's'); |
|
350 | - $this->out(' Average Execution time : ' . str_pad(Number::precision($item['runtime']), 8, ' ', STR_PAD_LEFT) . 's'); |
|
351 | - } |
|
352 | - } |
|
353 | - |
|
354 | - /** |
|
355 | - * Get option parser method to parse commandline options |
|
356 | - * |
|
357 | - * @return \Cake\Console\ConsoleOptionParser |
|
358 | - */ |
|
359 | - public function getOptionParser() |
|
360 | - { |
|
361 | - $subcommandParser = [ |
|
362 | - 'options' => [ |
|
363 | - /* |
|
103 | + return $text; |
|
104 | + } |
|
105 | + |
|
106 | + /** |
|
107 | + * Look for a Queue Task of hte passed name and try to call add() on it. |
|
108 | + * A QueueTask may provide an add function to enable the user to create new jobs via commandline. |
|
109 | + * |
|
110 | + * @return void |
|
111 | + */ |
|
112 | + public function add() |
|
113 | + { |
|
114 | + if (count($this->args) < 1) { |
|
115 | + $this->out('Please call like this:'); |
|
116 | + $this->out(' bin/cake queue add <taskname>'); |
|
117 | + $this->_displayAvailableTasks(); |
|
118 | + |
|
119 | + return; |
|
120 | + } |
|
121 | + |
|
122 | + $name = Inflector::camelize($this->args[0]); |
|
123 | + if (in_array('Queue' . $name, $this->taskNames, true)) { |
|
124 | + /** @var \Queue\Shell\Task\QueueTask|\Queue\Shell\Task\AddInterface $task */ |
|
125 | + $task = $this->{'Queue' . $name}; |
|
126 | + if (!($task instanceof AddInterface)) { |
|
127 | + $this->abort('This task does not support adding via CLI call'); |
|
128 | + } |
|
129 | + $task->add(); |
|
130 | + } else { |
|
131 | + $this->out('Error: Task not found: ' . $name); |
|
132 | + $this->_displayAvailableTasks(); |
|
133 | + } |
|
134 | + } |
|
135 | + |
|
136 | + /** |
|
137 | + * Output the task without Queue or Task |
|
138 | + * example: QueueImageTask becomes Image on display |
|
139 | + * |
|
140 | + * @param string $task Task name |
|
141 | + * @return string Cleaned task name |
|
142 | + */ |
|
143 | + protected function _taskName($task) |
|
144 | + { |
|
145 | + if (strpos($task, 'Queue') === 0) { |
|
146 | + return substr($task, 5); |
|
147 | + } |
|
148 | + |
|
149 | + return $task; |
|
150 | + } |
|
151 | + |
|
152 | + /** |
|
153 | + * Run a QueueWorker loop. |
|
154 | + * Runs a Queue Worker process which will try to find unassigned jobs in the queue |
|
155 | + * which it may run and try to fetch and execute them. |
|
156 | + * |
|
157 | + * @return int|null |
|
158 | + */ |
|
159 | + public function runworker() |
|
160 | + { |
|
161 | + try { |
|
162 | + $pid = $this->_initPid(); |
|
163 | + } catch (PersistenceFailedException $exception) { |
|
164 | + $this->err($exception->getMessage()); |
|
165 | + $limit = (int)Configure::read('Queue.maxWorkers'); |
|
166 | + if ($limit) { |
|
167 | + $this->out('Cannot start worker: Too many workers already/still running on this server (' . $limit . '/' . $limit . ')'); |
|
168 | + } |
|
169 | + |
|
170 | + return static::CODE_ERROR; |
|
171 | + } |
|
172 | + |
|
173 | + // Enable Garbage Collector (PHP >= 5.3) |
|
174 | + if (function_exists('gc_enable')) { |
|
175 | + gc_enable(); |
|
176 | + } |
|
177 | + if (function_exists('pcntl_signal')) { |
|
178 | + pcntl_signal(SIGTERM, [ |
|
179 | + &$this, |
|
180 | + '_exit' |
|
181 | + ]); |
|
182 | + pcntl_signal(SIGINT, [ |
|
183 | + &$this, |
|
184 | + '_exit' |
|
185 | + ]); |
|
186 | + pcntl_signal(SIGTSTP, [ |
|
187 | + &$this, |
|
188 | + '_exit' |
|
189 | + ]); |
|
190 | + pcntl_signal(SIGQUIT, [ |
|
191 | + &$this, |
|
192 | + '_exit' |
|
193 | + ]); |
|
194 | + } |
|
195 | + $this->_exit = false; |
|
196 | + |
|
197 | + $startTime = time(); |
|
198 | + $types = $this->_stringToArray($this->param('type')); |
|
199 | + |
|
200 | + while (!$this->_exit) { |
|
201 | + $this->out(__d('queue', 'Looking for a job.'), 1, Shell::VERBOSE); |
|
202 | + |
|
203 | + $QueuedTask = $this->QueuedTasks->requestJob($this->_getTaskConf(), $types); |
|
204 | + |
|
205 | + if ($QueuedTask) { |
|
206 | + $this->runJob($QueuedTask, $pid); |
|
207 | + } elseif (Configure::read('Queue.exitWhenNothingToDo')) { |
|
208 | + $this->out('nothing to do, exiting.'); |
|
209 | + $this->_exit = true; |
|
210 | + } else { |
|
211 | + $this->out('nothing to do, sleeping.'); |
|
212 | + sleep(Config::sleepTime()); |
|
213 | + } |
|
214 | + |
|
215 | + // check if we are over the maximum runtime and end processing if so. |
|
216 | + if (Configure::readOrFail('Queue.workerMaxRuntime') && (time() - $startTime) >= Configure::readOrFail('Queue.workerMaxRuntime')) { |
|
217 | + $this->_exit = true; |
|
218 | + $this->out('queue', 'Reached runtime of ' . (time() - $startTime) . ' Seconds (Max ' . Configure::readOrFail('Queue.workerMaxRuntime') . '), terminating.'); |
|
219 | + } |
|
220 | + if ($this->_exit || mt_rand(0, 100) > (100 - (int)Config::gcprob())) { |
|
221 | + $this->out(__d('queue', 'Performing old job cleanup.')); |
|
222 | + $this->QueuedTasks->cleanOldJobs(); |
|
223 | + } |
|
224 | + $this->hr(); |
|
225 | + } |
|
226 | + |
|
227 | + $this->_deletePid($pid); |
|
228 | + |
|
229 | + if ($this->param('verbose')) { |
|
230 | + $this->_log('endworker', $pid); |
|
231 | + } |
|
232 | + } |
|
233 | + |
|
234 | + /** |
|
235 | + * |
|
236 | + * @param \Queue\Model\Entity\QueuedTask $QueuedTask Queued task |
|
237 | + * @param string $pid PID of the process |
|
238 | + * @return void |
|
239 | + */ |
|
240 | + protected function runJob(QueuedTask $QueuedTask, $pid) |
|
241 | + { |
|
242 | + $this->out('Running Job of type "' . $QueuedTask->task . '"'); |
|
243 | + $this->_log('job ' . $QueuedTask->task . ', id ' . $QueuedTask->id, $pid, false); |
|
244 | + $taskName = 'Queue' . $QueuedTask->task; |
|
245 | + |
|
246 | + try { |
|
247 | + $this->_time = time(); |
|
248 | + |
|
249 | + $data = unserialize($QueuedTask->data); |
|
250 | + /** @var \Queue\Shell\Task\QueueTask $task */ |
|
251 | + $task = $this->{$taskName}; |
|
252 | + if (!$task instanceof QueueTaskInterface) { |
|
253 | + throw new RuntimeException('Task must implement ' . QueueTaskInterface::class); |
|
254 | + } |
|
255 | + |
|
256 | + $return = $task->run((array)$data, $QueuedTask->id); |
|
257 | + if ($return !== null) { |
|
258 | + trigger_error('run() should be void and throw exception in error case now.', E_USER_DEPRECATED); |
|
259 | + } |
|
260 | + $failureMessage = $taskName . ' failed'; |
|
261 | + } catch (Throwable $e) { |
|
262 | + $return = false; |
|
263 | + |
|
264 | + $failureMessage = get_class($e) . ': ' . $e->getMessage(); |
|
265 | + if (!($e instanceof QueueException)) { |
|
266 | + $failureMessage .= "\n" . $e->getTraceAsString(); |
|
267 | + } |
|
268 | + |
|
269 | + $this->_logError($taskName . ' (job ' . $QueuedTask->id . ')' . "\n" . $failureMessage, $pid); |
|
270 | + } catch (Exception $e) { |
|
271 | + $return = false; |
|
272 | + |
|
273 | + $failureMessage = get_class($e) . ': ' . $e->getMessage(); |
|
274 | + $this->_logError($taskName . "\n" . $failureMessage, $pid); |
|
275 | + } |
|
276 | + |
|
277 | + if ($return === false) { |
|
278 | + $this->QueuedTasks->markJobFailed($QueuedTask, $failureMessage); |
|
279 | + $failedStatus = $this->QueuedTasks->getFailedStatus($QueuedTask, $this->_getTaskConf()); |
|
280 | + $this->_log('job ' . $QueuedTask->task . ', id ' . $QueuedTask->id . ' failed and ' . $failedStatus, $pid); |
|
281 | + $this->out('Job did not finish, ' . $failedStatus . ' after try ' . $QueuedTask->failed . '.'); |
|
282 | + |
|
283 | + return; |
|
284 | + } |
|
285 | + |
|
286 | + $this->QueuedTasks->markJobDone($QueuedTask); |
|
287 | + $this->out('Job Finished.'); |
|
288 | + } |
|
289 | + |
|
290 | + /** |
|
291 | + * Manually trigger a Finished job cleanup. |
|
292 | + * |
|
293 | + * @return void |
|
294 | + */ |
|
295 | + public function clean() |
|
296 | + { |
|
297 | + if (!Configure::read('Queue.cleanupTimeout')) { |
|
298 | + $this->abort('You disabled cleanuptimout in config. Aborting.'); |
|
299 | + } |
|
300 | + |
|
301 | + $this->out('Deleting old jobs, that have finished before ' . date('Y-m-d H:i:s', time() - (int)Configure::read('Queue.cleanupTimeout'))); |
|
302 | + $this->QueuedTasks->cleanOldJobs(); |
|
303 | + } |
|
304 | + |
|
305 | + /** |
|
306 | + * Display current settings |
|
307 | + * |
|
308 | + * @return void |
|
309 | + */ |
|
310 | + public function settings() |
|
311 | + { |
|
312 | + $this->out('Current Settings:'); |
|
313 | + $conf = (array)Configure::read('Queue'); |
|
314 | + foreach ($conf as $key => $val) { |
|
315 | + if ($val === false) { |
|
316 | + $val = 'no'; |
|
317 | + } |
|
318 | + if ($val === true) { |
|
319 | + $val = 'yes'; |
|
320 | + } |
|
321 | + $this->out('* ' . $key . ': ' . print_r($val, true)); |
|
322 | + } |
|
323 | + |
|
324 | + $this->out(); |
|
325 | + } |
|
326 | + |
|
327 | + /** |
|
328 | + * Display some statistics about Finished Jobs. |
|
329 | + * |
|
330 | + * @return void |
|
331 | + */ |
|
332 | + public function stats() |
|
333 | + { |
|
334 | + $this->out('Jobs currently in the queue:'); |
|
335 | + |
|
336 | + $types = $this->QueuedTasks->getTypes()->toArray(); |
|
337 | + foreach ($types as $type) { |
|
338 | + $this->out(' ' . str_pad($type, 20, ' ', STR_PAD_RIGHT) . ': ' . $this->QueuedTasks->getLength($type)); |
|
339 | + } |
|
340 | + $this->hr(); |
|
341 | + $this->out('Total unfinished jobs: ' . $this->QueuedTasks->getLength()); |
|
342 | + $this->hr(); |
|
343 | + $this->out('Finished job statistics:'); |
|
344 | + $data = $this->QueuedTasks->getStats(); |
|
345 | + foreach ($data as $item) { |
|
346 | + $this->out(' ' . $item['task'] . ': '); |
|
347 | + $this->out(' Finished Jobs in Database: ' . $item['num']); |
|
348 | + $this->out(' Average Job existence : ' . str_pad(Number::precision($item['alltime']), 8, ' ', STR_PAD_LEFT) . 's'); |
|
349 | + $this->out(' Average Execution delay : ' . str_pad(Number::precision($item['fetchdelay']), 8, ' ', STR_PAD_LEFT) . 's'); |
|
350 | + $this->out(' Average Execution time : ' . str_pad(Number::precision($item['runtime']), 8, ' ', STR_PAD_LEFT) . 's'); |
|
351 | + } |
|
352 | + } |
|
353 | + |
|
354 | + /** |
|
355 | + * Get option parser method to parse commandline options |
|
356 | + * |
|
357 | + * @return \Cake\Console\ConsoleOptionParser |
|
358 | + */ |
|
359 | + public function getOptionParser() |
|
360 | + { |
|
361 | + $subcommandParser = [ |
|
362 | + 'options' => [ |
|
363 | + /* |
|
364 | 364 | * 'dry-run'=> array( |
365 | 365 | * 'short' => 'd', |
366 | 366 | * 'help' => 'Dry run the update, no jobs will actually be added.', |
367 | 367 | * 'boolean' => true |
368 | 368 | * ), |
369 | 369 | */ |
370 | - ] |
|
371 | - ]; |
|
372 | - $subcommandParserFull = $subcommandParser; |
|
373 | - $subcommandParserFull['options']['type'] = [ |
|
374 | - 'short' => 't', |
|
375 | - 'help' => 'Type (comma separated list possible)', |
|
376 | - 'default' => null |
|
377 | - ]; |
|
378 | - |
|
379 | - return parent::getOptionParser()->setDescription($this->getDescription()) |
|
380 | - ->addSubcommand('clean', [ |
|
381 | - 'help' => 'Remove old jobs (cleanup)', |
|
382 | - 'parser' => $subcommandParser |
|
383 | - ]) |
|
384 | - ->addSubcommand('add', [ |
|
385 | - 'help' => 'Add Job', |
|
386 | - 'parser' => $subcommandParser |
|
387 | - ]) |
|
388 | - ->addSubcommand('stats', [ |
|
389 | - 'help' => 'Stats', |
|
390 | - 'parser' => $subcommandParserFull |
|
391 | - ]) |
|
392 | - ->addSubcommand('settings', [ |
|
393 | - 'help' => 'Settings', |
|
394 | - 'parser' => $subcommandParserFull |
|
395 | - ]) |
|
396 | - ->addSubcommand('runworker', [ |
|
397 | - 'help' => 'Run Worker', |
|
398 | - 'parser' => $subcommandParserFull |
|
399 | - ]); |
|
400 | - } |
|
401 | - |
|
402 | - /** |
|
403 | - * Timestamped log. |
|
404 | - * |
|
405 | - * @param string $message Log type |
|
406 | - * @param string|null $pid PID of the process |
|
407 | - * @param bool $addDetails Details |
|
408 | - * @return void |
|
409 | - */ |
|
410 | - protected function _log($message, $pid = null, $addDetails = true) |
|
411 | - { |
|
412 | - if (!Configure::read('Queue.log')) { |
|
413 | - return; |
|
414 | - } |
|
415 | - |
|
416 | - if ($addDetails) { |
|
417 | - $timeNeeded = $this->_timeNeeded(); |
|
418 | - $memoryUsage = $this->_memoryUsage(); |
|
419 | - $message .= ' [' . $timeNeeded . ', ' . $memoryUsage . ']'; |
|
420 | - } |
|
421 | - |
|
422 | - if ($pid) { |
|
423 | - $message .= ' (pid ' . $pid . ')'; |
|
424 | - } |
|
425 | - Log::write('info', $message, [ |
|
426 | - 'scope' => 'queue' |
|
427 | - ]); |
|
428 | - } |
|
429 | - |
|
430 | - /** |
|
431 | - * |
|
432 | - * @param string $message Message |
|
433 | - * @param string|null $pid PID of the process |
|
434 | - * @return void |
|
435 | - */ |
|
436 | - protected function _logError($message, $pid = null) |
|
437 | - { |
|
438 | - $timeNeeded = $this->_timeNeeded(); |
|
439 | - $memoryUsage = $this->_memoryUsage(); |
|
440 | - $message .= ' [' . $timeNeeded . ', ' . $memoryUsage . ']'; |
|
441 | - |
|
442 | - if ($pid) { |
|
443 | - $message .= ' (pid ' . $pid . ')'; |
|
444 | - } |
|
445 | - |
|
446 | - Log::write('error', $message); |
|
447 | - } |
|
448 | - |
|
449 | - /** |
|
450 | - * Returns a List of available QueueTasks and their individual configurations. |
|
451 | - * |
|
452 | - * @return array |
|
453 | - */ |
|
454 | - protected function _getTaskConf() |
|
455 | - { |
|
456 | - if (!is_array($this->_taskConf)) { |
|
457 | - $this->_taskConf = []; |
|
458 | - foreach ($this->tasks as $task) { |
|
459 | - list ($pluginName, $taskName) = pluginSplit($task); |
|
460 | - |
|
461 | - $this->_taskConf[$taskName]['name'] = substr($taskName, 5); |
|
462 | - $this->_taskConf[$taskName]['plugin'] = $pluginName; |
|
463 | - if (property_exists($this->{$taskName}, 'timeout')) { |
|
464 | - $this->_taskConf[$taskName]['timeout'] = $this->{$taskName}->timeout; |
|
465 | - } else { |
|
466 | - $this->_taskConf[$taskName]['timeout'] = Config::defaultWorkerTimeout(); |
|
467 | - } |
|
468 | - if (property_exists($this->{$taskName}, 'retries')) { |
|
469 | - $this->_taskConf[$taskName]['retries'] = $this->{$taskName}->retries; |
|
470 | - } else { |
|
471 | - $this->_taskConf[$taskName]['retries'] = Config::defaultWorkerRetries(); |
|
472 | - } |
|
473 | - if (property_exists($this->{$taskName}, 'cleanupTimeout')) { |
|
474 | - $this->_taskConf[$taskName]['cleanupTimeout'] = $this->{$taskName}->cleanupTimeout; |
|
475 | - } else { |
|
476 | - $this->_taskConf[$taskName]['cleanupTimeout'] = Config::cleanupTimeout(); |
|
477 | - } |
|
478 | - } |
|
479 | - } |
|
480 | - |
|
481 | - return $this->_taskConf; |
|
482 | - } |
|
483 | - |
|
484 | - /** |
|
485 | - * Signal handling to queue worker for clean shutdown |
|
486 | - * |
|
487 | - * @param int $signal The signal |
|
488 | - * @return void |
|
489 | - */ |
|
490 | - protected function _exit($signal) |
|
491 | - { |
|
492 | - $this->out(__d('queue', 'Caught %d signal, exiting.', $signal)); |
|
493 | - $this->_exit = true; |
|
494 | - } |
|
495 | - |
|
496 | - /** |
|
497 | - * |
|
498 | - * @return void |
|
499 | - */ |
|
500 | - protected function _displayAvailableTasks() |
|
501 | - { |
|
502 | - $this->out('Available Tasks:'); |
|
503 | - foreach ($this->taskNames as $loadedTask) { |
|
504 | - $this->out("\t" . '* ' . $this->_taskName($loadedTask)); |
|
505 | - } |
|
506 | - } |
|
507 | - |
|
508 | - /** |
|
509 | - * |
|
510 | - * @return string |
|
511 | - */ |
|
512 | - protected function _initPid() |
|
513 | - { |
|
514 | - $this->_pid = $this->_retrievePid(); |
|
515 | - |
|
516 | - return $this->_pid; |
|
517 | - } |
|
518 | - |
|
519 | - /** |
|
520 | - * |
|
521 | - * @return string |
|
522 | - */ |
|
523 | - protected function _retrievePid() |
|
524 | - { |
|
525 | - if (function_exists('posix_getpid')) { |
|
526 | - $pid = (string)posix_getpid(); |
|
527 | - } else { |
|
528 | - $pid = $this->QueuedTasks->key(); |
|
529 | - } |
|
530 | - |
|
531 | - return $pid; |
|
532 | - } |
|
533 | - |
|
534 | - /** |
|
535 | - * |
|
536 | - * @return string Memory usage in MB. |
|
537 | - */ |
|
538 | - protected function _memoryUsage() |
|
539 | - { |
|
540 | - $limit = ini_get('memory_limit'); |
|
541 | - |
|
542 | - $used = number_format(memory_get_peak_usage(true) / (1024 * 1024), 0) . 'MB'; |
|
543 | - if ($limit !== '-1') { |
|
544 | - $used .= '/' . $limit; |
|
545 | - } |
|
546 | - |
|
547 | - return $used; |
|
548 | - } |
|
549 | - |
|
550 | - /** |
|
551 | - * |
|
552 | - * @param string|null $pid PID of the process |
|
553 | - * |
|
554 | - * @return void |
|
555 | - */ |
|
556 | - protected function _deletePid($pid) |
|
557 | - { |
|
558 | - if (!$pid) { |
|
559 | - $pid = $this->_pid; |
|
560 | - } |
|
561 | - if (!$pid) { |
|
562 | - return; |
|
563 | - } |
|
564 | - } |
|
565 | - |
|
566 | - /** |
|
567 | - * |
|
568 | - * @return string |
|
569 | - */ |
|
570 | - protected function _timeNeeded() |
|
571 | - { |
|
572 | - $diff = $this->_time() - $this->_time($this->_time); |
|
573 | - $seconds = max($diff, 1); |
|
574 | - |
|
575 | - return $seconds . 's'; |
|
576 | - } |
|
577 | - |
|
578 | - /** |
|
579 | - * |
|
580 | - * @param int|null $providedTime Provided time |
|
581 | - * |
|
582 | - * @return int |
|
583 | - */ |
|
584 | - protected function _time($providedTime = null) |
|
585 | - { |
|
586 | - if ($providedTime) { |
|
587 | - return $providedTime; |
|
588 | - } |
|
589 | - |
|
590 | - return time(); |
|
591 | - } |
|
592 | - |
|
593 | - /** |
|
594 | - * |
|
595 | - * @param string|null $param String to convert |
|
596 | - * @return array |
|
597 | - */ |
|
598 | - protected function _stringToArray($param) |
|
599 | - { |
|
600 | - if (!$param) { |
|
601 | - return []; |
|
602 | - } |
|
603 | - |
|
604 | - $array = Text::tokenize($param); |
|
605 | - |
|
606 | - return array_filter($array); |
|
607 | - } |
|
370 | + ] |
|
371 | + ]; |
|
372 | + $subcommandParserFull = $subcommandParser; |
|
373 | + $subcommandParserFull['options']['type'] = [ |
|
374 | + 'short' => 't', |
|
375 | + 'help' => 'Type (comma separated list possible)', |
|
376 | + 'default' => null |
|
377 | + ]; |
|
378 | + |
|
379 | + return parent::getOptionParser()->setDescription($this->getDescription()) |
|
380 | + ->addSubcommand('clean', [ |
|
381 | + 'help' => 'Remove old jobs (cleanup)', |
|
382 | + 'parser' => $subcommandParser |
|
383 | + ]) |
|
384 | + ->addSubcommand('add', [ |
|
385 | + 'help' => 'Add Job', |
|
386 | + 'parser' => $subcommandParser |
|
387 | + ]) |
|
388 | + ->addSubcommand('stats', [ |
|
389 | + 'help' => 'Stats', |
|
390 | + 'parser' => $subcommandParserFull |
|
391 | + ]) |
|
392 | + ->addSubcommand('settings', [ |
|
393 | + 'help' => 'Settings', |
|
394 | + 'parser' => $subcommandParserFull |
|
395 | + ]) |
|
396 | + ->addSubcommand('runworker', [ |
|
397 | + 'help' => 'Run Worker', |
|
398 | + 'parser' => $subcommandParserFull |
|
399 | + ]); |
|
400 | + } |
|
401 | + |
|
402 | + /** |
|
403 | + * Timestamped log. |
|
404 | + * |
|
405 | + * @param string $message Log type |
|
406 | + * @param string|null $pid PID of the process |
|
407 | + * @param bool $addDetails Details |
|
408 | + * @return void |
|
409 | + */ |
|
410 | + protected function _log($message, $pid = null, $addDetails = true) |
|
411 | + { |
|
412 | + if (!Configure::read('Queue.log')) { |
|
413 | + return; |
|
414 | + } |
|
415 | + |
|
416 | + if ($addDetails) { |
|
417 | + $timeNeeded = $this->_timeNeeded(); |
|
418 | + $memoryUsage = $this->_memoryUsage(); |
|
419 | + $message .= ' [' . $timeNeeded . ', ' . $memoryUsage . ']'; |
|
420 | + } |
|
421 | + |
|
422 | + if ($pid) { |
|
423 | + $message .= ' (pid ' . $pid . ')'; |
|
424 | + } |
|
425 | + Log::write('info', $message, [ |
|
426 | + 'scope' => 'queue' |
|
427 | + ]); |
|
428 | + } |
|
429 | + |
|
430 | + /** |
|
431 | + * |
|
432 | + * @param string $message Message |
|
433 | + * @param string|null $pid PID of the process |
|
434 | + * @return void |
|
435 | + */ |
|
436 | + protected function _logError($message, $pid = null) |
|
437 | + { |
|
438 | + $timeNeeded = $this->_timeNeeded(); |
|
439 | + $memoryUsage = $this->_memoryUsage(); |
|
440 | + $message .= ' [' . $timeNeeded . ', ' . $memoryUsage . ']'; |
|
441 | + |
|
442 | + if ($pid) { |
|
443 | + $message .= ' (pid ' . $pid . ')'; |
|
444 | + } |
|
445 | + |
|
446 | + Log::write('error', $message); |
|
447 | + } |
|
448 | + |
|
449 | + /** |
|
450 | + * Returns a List of available QueueTasks and their individual configurations. |
|
451 | + * |
|
452 | + * @return array |
|
453 | + */ |
|
454 | + protected function _getTaskConf() |
|
455 | + { |
|
456 | + if (!is_array($this->_taskConf)) { |
|
457 | + $this->_taskConf = []; |
|
458 | + foreach ($this->tasks as $task) { |
|
459 | + list ($pluginName, $taskName) = pluginSplit($task); |
|
460 | + |
|
461 | + $this->_taskConf[$taskName]['name'] = substr($taskName, 5); |
|
462 | + $this->_taskConf[$taskName]['plugin'] = $pluginName; |
|
463 | + if (property_exists($this->{$taskName}, 'timeout')) { |
|
464 | + $this->_taskConf[$taskName]['timeout'] = $this->{$taskName}->timeout; |
|
465 | + } else { |
|
466 | + $this->_taskConf[$taskName]['timeout'] = Config::defaultWorkerTimeout(); |
|
467 | + } |
|
468 | + if (property_exists($this->{$taskName}, 'retries')) { |
|
469 | + $this->_taskConf[$taskName]['retries'] = $this->{$taskName}->retries; |
|
470 | + } else { |
|
471 | + $this->_taskConf[$taskName]['retries'] = Config::defaultWorkerRetries(); |
|
472 | + } |
|
473 | + if (property_exists($this->{$taskName}, 'cleanupTimeout')) { |
|
474 | + $this->_taskConf[$taskName]['cleanupTimeout'] = $this->{$taskName}->cleanupTimeout; |
|
475 | + } else { |
|
476 | + $this->_taskConf[$taskName]['cleanupTimeout'] = Config::cleanupTimeout(); |
|
477 | + } |
|
478 | + } |
|
479 | + } |
|
480 | + |
|
481 | + return $this->_taskConf; |
|
482 | + } |
|
483 | + |
|
484 | + /** |
|
485 | + * Signal handling to queue worker for clean shutdown |
|
486 | + * |
|
487 | + * @param int $signal The signal |
|
488 | + * @return void |
|
489 | + */ |
|
490 | + protected function _exit($signal) |
|
491 | + { |
|
492 | + $this->out(__d('queue', 'Caught %d signal, exiting.', $signal)); |
|
493 | + $this->_exit = true; |
|
494 | + } |
|
495 | + |
|
496 | + /** |
|
497 | + * |
|
498 | + * @return void |
|
499 | + */ |
|
500 | + protected function _displayAvailableTasks() |
|
501 | + { |
|
502 | + $this->out('Available Tasks:'); |
|
503 | + foreach ($this->taskNames as $loadedTask) { |
|
504 | + $this->out("\t" . '* ' . $this->_taskName($loadedTask)); |
|
505 | + } |
|
506 | + } |
|
507 | + |
|
508 | + /** |
|
509 | + * |
|
510 | + * @return string |
|
511 | + */ |
|
512 | + protected function _initPid() |
|
513 | + { |
|
514 | + $this->_pid = $this->_retrievePid(); |
|
515 | + |
|
516 | + return $this->_pid; |
|
517 | + } |
|
518 | + |
|
519 | + /** |
|
520 | + * |
|
521 | + * @return string |
|
522 | + */ |
|
523 | + protected function _retrievePid() |
|
524 | + { |
|
525 | + if (function_exists('posix_getpid')) { |
|
526 | + $pid = (string)posix_getpid(); |
|
527 | + } else { |
|
528 | + $pid = $this->QueuedTasks->key(); |
|
529 | + } |
|
530 | + |
|
531 | + return $pid; |
|
532 | + } |
|
533 | + |
|
534 | + /** |
|
535 | + * |
|
536 | + * @return string Memory usage in MB. |
|
537 | + */ |
|
538 | + protected function _memoryUsage() |
|
539 | + { |
|
540 | + $limit = ini_get('memory_limit'); |
|
541 | + |
|
542 | + $used = number_format(memory_get_peak_usage(true) / (1024 * 1024), 0) . 'MB'; |
|
543 | + if ($limit !== '-1') { |
|
544 | + $used .= '/' . $limit; |
|
545 | + } |
|
546 | + |
|
547 | + return $used; |
|
548 | + } |
|
549 | + |
|
550 | + /** |
|
551 | + * |
|
552 | + * @param string|null $pid PID of the process |
|
553 | + * |
|
554 | + * @return void |
|
555 | + */ |
|
556 | + protected function _deletePid($pid) |
|
557 | + { |
|
558 | + if (!$pid) { |
|
559 | + $pid = $this->_pid; |
|
560 | + } |
|
561 | + if (!$pid) { |
|
562 | + return; |
|
563 | + } |
|
564 | + } |
|
565 | + |
|
566 | + /** |
|
567 | + * |
|
568 | + * @return string |
|
569 | + */ |
|
570 | + protected function _timeNeeded() |
|
571 | + { |
|
572 | + $diff = $this->_time() - $this->_time($this->_time); |
|
573 | + $seconds = max($diff, 1); |
|
574 | + |
|
575 | + return $seconds . 's'; |
|
576 | + } |
|
577 | + |
|
578 | + /** |
|
579 | + * |
|
580 | + * @param int|null $providedTime Provided time |
|
581 | + * |
|
582 | + * @return int |
|
583 | + */ |
|
584 | + protected function _time($providedTime = null) |
|
585 | + { |
|
586 | + if ($providedTime) { |
|
587 | + return $providedTime; |
|
588 | + } |
|
589 | + |
|
590 | + return time(); |
|
591 | + } |
|
592 | + |
|
593 | + /** |
|
594 | + * |
|
595 | + * @param string|null $param String to convert |
|
596 | + * @return array |
|
597 | + */ |
|
598 | + protected function _stringToArray($param) |
|
599 | + { |
|
600 | + if (!$param) { |
|
601 | + return []; |
|
602 | + } |
|
603 | + |
|
604 | + $array = Text::tokenize($param); |
|
605 | + |
|
606 | + return array_filter($array); |
|
607 | + } |
|
608 | 608 | } |