|
1
|
|
|
<?php |
|
2
|
|
|
namespace PHPDaemon\IPCManager; |
|
3
|
|
|
|
|
4
|
|
|
use PHPDaemon\Config; |
|
5
|
|
|
use PHPDaemon\Core\AppInstance; |
|
6
|
|
|
use PHPDaemon\Core\Daemon; |
|
7
|
|
|
use PHPDaemon\Thread; |
|
8
|
|
|
|
|
9
|
|
|
class IPCManager extends AppInstance |
|
10
|
|
|
{ |
|
11
|
|
|
/** @var */ |
|
12
|
|
|
public $pool; |
|
13
|
|
|
/** @var */ |
|
14
|
|
|
public $conn; |
|
15
|
|
|
/** @var */ |
|
16
|
|
|
public $socketurl; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Setting default config options |
|
20
|
|
|
* Overriden from AppInstance::getConfigDefaults |
|
21
|
|
|
* @return array|bool |
|
22
|
|
|
*/ |
|
23
|
|
|
protected function getConfigDefaults() |
|
24
|
|
|
{ |
|
25
|
|
|
return [ |
|
26
|
|
|
// listen to |
|
27
|
|
|
'mastersocket' => 'unix:///tmp/phpDaemon-ipc-%x.sock', |
|
28
|
|
|
]; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Constructor. |
|
33
|
|
|
* @return void |
|
34
|
|
|
*/ |
|
35
|
|
|
public function init() |
|
36
|
|
|
{ |
|
37
|
|
|
$this->socketurl = sprintf($this->config->mastersocket->value, |
|
38
|
|
|
crc32(Daemon::$config->pidfile->value . "\x00" . Daemon::$config->user->value . "\x00" . Daemon::$config->group->value)); |
|
39
|
|
|
if (Daemon::$process instanceof Thread\IPC) { |
|
40
|
|
|
$this->pool = MasterPool::getInstance(['listen' => $this->socketurl]); |
|
|
|
|
|
|
41
|
|
|
$this->pool->appInstance = $this; |
|
42
|
|
|
$this->pool->onReady(); |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function getSocketUrl() |
|
47
|
|
|
{ |
|
48
|
|
|
return $this->socketurl; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @TODO DESCR |
|
53
|
|
|
*/ |
|
54
|
|
|
public function updatedWorkers() |
|
55
|
|
|
{ |
|
56
|
|
|
$perWorker = 1; |
|
57
|
|
|
$instancesCount = []; |
|
58
|
|
|
foreach (Daemon::$config as $name => $section) { |
|
|
|
|
|
|
59
|
|
|
if ((!$section instanceof Config\Section) || !isset($section->limitinstances)) { |
|
60
|
|
|
continue; |
|
61
|
|
|
} |
|
62
|
|
|
$instancesCount[$name] = 0; |
|
63
|
|
|
} |
|
64
|
|
|
foreach ($this->pool->workers as $worker) { |
|
65
|
|
|
foreach ($worker->instancesCount as $k => $v) { |
|
66
|
|
|
if (!isset($instancesCount[$k])) { |
|
67
|
|
|
unset($worker->instancesCount[$k]); |
|
68
|
|
|
continue; |
|
69
|
|
|
} |
|
70
|
|
|
$instancesCount[$k] += $v; |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
foreach ($instancesCount as $name => $num) { |
|
74
|
|
|
$v = Daemon::$config->{$name}->limitinstances->value - $num; |
|
75
|
|
|
foreach ($this->pool->workers as $worker) { |
|
76
|
|
|
if ($v <= 0) { |
|
77
|
|
|
break; |
|
78
|
|
|
} |
|
79
|
|
|
if ((isset($worker->instancesCount[$name])) && ($worker->instancesCount[$name] < $perWorker)) { |
|
80
|
|
|
continue; |
|
81
|
|
|
} |
|
82
|
|
|
if (!isset($worker->instancesCount[$name])) { |
|
83
|
|
|
$worker->instancesCount[$name] = 1; |
|
84
|
|
|
} else { |
|
85
|
|
|
++$worker->instancesCount[$name]; |
|
86
|
|
|
} |
|
87
|
|
|
$worker->sendPacket(['op' => 'spawnInstance', 'appfullname' => $name]); |
|
88
|
|
|
--$v; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Called when application instance is going to shutdown. |
|
95
|
|
|
* @param bool $graceful |
|
96
|
|
|
* @return boolean Ready to shutdown? |
|
97
|
|
|
*/ |
|
98
|
|
|
public function onShutdown($graceful = false) |
|
99
|
|
|
{ |
|
100
|
|
|
if ($this->pool) { |
|
101
|
|
|
return $this->pool->onShutdown(); |
|
102
|
|
|
} |
|
103
|
|
|
return true; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @TODO DESCR |
|
108
|
|
|
* @param $workerId |
|
109
|
|
|
* @param $path |
|
110
|
|
|
* @return bool |
|
111
|
|
|
*/ |
|
112
|
|
|
public function importFile($workerId, $path) |
|
113
|
|
|
{ |
|
114
|
|
|
if (!isset($this->pool->workers[$workerId])) { |
|
115
|
|
|
return false; |
|
116
|
|
|
} |
|
117
|
|
|
$worker = $this->pool->workers[$workerId]; |
|
118
|
|
|
$worker->sendPacket(['op' => 'importFile', 'path' => $path]); |
|
119
|
|
|
return true; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @TODO DESCR |
|
124
|
|
|
*/ |
|
125
|
|
|
public function ensureConnection() |
|
126
|
|
|
{ |
|
127
|
|
|
$this->sendPacket(''); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @TODO DESCR |
|
132
|
|
|
* @param $packet |
|
133
|
|
|
*/ |
|
134
|
|
|
public function sendPacket($packet = null) |
|
135
|
|
|
{ |
|
136
|
|
|
if ($this->conn && $this->conn->isConnected()) { |
|
137
|
|
|
$this->conn->sendPacket($packet); |
|
138
|
|
|
return; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
$cb = function ($conn) use ($packet) { |
|
142
|
|
|
$conn->sendPacket($packet); |
|
143
|
|
|
}; |
|
144
|
|
|
if (!$this->conn) { |
|
145
|
|
|
$this->conn = new WorkerConnection(null, null, null); |
|
|
|
|
|
|
146
|
|
|
$this->conn->connect($this->socketurl); |
|
147
|
|
|
} |
|
148
|
|
|
$this->conn->onConnected($cb); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* @TODO DESCR |
|
153
|
|
|
* @param $appInstance |
|
154
|
|
|
* @param $method |
|
155
|
|
|
* @param array $args |
|
156
|
|
|
* @param callable $cb |
|
|
|
|
|
|
157
|
|
|
*/ |
|
158
|
|
View Code Duplication |
public function sendBroadcastCall($appInstance, $method, $args = [], $cb = null) |
|
|
|
|
|
|
159
|
|
|
{ |
|
160
|
|
|
$this->sendPacket([ |
|
161
|
|
|
'op' => 'broadcastCall', |
|
162
|
|
|
'appfullname' => $appInstance, |
|
163
|
|
|
'method' => $method, |
|
164
|
|
|
'args' => $args, |
|
165
|
|
|
]); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* @TODO DESCR |
|
170
|
|
|
* @param $appInstance |
|
171
|
|
|
* @param $method |
|
172
|
|
|
* @param array $args |
|
173
|
|
|
* @param callable $cb |
|
|
|
|
|
|
174
|
|
|
*/ |
|
175
|
|
View Code Duplication |
public function sendSingleCall($appInstance, $method, $args = [], $cb = null) |
|
|
|
|
|
|
176
|
|
|
{ |
|
177
|
|
|
$this->sendPacket([ |
|
178
|
|
|
'op' => 'singleCall', |
|
179
|
|
|
'appfullname' => $appInstance, |
|
180
|
|
|
'method' => $method, |
|
181
|
|
|
'args' => $args, |
|
182
|
|
|
]); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* @TODO DESCR |
|
187
|
|
|
* @param $workerId |
|
188
|
|
|
* @param $appInstance |
|
189
|
|
|
* @param $method |
|
190
|
|
|
* @param array $args |
|
191
|
|
|
* @param callable $cb |
|
|
|
|
|
|
192
|
|
|
*/ |
|
193
|
|
|
public function sendDirectCall($workerId, $appInstance, $method, $args = [], $cb = null) |
|
|
|
|
|
|
194
|
|
|
{ |
|
195
|
|
|
$this->sendPacket([ |
|
196
|
|
|
'op' => 'directCall', |
|
197
|
|
|
'appfullname' => $appInstance, |
|
198
|
|
|
'method' => $method, |
|
199
|
|
|
'args' => $args, |
|
200
|
|
|
'workerId' => $workerId, |
|
201
|
|
|
]); |
|
202
|
|
|
} |
|
203
|
|
|
} |
|
204
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: