Completed
Push — 2.0 ( 14c778...b5ef61 )
by Marco
11:18
created

ProcessUtils::kill()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 6

Duplication

Lines 15
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 15
loc 15
rs 9.4285
cc 3
eloc 6
nc 3
nop 2
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) {
0 ignored issues
show
Unused Code introduced by
The parameter $lagger_timeout is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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
}