1 | <?php |
||||||
2 | /** |
||||||
3 | * Service registry |
||||||
4 | * User: moyo |
||||||
5 | * Date: 27/12/2017 |
||||||
6 | * Time: 6:02 PM |
||||||
7 | */ |
||||||
8 | |||||||
9 | namespace Carno\HRPC\Plugins; |
||||||
10 | |||||||
11 | use Carno\Console\Boot\Waited; |
||||||
12 | use Carno\Container\DI; |
||||||
13 | use Carno\HRPC\Serviced\Registry as ServiceRG; |
||||||
14 | use Carno\Net\Address; |
||||||
15 | use Carno\Net\Contracts\Conn; |
||||||
16 | use Carno\Net\Events; |
||||||
17 | use Carno\Process\Piping; |
||||||
18 | use Carno\Process\Program; |
||||||
19 | use Carno\Promise\Promised; |
||||||
20 | use Carno\Serving\Contracts\Plugins; |
||||||
21 | |||||||
22 | class Registry extends Program implements Plugins |
||||||
23 | { |
||||||
24 | /** |
||||||
25 | * @var string |
||||||
26 | */ |
||||||
27 | protected $name = 'service.registry'; |
||||||
28 | |||||||
29 | /** |
||||||
30 | * @var ServiceRG |
||||||
31 | */ |
||||||
32 | private $registry = null; |
||||||
33 | |||||||
34 | /** |
||||||
35 | * @var Address |
||||||
36 | */ |
||||||
37 | private $advertise = null; |
||||||
38 | |||||||
39 | /** |
||||||
40 | * @var int |
||||||
41 | */ |
||||||
42 | private $waiting = 1; |
||||||
43 | |||||||
44 | /** |
||||||
45 | * @var int |
||||||
46 | */ |
||||||
47 | private $ready = 0; |
||||||
48 | |||||||
49 | /** |
||||||
50 | * @var int |
||||||
51 | */ |
||||||
52 | private $waits = null; |
||||||
53 | |||||||
54 | /** |
||||||
55 | * ServiceRegistry constructor. |
||||||
56 | * @param Address $advertise |
||||||
57 | * @param int $waits |
||||||
58 | */ |
||||||
59 | public function __construct(Address $advertise, int $waits = 1) |
||||||
60 | { |
||||||
61 | $this->advertise = $advertise; |
||||||
62 | $this->waits = $waits; |
||||||
63 | } |
||||||
64 | |||||||
65 | /** |
||||||
66 | * @return bool |
||||||
67 | */ |
||||||
68 | public function enabled() : bool |
||||||
69 | { |
||||||
70 | return DI::has(ServiceRG::class) ? !! $this->registry = DI::get(ServiceRG::class) : false; |
||||||
71 | } |
||||||
72 | |||||||
73 | /** |
||||||
74 | * @param Events $events |
||||||
75 | */ |
||||||
76 | public function hooking(Events $events) : void |
||||||
77 | { |
||||||
78 | $forked = $this->fork(); |
||||||
79 | |||||||
80 | $forked->waiting($this->waits); |
||||||
81 | |||||||
82 | $events |
||||||
83 | ->attach(Events\Server::CREATED, static function (Conn $serv) use ($forked) { |
||||||
84 | $forked->startup($serv->server()->raw()); |
||||||
85 | }) |
||||||
86 | ->attach(Events\Server::STARTUP, static function (Conn $serv) use ($forked) { |
||||||
87 | $forked->porting($serv->local()->port()); |
||||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||||
88 | }) |
||||||
89 | ->attach(Events\Server::SHUTDOWN, static function () use ($forked) { |
||||||
90 | $forked->shutdown(); |
||||||
91 | }) |
||||||
92 | ->attach(Events\Worker::STARTED, static function (Conn $serv) use ($forked) { |
||||||
93 | $startup = $serv->ctx()->get('WG:STARTING'); |
||||||
94 | if ($startup instanceof Waited) { |
||||||
95 | $startup->done()->then(function () use ($serv, $forked) { |
||||||
96 | $forked->ready($serv->worker()); |
||||||
0 ignored issues
–
show
The method
ready() does not exist on Carno\Process\Program . It seems like you code against a sub-type of Carno\Process\Program such as Carno\HRPC\Plugins\Registry .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
97 | }); |
||||||
98 | } else { |
||||||
99 | trigger_error('No WG:STARTING in serv context', E_USER_WARNING); |
||||||
100 | } |
||||||
101 | }); |
||||||
102 | } |
||||||
103 | |||||||
104 | /** |
||||||
105 | * @param int $listen |
||||||
106 | */ |
||||||
107 | public function porting(int $listen) : void |
||||||
108 | { |
||||||
109 | if ($this->advertise->port() !== $listen) { |
||||||
110 | $this->advertise = new Address($this->advertise->host(), $listen); |
||||||
111 | } |
||||||
112 | } |
||||||
113 | |||||||
114 | /** |
||||||
115 | * @param int $all |
||||||
116 | */ |
||||||
117 | public function waiting(int $all) : void |
||||||
118 | { |
||||||
119 | $this->waiting = $all; |
||||||
120 | } |
||||||
121 | |||||||
122 | /** |
||||||
123 | * @param int $wid |
||||||
124 | */ |
||||||
125 | public function ready(int $wid) : void |
||||||
0 ignored issues
–
show
The parameter
$wid is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. ![]() |
|||||||
126 | { |
||||||
127 | $this->ready ++; |
||||||
128 | $this->starting(); |
||||||
129 | } |
||||||
130 | |||||||
131 | /** |
||||||
132 | * @param Piping $piping |
||||||
133 | */ |
||||||
134 | protected function forking(Piping $piping) : void |
||||||
135 | { |
||||||
136 | // do nothing |
||||||
137 | } |
||||||
138 | |||||||
139 | /** |
||||||
140 | * server register |
||||||
141 | */ |
||||||
142 | protected function starting() : void |
||||||
143 | { |
||||||
144 | if ($this->ready === $this->waiting) { |
||||||
145 | $this->registry->register($this->advertise); |
||||||
146 | } |
||||||
147 | } |
||||||
148 | |||||||
149 | /** |
||||||
150 | * service deregister |
||||||
151 | * @param Promised $wait |
||||||
152 | */ |
||||||
153 | protected function stopping(Promised $wait) : void |
||||||
154 | { |
||||||
155 | $this->registry->deregister()->sync($wait); |
||||||
156 | } |
||||||
157 | } |
||||||
158 |