forked from laravel/horizon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProcessInspector.php
72 lines (64 loc) · 1.67 KB
/
ProcessInspector.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
namespace Laravel\Horizon;
use Illuminate\Support\Arr;
use Laravel\Horizon\Contracts\MasterSupervisorRepository;
use Laravel\Horizon\Contracts\SupervisorRepository;
class ProcessInspector
{
/**
* The command executor.
*
* @var \Laravel\Horizon\Exec
*/
public $exec;
/**
* Create a new process inspector instance.
*
* @param \Laravel\Horizon\Exec $exec
* @return void
*/
public function __construct(Exec $exec)
{
$this->exec = $exec;
}
/**
* Get the IDs of all Horizon processes running on the system.
*
* @return array
*/
public function current()
{
return array_diff(
$this->exec->run('pgrep -f [h]orizon'),
$this->exec->run('pgrep -f horizon:purge')
);
}
/**
* Get an array of running Horizon processes that can't be accounted for.
*
* @return array
*/
public function orphaned()
{
return array_diff($this->current(), $this->monitoring());
}
/**
* Get all of the process IDs Horizon is actively monitoring.
*
* @return array
*/
public function monitoring()
{
return collect(app(SupervisorRepository::class)->all())
->pluck('pid')
->pipe(function ($processes) {
$processes->each(function ($process) use (&$processes) {
$processes = $processes->merge($this->exec->run("pgrep -P {$process}"));
});
return $processes;
})
->merge(
Arr::pluck(app(MasterSupervisorRepository::class)->all(), 'pid')
)->all();
}
}