1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* RPC server -> start |
4
|
|
|
* User: moyo |
5
|
|
|
* Date: 12/12/2017 |
6
|
|
|
* Time: 12:45 PM |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Carno\HRPC\Commands; |
10
|
|
|
|
11
|
|
|
use Carno\Console\Based; |
12
|
|
|
use Carno\Console\Configure; |
13
|
|
|
use Carno\Console\Contracts\Application; |
14
|
|
|
use Carno\HRPC\Components\Dispatcher; |
15
|
|
|
use Carno\HRPC\Components\Register; |
16
|
|
|
use Carno\HRPC\Components\Serving; |
17
|
|
|
use Carno\HRPC\Components\Tracing; |
18
|
|
|
use Carno\HRPC\Plugins\Registry; |
19
|
|
|
use Carno\HRPC\Server; |
20
|
|
|
use Carno\Net\Address; |
21
|
|
|
use Carno\Serving\Chips\HWIGet; |
22
|
|
|
use Carno\Serving\Contracts\Options; |
23
|
|
|
use Carno\Serving\Options as Opt; |
24
|
|
|
use Carno\Serving\Plugins\LiveReloading; |
25
|
|
|
use Carno\Serving\Plugins\MetricsExporter; |
26
|
|
|
use Carno\Serving\Plugins\ServerMonitor; |
27
|
|
|
use Symfony\Component\Console\Input\InputOption; |
28
|
|
|
|
29
|
|
|
class ServerStart extends Based |
30
|
|
|
{ |
31
|
|
|
use HWIGet; |
32
|
|
|
use Opt\Common; |
33
|
|
|
use Opt\Metrics; |
34
|
|
|
use Opt\Discovery; |
35
|
|
|
use Opt\Listener; |
36
|
|
|
|
37
|
|
|
// service broadcast ip |
38
|
|
|
private const OPT_ADVERTISE = 'broadcast-ip'; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
protected $name = 'server:start'; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var string |
47
|
|
|
*/ |
48
|
|
|
protected $description = 'Start the RPC server'; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var array |
52
|
|
|
*/ |
53
|
|
|
protected $components = [ |
54
|
|
|
Dispatcher::class, |
55
|
|
|
Serving::class, |
56
|
|
|
Register::class, |
57
|
|
|
Tracing::class, |
58
|
|
|
]; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @var bool |
62
|
|
|
*/ |
63
|
|
|
protected $ready = false; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param Configure $conf |
67
|
|
|
*/ |
68
|
|
|
protected function options(Configure $conf) : void |
69
|
|
|
{ |
70
|
|
|
$conf->addOption(self::OPT_ADVERTISE, null, InputOption::VALUE_OPTIONAL, 'Advertised IP', '127.0.0.1'); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param Application $app |
75
|
|
|
*/ |
76
|
|
|
protected function firing(Application $app) : void |
77
|
|
|
{ |
78
|
|
|
$workers = $app->input()->getOption(Options::WORKERS) ?: $this->numCPUs(); |
79
|
|
|
|
80
|
|
|
(new Server( |
81
|
|
|
$app->name(), |
82
|
|
|
new Address($app->input()->getOption(Options::LISTEN)) |
83
|
|
|
)) |
84
|
|
|
->bootstrap($this->bootstrap()) |
85
|
|
|
->plugins( |
86
|
|
|
new Registry(new Address($app->input()->getOption(self::OPT_ADVERTISE)), $workers), |
87
|
|
|
new LiveReloading(), |
88
|
|
|
new ServerMonitor(), |
89
|
|
|
new MetricsExporter() |
90
|
|
|
) |
91
|
|
|
->wants($app->starting(), $app->stopping()) |
92
|
|
|
->run($workers) |
93
|
|
|
; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|