1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Gearman Bundle for Symfony2 / Symfony3 |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
* |
9
|
|
|
* Feel free to edit as you please, and have fun. |
10
|
|
|
* |
11
|
|
|
* @author Marc Morera <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Mkk\GearmanBundle\Module; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Job status class |
18
|
|
|
*/ |
19
|
|
|
class JobStatus |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var boolean |
23
|
|
|
* |
24
|
|
|
* Job is known |
25
|
|
|
*/ |
26
|
|
|
private $known; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var boolean |
30
|
|
|
* |
31
|
|
|
* Job is running |
32
|
|
|
*/ |
33
|
|
|
private $running; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var Integer |
37
|
|
|
* |
38
|
|
|
* Job completition |
39
|
|
|
*/ |
40
|
|
|
private $completed; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var integer |
44
|
|
|
* |
45
|
|
|
* Job completition total |
46
|
|
|
*/ |
47
|
|
|
private $completionTotal; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Construct method |
51
|
|
|
* |
52
|
|
|
* @param array $response Response to parse |
53
|
|
|
*/ |
54
|
4 |
|
public function __construct(array $response) |
55
|
|
|
{ |
56
|
4 |
|
$this->known = (isset($response[0]) && $response[0]); |
57
|
4 |
|
$this->running = (isset($response[1]) && $response[1]); |
58
|
|
|
|
59
|
4 |
|
$this->completed = (isset($response[2]) && !$response[2]) |
60
|
1 |
|
? 0 |
61
|
3 |
|
: $response[2]; |
62
|
|
|
|
63
|
4 |
|
$this->completionTotal = (isset($response[3]) && !$response[3]) |
64
|
|
|
? 0 |
65
|
4 |
|
: $response[3]; |
66
|
4 |
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Return if job is known |
70
|
|
|
* |
71
|
|
|
* @return boolean Job is still known |
72
|
|
|
*/ |
73
|
4 |
|
public function isKnown() |
74
|
|
|
{ |
75
|
4 |
|
return $this->known; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Return if job is still running |
80
|
|
|
* |
81
|
|
|
* @return boolean Jon is still running |
82
|
|
|
*/ |
83
|
4 |
|
public function isRunning() |
84
|
|
|
{ |
85
|
4 |
|
return $this->running; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Return completed value |
90
|
|
|
* |
91
|
|
|
* @return integer Completed |
92
|
|
|
*/ |
93
|
4 |
|
public function getCompleted() |
94
|
|
|
{ |
95
|
4 |
|
return $this->completed; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Return completition total |
100
|
|
|
* |
101
|
|
|
* @return integer Completition total |
102
|
|
|
*/ |
103
|
4 |
|
public function getCompletionTotal() |
104
|
|
|
{ |
105
|
4 |
|
return $this->completionTotal; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Return percent completed. |
110
|
|
|
* |
111
|
|
|
* 0 is not started or not known |
112
|
|
|
* 1 is finished |
113
|
|
|
* Between 0 and 1 is in process. Value is a float |
114
|
|
|
* |
115
|
|
|
* @return float Percent completed |
|
|
|
|
116
|
|
|
*/ |
117
|
4 |
|
public function getCompletionPercent() |
118
|
|
|
{ |
119
|
4 |
|
$percent = 0; |
120
|
|
|
|
121
|
4 |
|
if (($this->completed > 0) && ($this->completionTotal > 0)) { |
122
|
|
|
|
123
|
2 |
|
$percent = $this->completed / $this->completionTotal; |
124
|
|
|
} |
125
|
|
|
|
126
|
4 |
|
return $percent; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Return if job is still running |
131
|
|
|
* |
132
|
|
|
* @return boolean Jon is still running |
133
|
|
|
*/ |
134
|
4 |
|
public function isFinished() |
135
|
|
|
{ |
136
|
4 |
|
return $this->isKnown() && !$this->isRunning() && $this->getCompletionPercent() == 1; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.