|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Innmind\Server\Status\Server\Processes; |
|
5
|
|
|
|
|
6
|
|
|
use Innmind\Server\Status\{ |
|
7
|
|
|
Server\Processes, |
|
8
|
|
|
Server\Process, |
|
9
|
|
|
Server\Process\Pid, |
|
10
|
|
|
Server\Process\User, |
|
11
|
|
|
Server\Process\Command, |
|
12
|
|
|
Server\Process\Memory, |
|
13
|
|
|
Server\Cpu\Percentage, |
|
14
|
|
|
Exception\InformationNotAccessible |
|
15
|
|
|
}; |
|
16
|
|
|
use Innmind\TimeContinuum\TimeContinuumInterface; |
|
17
|
|
|
use Innmind\Immutable\{ |
|
18
|
|
|
MapInterface, |
|
19
|
|
|
Str, |
|
20
|
|
|
StreamInterface, |
|
21
|
|
|
Sequence, |
|
22
|
|
|
Map |
|
23
|
|
|
}; |
|
24
|
|
|
use Symfony\Component\Process\Process as SfProcess; |
|
25
|
|
|
|
|
26
|
|
|
final class UnixProcesses implements Processes |
|
27
|
|
|
{ |
|
28
|
|
|
private $clock; |
|
29
|
|
|
|
|
30
|
9 |
|
public function __construct(TimeContinuumInterface $clock) |
|
31
|
|
|
{ |
|
32
|
9 |
|
$this->clock = $clock; |
|
33
|
9 |
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* {@inheritdoc} |
|
37
|
|
|
*/ |
|
38
|
|
|
public function all(): MapInterface |
|
39
|
|
|
{ |
|
40
|
|
|
return $this->parse( |
|
41
|
|
|
$this->run('ps aux') |
|
42
|
|
|
); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
1 |
|
public function get(Pid $pid): Process |
|
46
|
|
|
{ |
|
47
|
1 |
|
$flag = PHP_OS === 'Linux' ? 'q' : 'p'; |
|
48
|
|
|
|
|
49
|
|
|
return $this |
|
50
|
1 |
|
->parse($this->run(sprintf('ps ux -%s %s', $flag, $pid))) |
|
51
|
|
|
->get($pid->toInt()); |
|
|
|
|
|
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
1 |
View Code Duplication |
private function run(string $command): Str |
|
|
|
|
|
|
55
|
|
|
{ |
|
56
|
1 |
|
$process = new SfProcess($command); |
|
57
|
1 |
|
$process->run(); |
|
58
|
|
|
|
|
59
|
1 |
|
if (!$process->isSuccessful()) { |
|
60
|
1 |
|
throw new InformationNotAccessible; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
return new Str($process->getOutput()); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @return MapInterface<int, Process> |
|
|
|
|
|
|
68
|
|
|
*/ |
|
69
|
|
|
private function parse(Str $output): MapInterface |
|
70
|
|
|
{ |
|
71
|
|
|
$lines = $output |
|
72
|
|
|
->trim() |
|
73
|
|
|
->split("\n"); |
|
74
|
|
|
$columns = $lines |
|
75
|
|
|
->first() |
|
76
|
|
|
->pregSplit('~ +~') |
|
77
|
|
|
->reduce( |
|
78
|
|
|
new Sequence, |
|
79
|
|
|
static function(Sequence $columns, Str $column): Sequence { |
|
80
|
|
|
return $columns->add((string) $column); |
|
81
|
|
|
} |
|
82
|
|
|
); |
|
83
|
|
|
|
|
84
|
|
|
return $lines |
|
85
|
|
|
->drop(1) |
|
86
|
|
|
->reduce( |
|
87
|
|
|
new Sequence, |
|
88
|
|
|
static function(Sequence $lines, Str $line) use ($columns): Sequence { |
|
89
|
|
|
return $lines->add( |
|
90
|
|
|
$line->pregSplit('~ +~', $columns->size()) |
|
91
|
|
|
); |
|
92
|
|
|
} |
|
93
|
|
|
) |
|
94
|
|
|
->map(function(StreamInterface $parts) use ($columns): Process { |
|
95
|
|
|
return new Process( |
|
96
|
|
|
new Pid((int) (string) $parts->get($columns->indexOf('PID'))), |
|
97
|
|
|
new User((string) $parts->get($columns->indexOf('USER'))), |
|
98
|
|
|
new Percentage((float) (string) $parts->get($columns->indexOf('%CPU'))), |
|
99
|
|
|
new Memory((float) (string) $parts->get($columns->indexOf('%MEM'))), |
|
100
|
|
|
$this->clock->at((string) $parts->get($columns->indexOf('STARTED'))), |
|
101
|
|
|
new Command((string) $parts->get($columns->indexOf('COMMAND'))) |
|
102
|
|
|
); |
|
103
|
|
|
}) |
|
104
|
|
|
->reduce( |
|
105
|
|
|
new Map('int', Process::class), |
|
106
|
|
|
static function(Map $processes, Process $process): Map { |
|
107
|
|
|
return $processes->put( |
|
108
|
|
|
$process->pid()->toInt(), |
|
|
|
|
|
|
109
|
|
|
$process |
|
|
|
|
|
|
110
|
|
|
); |
|
111
|
|
|
} |
|
112
|
|
|
); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: