summaryrefslogtreecommitdiffstats
path: root/library/Director/Daemon/ProcessList.php
blob: 85b9aac917e03bbbb6cfb7d2a2244acc7fdac77d (plain)
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php

namespace Icinga\Module\Director\Daemon;

use gipfl\LinuxHealth\Memory;
use Icinga\Application\Logger;
use ipl\Stdlib\EventEmitter;
use React\ChildProcess\Process;
use React\EventLoop\LoopInterface;
use React\Promise\Deferred;
use function React\Promise\resolve;

class ProcessList
{
    use EventEmitter;

    /** @var LoopInterface */
    protected $loop;

    /** @var \SplObjectStorage */
    protected $processes;

    /**
     * ProcessList constructor.
     * @param LoopInterface $loop
     * @param Process[] $processes
     */
    public function __construct(LoopInterface $loop, array $processes = [])
    {
        $this->loop = $loop;
        $this->processes = new \SplObjectStorage();
        foreach ($processes as $process) {
            $this->attach($process);
        }
    }

    public function attach(Process $process)
    {
        $this->processes->attach($process);
        $this->emit('start', [$process]);
        $process->on('exit', function () use ($process) {
            $this->detach($process);
            $this->emit('exit', [$process]);
        });

        return $this;
    }

    public function detach(Process $process)
    {
        $this->processes->detach($process);

        return $this;
    }

    /**
     * @param int $timeout
     * @return \React\Promise\ExtendedPromiseInterface
     */
    public function killOrTerminate($timeout = 5)
    {
        if ($this->processes->count() === 0) {
            return resolve();
        }
        $deferred = new Deferred();
        $killTimer = $this->loop->addTimer($timeout, function () use ($deferred) {
            /** @var Process $process */
            foreach ($this->processes as $process) {
                $pid = $process->getPid();
                Logger::error("Process $pid is still running, sending SIGKILL");
                $process->terminate(SIGKILL);
            }

            // Let's a little bit of delay after KILLing
            $this->loop->addTimer(0.1, function () use ($deferred) {
                $deferred->resolve();
            });
        });

        $timer = $this->loop->addPeriodicTimer($timeout / 20, function () use (
            $deferred,
            &$timer,
            $killTimer
        ) {
            $stopped = [];
            /** @var Process $process */
            foreach ($this->processes as $process) {
                if (! $process->isRunning()) {
                    $stopped[] = $process;
                }
            }
            foreach ($stopped as $process) {
                $this->processes->detach($process);
            }
            if ($this->processes->count() === 0) {
                $this->loop->cancelTimer($timer);
                $this->loop->cancelTimer($killTimer);
                $deferred->resolve();
            }
        });
        /** @var Process $process */
        foreach ($this->processes as $process) {
            $process->terminate(SIGTERM);
        }

        return $deferred->promise();
    }

    public function getOverview()
    {
        $info = [];

        /** @var Process $process */
        foreach ($this->processes as $process) {
            $pid = $process->getPid();
            $info[$pid] = (object) [
                'command' => preg_replace('/^exec /', '', $process->getCommand()),
                'running' => $process->isRunning(),
                'memory'  => Memory::getUsageForPid($pid)
            ];
        }

        return  $info;
    }
}