|
1
|
|
|
<?php namespace Comodojo\Extender\Components; |
|
2
|
|
|
|
|
3
|
|
|
use \Exception; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Basic signaling for extender components |
|
7
|
|
|
* |
|
8
|
|
|
* @package Comodojo Framework |
|
9
|
|
|
* @author Marco Giovinazzi <[email protected]> |
|
10
|
|
|
* @license GPL-3.0+ |
|
11
|
|
|
* |
|
12
|
|
|
* LICENSE: |
|
13
|
|
|
* |
|
14
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
15
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
16
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
17
|
|
|
* License, or (at your option) any later version. |
|
18
|
|
|
* |
|
19
|
|
|
* This program is distributed in the hope that it will be useful, |
|
20
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
21
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
22
|
|
|
* GNU Affero General Public License for more details. |
|
23
|
|
|
* |
|
24
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
25
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
26
|
|
|
*/ |
|
27
|
|
|
|
|
28
|
|
|
class ProcessUtils { |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Kill a child process |
|
32
|
|
|
* |
|
33
|
|
|
* @return bool |
|
34
|
|
|
*/ |
|
35
|
|
View Code Duplication |
public static function kill($pid, $lagger_timeout = 0) { |
|
|
|
|
|
|
36
|
|
|
|
|
37
|
|
|
$kill_time = time() + self::$lagger_timeout; |
|
38
|
|
|
|
|
39
|
|
|
$term = posix_kill($pid, SIGTERM); |
|
40
|
|
|
|
|
41
|
|
|
while ( time() < $kill_time ) { |
|
42
|
|
|
|
|
43
|
|
|
if ( !self::isRunning($pid) ) return $term; |
|
44
|
|
|
|
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
return posix_kill($pid, SIGKILL); |
|
48
|
|
|
|
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Return true if process is still running, false otherwise |
|
53
|
|
|
* |
|
54
|
|
|
* @return bool |
|
55
|
|
|
*/ |
|
56
|
|
|
public static function isRunning($pid) { |
|
57
|
|
|
|
|
58
|
|
|
return (pcntl_waitpid($pid, $status, WNOHANG) === 0); |
|
59
|
|
|
|
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public static function getNiceness($pid = null) { |
|
63
|
|
|
|
|
64
|
|
|
return pcntl_getpriority($pid); |
|
65
|
|
|
|
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function setNiceness($niceness = null, $pid = null) { |
|
69
|
|
|
|
|
70
|
|
|
$niceness = self::filterNiceness($niceness); |
|
71
|
|
|
|
|
72
|
|
|
if ( is_null($pid) ) { |
|
73
|
|
|
|
|
74
|
|
|
return proc_nice($niceness); |
|
75
|
|
|
|
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
return pcntl_setpriority($pid, $$niceness); |
|
79
|
|
|
|
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
View Code Duplication |
public static function filterNiceness($niceness=null) { |
|
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
return filter_var($niceness, FILTER_VALIDATE_INT, array( |
|
85
|
|
|
'options' => array( |
|
86
|
|
|
'default' => 0, |
|
87
|
|
|
'min_range' => -10, |
|
88
|
|
|
'max_range' => 10 |
|
89
|
|
|
) |
|
90
|
|
|
)); |
|
91
|
|
|
|
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
} |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.