Daemon::stopping()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Daemon program
4
 * User: moyo
5
 * Date: 28/12/2017
6
 * Time: 3:47 PM
7
 */
8
9
namespace Carno\Monitor;
10
11
use Carno\Container\DI;
12
use Carno\Monitor\Chips\Trunking;
13
use Carno\Monitor\Collector\Aggregator;
14
use Carno\Monitor\Collector\Cleaner;
15
use Carno\Monitor\Output\Prometheus;
16
use Carno\Net\Address;
17
use Carno\Process\Piping;
18
use Carno\Process\Program;
19
use Carno\Promise\Promised;
20
21
class Daemon extends Program
22
{
23
    use Trunking;
24
25
    /**
26
     * @var string
27
     */
28
    protected $name = 'metrics.exporter';
29
30
    /**
31
     * @var string
32
     */
33
    private $host = null;
34
35
    /**
36
     * @var string
37
     */
38
    private $app = null;
39
40
    /**
41
     * @var Cleaner
42
     */
43
    private $cls = null;
44
45
    /**
46
     * @var Aggregator
47
     */
48
    private $agg = null;
49
50
    /**
51
     * @var Prometheus
52
     */
53
    private $out = null;
54
55
    /**
56
     * @var Address
57
     */
58
    private $listener = null;
59
60
    /**
61
     * @var Address
62
     */
63
    private $gateway = null;
64
65
    /**
66
     * Daemon constructor.
67
     * @param string $app
68
     * @param Address $listener
69
     * @param Address $gateway
70
     */
71
    public function __construct(string $app, Address $listener = null, Address $gateway = null)
72
    {
73
        $this->host = gethostname();
74
        $this->app = $app;
75
        $this->listener = $listener;
76
        $this->gateway = $gateway;
77
    }
78
79
    /**
80
     * @param Piping $piping
81
     */
82
    protected function forking(Piping $piping) : void
83
    {
84
        DI::set(Daemon::class, $piping);
85
    }
86
87
    /**
88
     * triggered when process started
89
     */
90
    protected function starting() : void
91
    {
92
        $this->agg = (new Aggregator($this))->start();
93
        $this->out = (new Prometheus($this->host, $this->app, $this->agg))->start($this->listener, $this->gateway);
94
        $this->cls = (new Cleaner($this))->start();
95
    }
96
97
    /**
98
     * triggered when process exiting
99
     * @param Promised $wait
100
     */
101
    protected function stopping(Promised $wait) : void
102
    {
103
        $this->cls->stop();
104
        $this->out->stop();
105
        $this->agg->stop()->sync($wait);
106
    }
107
}
108