|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Dtc\QueueBundle\Redis; |
|
4
|
|
|
|
|
5
|
|
|
use Dtc\QueueBundle\Model\BaseJob; |
|
6
|
|
|
|
|
7
|
|
|
trait StatusTrait |
|
8
|
|
|
{ |
|
9
|
3 |
|
protected function collateStatusResults(array &$results, $cacheKey) |
|
10
|
|
|
{ |
|
11
|
3 |
|
$cursor = null; |
|
12
|
3 |
|
while ($jobs = $this->redis->zScan($cacheKey, $cursor, '', 100)) { |
|
|
|
|
|
|
13
|
3 |
|
$jobs = $this->redis->mget(array_map(function ($item) { |
|
14
|
3 |
|
return $this->getJobCacheKey($item); |
|
|
|
|
|
|
15
|
3 |
|
}, array_keys($jobs))); |
|
16
|
3 |
|
$this->extractStatusResults($jobs, $results); |
|
17
|
3 |
|
if (0 === $cursor) { |
|
18
|
3 |
|
break; |
|
19
|
|
|
} |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
3 |
|
return $results; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
3 |
|
protected function extractStatusResults(array $jobs, array &$results) |
|
26
|
|
|
{ |
|
27
|
3 |
|
foreach ($jobs as $jobMessage) { |
|
28
|
3 |
|
if (is_string($jobMessage)) { |
|
29
|
3 |
|
$job = new Job(); |
|
30
|
3 |
|
$job->fromMessage($jobMessage); |
|
31
|
3 |
|
$resultHashKey = $job->getWorkerName().'->'.$job->getMethod().'()'; |
|
32
|
3 |
|
if (!isset($results[$resultHashKey][BaseJob::STATUS_NEW])) { |
|
33
|
3 |
|
$results[$resultHashKey] = static::getAllStatuses(); |
|
34
|
3 |
|
} |
|
35
|
3 |
|
if (!isset($results[$resultHashKey][BaseJob::STATUS_NEW])) { |
|
36
|
|
|
$results[$resultHashKey][BaseJob::STATUS_NEW] = 0; |
|
37
|
|
|
} |
|
38
|
3 |
|
++$results[$resultHashKey][BaseJob::STATUS_NEW]; |
|
39
|
3 |
|
} |
|
40
|
3 |
|
} |
|
41
|
3 |
|
} |
|
42
|
|
|
|
|
43
|
2 |
|
protected function extractStatusHashResults(array $hResults, array &$results) |
|
44
|
|
|
{ |
|
45
|
2 |
|
foreach ($hResults as $key => $value) { |
|
46
|
2 |
|
list($workerName, $method, $status) = explode(',', $key); |
|
47
|
2 |
|
$resultHashKey = $workerName.'->'.$method.'()'; |
|
48
|
2 |
|
if (!isset($results[$resultHashKey])) { |
|
49
|
|
|
$results[$resultHashKey] = static::getAllStatuses(); |
|
50
|
|
|
} |
|
51
|
2 |
|
if (!isset($results[$resultHashKey][$status])) { |
|
52
|
|
|
$results[$resultHashKey][$status] = 0; |
|
53
|
|
|
} |
|
54
|
2 |
|
$results[$resultHashKey][$status] += $value; |
|
55
|
2 |
|
} |
|
56
|
2 |
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: