1
|
|
|
<?php namespace Comodojo\Extender\Task; |
2
|
|
|
|
3
|
|
|
use \Comodojo\Daemon\Locker\AbstractLocker; |
4
|
|
|
use \Comodojo\Daemon\Utils\ProcessTools; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* @package Comodojo Extender |
8
|
|
|
* @author Marco Giovinazzi <[email protected]> |
9
|
|
|
* @license MIT |
10
|
|
|
* |
11
|
|
|
* LICENSE: |
12
|
|
|
* |
13
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
14
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
15
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
16
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
17
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
18
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
19
|
|
|
* THE SOFTWARE. |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
class Locker extends AbstractLocker { |
23
|
|
|
|
24
|
|
|
protected $usage = [ |
25
|
|
|
'STARTTIMESTAMP' => null, |
26
|
|
|
'PID' => null, |
27
|
|
|
'MEMORYUSAGE' => null, |
28
|
|
|
'MEMORYPEAKUSAGE' => null |
29
|
|
|
]; |
30
|
|
|
|
31
|
|
|
protected $counters = [ |
32
|
|
|
'QUEUED' => 0, |
33
|
|
|
'RUNNING' => 0, |
34
|
|
|
'COMPLETED' => 0, |
35
|
|
|
'SUCCEEDED' => 0, |
36
|
|
|
'FAILED' => 0, |
37
|
|
|
'ABORTED' => 0 |
38
|
|
|
]; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Lock file name |
42
|
|
|
* |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
|
|
private $lockfile; |
46
|
|
|
|
47
|
|
|
public function __construct($lockfile) { |
48
|
|
|
|
49
|
|
|
$this->lockfile = $lockfile; |
50
|
|
|
$this->usage_data['STARTTIMESTAMP'] = time(); |
|
|
|
|
51
|
|
|
$this->usage_data['PID'] = ProcessTools::getPid(); |
52
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function getQueued() { |
56
|
|
|
|
57
|
|
|
return $this->counters['QUEUED']; |
58
|
|
|
|
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function getRunning() { |
62
|
|
|
|
63
|
|
|
return $this->counters['RUNNING']; |
64
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function getCompleted() { |
68
|
|
|
|
69
|
|
|
return $this->counters['COMPLETED']; |
70
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function getSucceeded() { |
74
|
|
|
|
75
|
|
|
return $this->counters['SUCCEEDED']; |
76
|
|
|
|
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function getFailed() { |
80
|
|
|
|
81
|
|
|
return $this->counters['FAILED']; |
82
|
|
|
|
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function getAborted() { |
86
|
|
|
|
87
|
|
|
return $this->counters['ABORTED']; |
88
|
|
|
|
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function lock($what) { |
92
|
|
|
|
93
|
|
|
$usage = $this->updateUsage(); |
94
|
|
|
$counters = $this->updateCounters($what); |
95
|
|
|
|
96
|
|
|
$data = serialize([ |
97
|
|
|
'USAGE' => $usage, |
98
|
|
|
'COUNTERS' => $counters |
99
|
|
|
]); |
100
|
|
|
|
101
|
|
|
return self::writeLock($this->lockfile, $data); |
102
|
|
|
|
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function release() { |
106
|
|
|
|
107
|
|
|
return self::releaseLock($this->lockfile); |
108
|
|
|
|
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
protected function updateUsage() { |
112
|
|
|
|
113
|
|
|
$this->usage_data['MEMORYUSAGE'] = memory_get_usage(); |
|
|
|
|
114
|
|
|
$this->usage_data['MEMORYPEAKUSAGE'] = memory_get_peak_usage(); |
115
|
|
|
|
116
|
|
|
return $this->usage_data; |
117
|
|
|
|
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
protected function updateCounters($counters) { |
121
|
|
|
|
122
|
|
|
if ( isset($counters['QUEUED']) && is_int($counters['QUEUED']) ) $this->counters['QUEUED'] = $counters['QUEUED']; |
123
|
|
|
if ( isset($counters['RUNNING']) && is_int($counters['RUNNING']) ) $this->counters['RUNNING'] = $counters['RUNNING']; |
124
|
|
|
if ( !empty($counters['COMPLETED']) ) $this->counters['COMPLETED'] = $this->counters['COMPLETED'] + (int) $counters['COMPLETED']; |
125
|
|
|
if ( !empty($counters['SUCCEEDED']) ) $this->counters['SUCCEEDED'] = $this->counters['SUCCEEDED'] + (int) $counters['SUCCEEDED']; |
126
|
|
|
if ( !empty($counters['FAILED']) ) $this->counters['FAILED'] = $this->counters['FAILED'] + (int) $counters['FAILED']; |
127
|
|
|
if ( !empty($counters['ABORTED']) ) $this->counters['ABORTED'] = $this->counters['ABORTED'] + (int) $counters['ABORTED']; |
128
|
|
|
|
129
|
|
|
// array_walk($this->counters, function(&$counter, $name) use ($counters) { |
|
|
|
|
130
|
|
|
// |
131
|
|
|
// if ( !empty($counters[$name]) ) $counter = $counter + (int) $counters[$name]; |
|
|
|
|
132
|
|
|
// |
133
|
|
|
// }); |
|
|
|
|
134
|
|
|
|
135
|
|
|
return $this->counters; |
136
|
|
|
|
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
} |
140
|
|
|
|