|
1
|
|
|
<?php namespace Comodojo\Extender\Components; |
|
2
|
|
|
|
|
3
|
|
|
use \Psr\Log\LoggerInterface; |
|
4
|
|
|
use \Exception; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Lock file manager (static methods) |
|
8
|
|
|
* |
|
9
|
|
|
* @package Comodojo extender |
|
10
|
|
|
* @author Marco Giovinazzi <[email protected]> |
|
11
|
|
|
* @license GPL-3.0+ |
|
12
|
|
|
* |
|
13
|
|
|
* LICENSE: |
|
14
|
|
|
* |
|
15
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
16
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
17
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
18
|
|
|
* License, or (at your option) any later version. |
|
19
|
|
|
* |
|
20
|
|
|
* This program is distributed in the hope that it will be useful, |
|
21
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
22
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
23
|
|
|
* GNU Affero General Public License for more details. |
|
24
|
|
|
* |
|
25
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
26
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
27
|
|
|
*/ |
|
28
|
|
|
|
|
29
|
|
|
class Niceness { |
|
30
|
|
|
|
|
31
|
|
|
private $logger; |
|
32
|
|
|
|
|
33
|
|
|
private $niceness = 0; |
|
34
|
|
|
|
|
35
|
|
|
public function __contruct(LoggerInterface $logger) { |
|
36
|
|
|
|
|
37
|
|
|
$this->logger = $logger; |
|
38
|
|
|
|
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function get($pid = null) { |
|
42
|
|
|
|
|
43
|
|
|
return is_null($pid) ? $this->niceness : pcntl_getpriority($pid); |
|
44
|
|
|
|
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function set($niceness = null, $pid = null) { |
|
48
|
|
|
|
|
49
|
|
|
$niceness = self::getNiceness($niceness); |
|
50
|
|
|
|
|
51
|
|
|
if ( is_null($pid) ) { |
|
52
|
|
|
|
|
53
|
|
|
$nice = proc_nice($niceness); |
|
54
|
|
|
|
|
55
|
|
|
if ( $nice === false ) { |
|
56
|
|
|
|
|
57
|
|
|
$this->logger->warning("Unable to set parent process niceness to $niceness"); |
|
58
|
|
|
|
|
59
|
|
|
} else { |
|
60
|
|
|
|
|
61
|
|
|
$this->niceness = $niceness; |
|
62
|
|
|
|
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
} else { |
|
66
|
|
|
|
|
67
|
|
|
$nice = pcntl_setpriority($pid, $$niceness); |
|
68
|
|
|
|
|
69
|
|
|
if ( $nice == false ) $logger->warning("Unable to set child process $pid niceness to $niceness"); |
|
|
|
|
|
|
70
|
|
|
|
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
return $nice; |
|
74
|
|
|
|
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
View Code Duplication |
private static function getNiceness($niceness=null) { |
|
|
|
|
|
|
78
|
|
|
|
|
79
|
|
|
return filter_var($niceness, FILTER_VALIDATE_INT, array( |
|
80
|
|
|
'options' => array( |
|
81
|
|
|
'default' => 0, |
|
82
|
|
|
'min_range' => -10, |
|
83
|
|
|
'max_range' => 10 |
|
84
|
|
|
) |
|
85
|
|
|
)); |
|
86
|
|
|
|
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
} |
|
90
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.