|
1
|
|
|
<?php |
|
2
|
|
|
namespace PHPDaemon\Core; |
|
3
|
|
|
|
|
4
|
|
|
use PHPDaemon\Config\Entry\Generic; |
|
5
|
|
|
use PHPDaemon\FS\FileSystem; |
|
6
|
|
|
use PHPDaemon\Thread; |
|
7
|
|
|
use PHPDaemon\Utils\DateTime; |
|
8
|
|
|
use PHPDaemon\Utils\ShmEntity; |
|
9
|
|
|
use PHPDaemon\Utils\Terminal; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Bootstrap for PHPDaemon |
|
13
|
|
|
* |
|
14
|
|
|
* @package Core |
|
15
|
|
|
* |
|
16
|
|
|
* @author Vasily Zorin <[email protected]> |
|
17
|
|
|
*/ |
|
18
|
|
|
class Bootstrap |
|
19
|
|
|
{ |
|
20
|
|
|
use \PHPDaemon\Traits\ClassWatchdog; |
|
21
|
|
|
use \PHPDaemon\Traits\StaticObjectWatchdog; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Master process ID |
|
25
|
|
|
* @var integer |
|
26
|
|
|
*/ |
|
27
|
|
|
protected static $pid; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* List of commands |
|
31
|
|
|
* @var array |
|
32
|
|
|
*/ |
|
33
|
|
|
protected static $commands = [ |
|
34
|
|
|
'start', |
|
35
|
|
|
'stop', |
|
36
|
|
|
'hardstop', |
|
37
|
|
|
'gracefulstop', |
|
38
|
|
|
'update', |
|
39
|
|
|
'reload', |
|
40
|
|
|
'restart', |
|
41
|
|
|
'hardrestart', |
|
42
|
|
|
'fullstatus', |
|
43
|
|
|
'status', |
|
44
|
|
|
'configtest', |
|
45
|
|
|
'log', |
|
46
|
|
|
'runworker', |
|
47
|
|
|
'ipcpath', |
|
48
|
|
|
'reopenlog' |
|
49
|
|
|
]; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Command-line params |
|
53
|
|
|
* @var array |
|
54
|
|
|
*/ |
|
55
|
|
|
protected static $params = [ |
|
56
|
|
|
'pid-file' => [ |
|
57
|
|
|
'val' => '/path/to/pid-file', |
|
58
|
|
|
'desc' => 'Pid file' |
|
59
|
|
|
], |
|
60
|
|
|
'max-requests' => [ |
|
61
|
|
|
'desc' => 'Maximum requests to worker before respawn', |
|
62
|
|
|
'val' => [ |
|
63
|
|
|
'n' => 'Count' |
|
64
|
|
|
] |
|
65
|
|
|
], |
|
66
|
|
|
'path' => [ |
|
67
|
|
|
'desc' => 'Path to your application resolver', |
|
68
|
|
|
'val' => '/path/to/resolver.php' |
|
69
|
|
|
], |
|
70
|
|
|
'config-file' => [ |
|
71
|
|
|
'desc' => 'Paths to configuration file separated by semicolon. First found will be used.', |
|
72
|
|
|
'val' => '/path/to/file' |
|
73
|
|
|
], |
|
74
|
|
|
'logging' => [ |
|
75
|
|
|
'desc' => 'Logging status', |
|
76
|
|
|
'val' => [ |
|
77
|
|
|
'0' => 'Disabled', |
|
78
|
|
|
'1' => 'Enabled' |
|
79
|
|
|
] |
|
80
|
|
|
], |
|
81
|
|
|
'log-storage' => [ |
|
82
|
|
|
'desc' => 'Log storage', |
|
83
|
|
|
'val' => '/path/to/file' |
|
84
|
|
|
], |
|
85
|
|
|
'user' => [ |
|
86
|
|
|
'desc' => 'User of master process', |
|
87
|
|
|
'val' => 'username' |
|
88
|
|
|
], |
|
89
|
|
|
'group' => [ |
|
90
|
|
|
'desc' => 'Group of master process', |
|
91
|
|
|
'val' => 'groupname' |
|
92
|
|
|
], |
|
93
|
|
|
'help' => 'This help information' |
|
94
|
|
|
]; |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Actions on early startup. |
|
98
|
|
|
* @param string Optional. Config file path. |
|
99
|
|
|
* @return void |
|
100
|
|
|
*/ |
|
101
|
|
|
public static function init($configFile = null) |
|
|
|
|
|
|
102
|
|
|
{ |
|
103
|
|
|
if (!version_compare(PHP_VERSION, '5.6.0', '>=')) { |
|
104
|
|
|
Daemon::log('PHP >= 5.6.0 required.'); |
|
105
|
|
|
return; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
//run without composer |
|
109
|
|
|
if (!function_exists('setTimeout')) { |
|
110
|
|
|
require 'PHPDaemon/Utils/func.php'; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
Daemon::initSettings(); |
|
114
|
|
|
FileSystem::init(); |
|
115
|
|
|
Daemon::$runName = basename($_SERVER['argv'][0]); |
|
116
|
|
|
|
|
117
|
|
|
$error = false; |
|
118
|
|
|
$argv = $_SERVER['argv']; |
|
119
|
|
|
$runmode = isset($argv[1]) ? str_replace('-', '', $argv[1]) : ''; |
|
120
|
|
|
$args = Bootstrap::getArgs($argv); |
|
|
|
|
|
|
121
|
|
|
|
|
122
|
|
|
if (!isset(self::$params[$runmode]) && !in_array($runmode, self::$commands)) { |
|
123
|
|
|
if ('' !== $runmode) { |
|
124
|
|
|
echo('Unrecognized command: ' . $runmode . "\n"); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
self::printUsage(); |
|
128
|
|
|
exit; |
|
|
|
|
|
|
129
|
|
|
} elseif ('help' === $runmode) { |
|
130
|
|
|
self::printHelp(); |
|
131
|
|
|
exit; |
|
|
|
|
|
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
$n = null; |
|
135
|
|
|
if ('log' === $runmode) { |
|
136
|
|
|
if (isset($args['n'])) { |
|
137
|
|
|
$n = $args['n']; |
|
138
|
|
|
unset($args['n']); |
|
139
|
|
|
} else { |
|
140
|
|
|
$n = 20; |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
if (isset($configFile)) { |
|
145
|
|
|
Daemon::$config->configfile->setHumanValue($configFile); |
|
146
|
|
|
} |
|
147
|
|
|
if (isset($args['configfile'])) { |
|
148
|
|
|
Daemon::$config->configfile->setHumanValue($args['configfile']); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
if (!Daemon::$config->loadCmdLineArgs($args)) { |
|
152
|
|
|
$error = true; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
if (!Daemon::loadConfig(Daemon::$config->configfile->value)) { |
|
156
|
|
|
$error = true; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
if ('log' === $runmode) { |
|
160
|
|
|
passthru('tail -n ' . $n . ' -f ' . escapeshellarg(Daemon::$config->logstorage->value)); |
|
161
|
|
|
exit; |
|
|
|
|
|
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
if (extension_loaded('apc') && ini_get('apc.enabled')) { |
|
165
|
|
|
Daemon::log('Detected pecl-apc extension enabled. Usage of bytecode caching (APC/eAccelerator/xcache/...) makes no sense at all in case of using phpDaemon \'cause phpDaemon includes files just in time itself.'); |
|
|
|
|
|
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
if (isset(Daemon::$config->locale->value) && Daemon::$config->locale->value !== '') { |
|
169
|
|
|
setlocale(LC_ALL, array_map('trim', explode(',', Daemon::$config->locale->value))); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
if (Daemon::$config->autoreimport->value && !is_callable('runkit_import')) { |
|
173
|
|
|
Daemon::log('[WARN] runkit extension not found. You should install it or disable --auto-reimport. Non-critical error.'); |
|
|
|
|
|
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
if (!is_callable('posix_kill')) { |
|
177
|
|
|
Daemon::log('[EMERG] Posix not found. You should compile PHP without \'--disable-posix\'.'); |
|
178
|
|
|
$error = true; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
if (!is_callable('pcntl_signal')) { |
|
182
|
|
|
Daemon::log('[EMERG] PCNTL not found. You should compile PHP with \'--enable-pcntl\'.'); |
|
183
|
|
|
$error = true; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
if (extension_loaded('libevent')) { |
|
187
|
|
|
Daemon::log('[EMERG] libevent extension found. You have to remove libevent.so extension.'); |
|
188
|
|
|
$error = true; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
$eventVer = '1.6.1'; |
|
192
|
|
|
$eventVerType = 'stable'; |
|
193
|
|
|
if (!Daemon::loadModuleIfAbsent('event', $eventVer . '-' . $eventVerType)) { |
|
194
|
|
|
Daemon::log('[EMERG] event extension >= ' . $eventVer . ' not found (or OUTDATED). You have to install it. `pecl install http://pecl.php.net/get/event`'); |
|
|
|
|
|
|
195
|
|
|
$error = true; |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
if (!is_callable('socket_create')) { |
|
199
|
|
|
Daemon::log('[EMERG] Sockets extension not found. You should compile PHP with \'--enable-sockets\'.'); |
|
200
|
|
|
$error = true; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
if (!is_callable('shmop_open')) { |
|
204
|
|
|
Daemon::log('[EMERG] Shmop extension not found. You should compile PHP with \'--enable-shmop\'.'); |
|
205
|
|
|
$error = true; |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
if (!isset(Daemon::$config->user)) { |
|
209
|
|
|
Daemon::log('[EMERG] You must set \'user\' parameter.'); |
|
210
|
|
|
$error = true; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
if (!isset(Daemon::$config->path)) { |
|
214
|
|
|
Daemon::log('[EMERG] You must set \'path\' parameter (path to your application resolver).'); |
|
215
|
|
|
$error = true; |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
if (!file_exists(Daemon::$config->pidfile->value)) { |
|
219
|
|
View Code Duplication |
if (!touch(Daemon::$config->pidfile->value)) { |
|
|
|
|
|
|
220
|
|
|
Daemon::log('[EMERG] Couldn\'t create pid-file \'' . Daemon::$config->pidfile->value . '\'.'); |
|
221
|
|
|
$error = true; |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
Bootstrap::$pid = 0; |
|
|
|
|
|
|
225
|
|
View Code Duplication |
} elseif (!is_file(Daemon::$config->pidfile->value)) { |
|
|
|
|
|
|
226
|
|
|
Daemon::log('Pid-file \'' . Daemon::$config->pidfile->value . '\' must be a regular file.'); |
|
227
|
|
|
Bootstrap::$pid = false; |
|
|
|
|
|
|
228
|
|
|
$error = true; |
|
229
|
|
|
} elseif (!is_writable(Daemon::$config->pidfile->value)) { |
|
230
|
|
|
Daemon::log('Pid-file \'' . Daemon::$config->pidfile->value . '\' must be writable.'); |
|
231
|
|
|
$error = true; |
|
232
|
|
View Code Duplication |
} elseif (!is_readable(Daemon::$config->pidfile->value)) { |
|
|
|
|
|
|
233
|
|
|
Daemon::log('Pid-file \'' . Daemon::$config->pidfile->value . '\' must be readable.'); |
|
234
|
|
|
Bootstrap::$pid = false; |
|
|
|
|
|
|
235
|
|
|
$error = true; |
|
236
|
|
|
} else { |
|
237
|
|
|
Bootstrap::$pid = (int)file_get_contents(Daemon::$config->pidfile->value); |
|
|
|
|
|
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
if (Daemon::$config->chroot->value !== '/') { |
|
241
|
|
|
if (posix_getuid() != 0) { |
|
242
|
|
|
Daemon::log('You must have the root privileges to change root.'); |
|
243
|
|
|
$error = true; |
|
244
|
|
|
} |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
$pathList = preg_split('~\s*;\s*~', Daemon::$config->path->value); |
|
248
|
|
|
$found = false; |
|
249
|
|
|
foreach ($pathList as $path) { |
|
250
|
|
|
if (@is_file($path)) { |
|
251
|
|
|
Daemon::$appResolverPath = $path; |
|
252
|
|
|
$found = true; |
|
253
|
|
|
break; |
|
254
|
|
|
} |
|
255
|
|
|
} |
|
256
|
|
|
if (!$found) { |
|
257
|
|
|
Daemon::log('Your application resolver \'' . Daemon::$config->path->value . '\' is not available (config directive \'path\').'); |
|
|
|
|
|
|
258
|
|
|
$error = true; |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
Daemon::$appResolver = require Daemon::$appResolverPath; |
|
262
|
|
|
|
|
263
|
|
View Code Duplication |
if (isset(Daemon::$config->group->value) && is_callable('posix_getgid')) { |
|
|
|
|
|
|
264
|
|
|
if (($sg = posix_getgrnam(Daemon::$config->group->value)) === false) { |
|
265
|
|
|
Daemon::log('Unexisting group \'' . Daemon::$config->group->value . '\'. You have to replace config-variable \'group\' with existing group-name.'); |
|
|
|
|
|
|
266
|
|
|
$error = true; |
|
267
|
|
|
} elseif (($sg['gid'] != posix_getgid()) && (posix_getuid() != 0)) { |
|
268
|
|
|
Daemon::log('You must have the root privileges to change group.'); |
|
269
|
|
|
$error = true; |
|
270
|
|
|
} |
|
271
|
|
|
} |
|
272
|
|
|
|
|
273
|
|
View Code Duplication |
if (isset(Daemon::$config->user->value) && is_callable('posix_getuid')) { |
|
|
|
|
|
|
274
|
|
|
if (($su = posix_getpwnam(Daemon::$config->user->value)) === false) { |
|
275
|
|
|
Daemon::log('Unexisting user \'' . Daemon::$config->user->value . '\', user not found. You have to replace config-variable \'user\' with existing username.'); |
|
|
|
|
|
|
276
|
|
|
$error = true; |
|
277
|
|
|
} elseif (($su['uid'] != posix_getuid()) && (posix_getuid() != 0)) { |
|
278
|
|
|
Daemon::log('You must have the root privileges to change user.'); |
|
279
|
|
|
$error = true; |
|
280
|
|
|
} |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
|
|
if (isset(Daemon::$config->minspareworkers->value) |
|
284
|
|
|
&& Daemon::$config->minspareworkers->value > 0 |
|
285
|
|
|
&& isset(Daemon::$config->maxspareworkers->value) |
|
286
|
|
|
&& Daemon::$config->maxspareworkers->value > 0 |
|
287
|
|
|
) { |
|
288
|
|
|
if (Daemon::$config->minspareworkers->value > Daemon::$config->maxspareworkers->value) { |
|
289
|
|
|
Daemon::log('\'minspareworkers\' cannot be greater than \'maxspareworkers\'.'); |
|
290
|
|
|
$error = true; |
|
291
|
|
|
} |
|
292
|
|
|
} |
|
293
|
|
|
|
|
294
|
|
|
if (isset(Daemon::$config->addincludepath->value)) { |
|
295
|
|
|
ini_set( |
|
296
|
|
|
'include_path', |
|
297
|
|
|
ini_get('include_path') . ':' . implode(':', Daemon::$config->addincludepath->value) |
|
298
|
|
|
); |
|
299
|
|
|
} |
|
300
|
|
|
|
|
301
|
|
|
if (isset(Daemon::$config->minworkers->value) && isset(Daemon::$config->maxworkers->value)) { |
|
302
|
|
|
if (Daemon::$config->minworkers->value > Daemon::$config->maxworkers->value) { |
|
303
|
|
|
Daemon::$config->minworkers->value = Daemon::$config->maxworkers->value; |
|
304
|
|
|
} |
|
305
|
|
|
} |
|
306
|
|
|
|
|
307
|
|
|
if ($runmode === 'start') { |
|
308
|
|
|
if ($error === false) { |
|
309
|
|
|
Bootstrap::start(); |
|
|
|
|
|
|
310
|
|
|
} else { |
|
311
|
|
|
exit(6); |
|
|
|
|
|
|
312
|
|
|
} |
|
313
|
|
|
} elseif ($runmode === 'runworker') { |
|
314
|
|
|
if ($error === false) { |
|
315
|
|
|
Bootstrap::runworker(); |
|
|
|
|
|
|
316
|
|
|
} else { |
|
317
|
|
|
exit(6); |
|
|
|
|
|
|
318
|
|
|
} |
|
319
|
|
|
} elseif ($runmode === 'status' || $runmode === 'fullstatus') { |
|
320
|
|
|
$status = Bootstrap::$pid && Thread\Generic::ifExistsByPid(Bootstrap::$pid); |
|
|
|
|
|
|
321
|
|
|
echo '[STATUS] phpDaemon ' . Daemon::$version . ' is ' . ($status ? 'running' : 'NOT running') . ' (' . Daemon::$config->pidfile->value . ").\n"; |
|
|
|
|
|
|
322
|
|
|
|
|
323
|
|
|
if ($status && ($runmode === 'fullstatus')) { |
|
324
|
|
|
echo 'Uptime: ' . DateTime::diffAsText(filemtime(Daemon::$config->pidfile->value), time()) . "\n"; |
|
325
|
|
|
|
|
326
|
|
|
Daemon::$shm_wstate = new ShmEntity(Daemon::$config->pidfile->value, Daemon::SHM_WSTATE_SIZE, 'wstate'); |
|
|
|
|
|
|
327
|
|
|
|
|
328
|
|
|
$stat = Daemon::getStateOfWorkers(); |
|
329
|
|
|
|
|
330
|
|
|
echo "State of workers:\n"; |
|
331
|
|
|
echo "\tTotal: " . $stat['alive'] . "\n"; |
|
332
|
|
|
echo "\tIdle: " . $stat['idle'] . "\n"; |
|
333
|
|
|
echo "\tBusy: " . $stat['busy'] . "\n"; |
|
334
|
|
|
echo "\tShutdown: " . $stat['shutdown'] . "\n"; |
|
335
|
|
|
echo "\tPre-init: " . $stat['preinit'] . "\n"; |
|
336
|
|
|
echo "\tInit: " . $stat['init'] . "\n"; |
|
337
|
|
|
} |
|
338
|
|
|
|
|
339
|
|
|
echo "\n"; |
|
340
|
|
View Code Duplication |
} elseif ($runmode === 'update') { |
|
|
|
|
|
|
341
|
|
|
if ((!Bootstrap::$pid) || (!posix_kill(Bootstrap::$pid, SIGHUP))) { |
|
|
|
|
|
|
342
|
|
|
echo '[UPDATE] ERROR. It seems that phpDaemon is not running' . (Bootstrap::$pid ? ' (PID ' . Bootstrap::$pid . ')' : '') . ".\n"; |
|
|
|
|
|
|
343
|
|
|
} |
|
344
|
|
|
} elseif ($runmode === 'reopenlog') { |
|
345
|
|
|
if ((!Bootstrap::$pid) || (!posix_kill(Bootstrap::$pid, SIGUSR1))) { |
|
|
|
|
|
|
346
|
|
|
echo '[REOPEN-LOG] ERROR. It seems that phpDaemon is not running' . (Bootstrap::$pid ? ' (PID ' . Bootstrap::$pid . ')' : '') . ".\n"; |
|
|
|
|
|
|
347
|
|
|
} |
|
348
|
|
View Code Duplication |
} elseif ($runmode === 'reload') { |
|
|
|
|
|
|
349
|
|
|
if ((!Bootstrap::$pid) || (!posix_kill(Bootstrap::$pid, SIGUSR2))) { |
|
|
|
|
|
|
350
|
|
|
echo '[RELOAD] ERROR. It seems that phpDaemon is not running' . (Bootstrap::$pid ? ' (PID ' . Bootstrap::$pid . ')' : '') . ".\n"; |
|
|
|
|
|
|
351
|
|
|
} |
|
352
|
|
|
} elseif ($runmode === 'restart') { |
|
353
|
|
|
if ($error === false) { |
|
354
|
|
|
Bootstrap::stop(2); |
|
|
|
|
|
|
355
|
|
|
Bootstrap::start(); |
|
|
|
|
|
|
356
|
|
|
} |
|
357
|
|
|
} elseif ($runmode === 'hardrestart') { |
|
358
|
|
|
Bootstrap::stop(3); |
|
|
|
|
|
|
359
|
|
|
Bootstrap::start(); |
|
|
|
|
|
|
360
|
|
|
} elseif ($runmode === 'ipcpath') { |
|
361
|
|
|
$i = Daemon::$appResolver->getInstanceByAppName('\PHPDaemon\IPCManager\IPCManager'); |
|
362
|
|
|
echo $i->getSocketUrl() . PHP_EOL; |
|
363
|
|
|
} elseif ($runmode === 'configtest') { |
|
364
|
|
|
$term = new Terminal; |
|
365
|
|
|
$term->enableColor(); |
|
366
|
|
|
|
|
367
|
|
|
echo "\n"; |
|
368
|
|
|
|
|
369
|
|
|
$rows = []; |
|
370
|
|
|
|
|
371
|
|
|
$rows[] = [ |
|
372
|
|
|
'parameter' => 'PARAMETER', |
|
373
|
|
|
'value' => 'VALUE', |
|
374
|
|
|
'_color' => '37', |
|
375
|
|
|
'_bold' => true, |
|
376
|
|
|
]; |
|
377
|
|
|
|
|
378
|
|
|
foreach (Daemon::$config as $name => $entry) { |
|
|
|
|
|
|
379
|
|
|
if (!$entry instanceof Generic) { |
|
380
|
|
|
continue; |
|
381
|
|
|
} |
|
382
|
|
|
|
|
383
|
|
|
$row = [ |
|
384
|
|
|
'parameter' => $name, |
|
385
|
|
|
'value' => var_export($entry->humanValue, true), |
|
386
|
|
|
]; |
|
387
|
|
|
|
|
388
|
|
|
if ($entry->defaultValue != $entry->humanValue) { |
|
389
|
|
|
$row['value'] .= ' (' . var_export($entry->defaultValue, true) . ')'; |
|
390
|
|
|
} |
|
391
|
|
|
|
|
392
|
|
|
$rows[] = $row; |
|
393
|
|
|
} |
|
394
|
|
|
|
|
395
|
|
|
$term->drawtable($rows); |
|
396
|
|
|
|
|
397
|
|
|
echo "\n"; |
|
398
|
|
|
} elseif ($runmode === 'stop') { |
|
399
|
|
|
Bootstrap::stop(); |
|
|
|
|
|
|
400
|
|
|
} elseif ($runmode === 'gracefulstop') { |
|
401
|
|
|
Bootstrap::stop(4); |
|
|
|
|
|
|
402
|
|
|
} elseif ($runmode === 'hardstop') { |
|
403
|
|
|
echo '[HARDSTOP] Sending SIGINT to ' . Bootstrap::$pid . '... '; |
|
|
|
|
|
|
404
|
|
|
|
|
405
|
|
|
$ok = Bootstrap::$pid && posix_kill(Bootstrap::$pid, SIGINT); |
|
|
|
|
|
|
406
|
|
|
|
|
407
|
|
|
echo $ok ? 'OK.' : 'ERROR. It seems that phpDaemon is not running.'; |
|
408
|
|
|
|
|
409
|
|
|
if ($ok) { |
|
410
|
|
|
$i = 0; |
|
411
|
|
|
|
|
412
|
|
|
while ($r = Thread\Generic::ifExistsByPid(Bootstrap::$pid)) { |
|
|
|
|
|
|
413
|
|
|
usleep(500000); |
|
414
|
|
|
|
|
415
|
|
|
if ($i === 9) { |
|
416
|
|
|
echo "\nphpDaemon master-process hasn't finished. Sending SIGKILL... "; |
|
417
|
|
|
posix_kill(Bootstrap::$pid, SIGKILL); |
|
|
|
|
|
|
418
|
|
|
sleep(0.2); |
|
419
|
|
|
if (!Thread\Generic::ifExistsByPid(Bootstrap::$pid)) { |
|
|
|
|
|
|
420
|
|
|
echo " Oh, his blood is on my hands :'("; |
|
421
|
|
|
} else { |
|
422
|
|
|
echo "ERROR: Process alive. Permissions?"; |
|
423
|
|
|
} |
|
424
|
|
|
|
|
425
|
|
|
break; |
|
426
|
|
|
} |
|
427
|
|
|
|
|
428
|
|
|
++$i; |
|
429
|
|
|
} |
|
430
|
|
|
} |
|
431
|
|
|
|
|
432
|
|
|
echo "\n"; |
|
433
|
|
|
} |
|
434
|
|
|
} |
|
435
|
|
|
|
|
436
|
|
|
/** |
|
437
|
|
|
* Print ussage |
|
438
|
|
|
* @return void |
|
439
|
|
|
*/ |
|
440
|
|
|
protected static function printUsage() |
|
441
|
|
|
{ |
|
442
|
|
|
echo 'usage: ' . Daemon::$runName . " (start|(hard|graceful)stop|update|reload|reopenlog|(hard)restart|fullstatus|status|configtest|log|runworker|help) ...\n"; |
|
|
|
|
|
|
443
|
|
|
} |
|
444
|
|
|
|
|
445
|
|
|
/** |
|
446
|
|
|
* Print help |
|
447
|
|
|
* @return void |
|
448
|
|
|
*/ |
|
449
|
|
|
protected static function printHelp() |
|
450
|
|
|
{ |
|
451
|
|
|
$term = new Terminal(); |
|
452
|
|
|
|
|
453
|
|
|
echo 'phpDaemon ' . Daemon::$version . ". http://phpdaemon.net\n"; |
|
454
|
|
|
|
|
455
|
|
|
self::printUsage(); |
|
456
|
|
|
|
|
457
|
|
|
echo "\nAlso you can use some optional parameters to override the same configuration variables:\n"; |
|
458
|
|
|
|
|
459
|
|
|
foreach (self::$params as $name => $desc) { |
|
460
|
|
|
if (empty($desc)) { |
|
461
|
|
|
continue; |
|
462
|
|
|
} elseif (!is_array($desc)) { |
|
463
|
|
|
$term->drawParam($name, $desc); |
|
464
|
|
|
} else { |
|
465
|
|
|
$term->drawParam( |
|
466
|
|
|
$name, |
|
467
|
|
|
isset($desc['desc']) ? $desc['desc'] : '', |
|
468
|
|
|
isset($desc['val']) ? $desc['val'] : '' |
|
469
|
|
|
); |
|
470
|
|
|
} |
|
471
|
|
|
} |
|
472
|
|
|
|
|
473
|
|
|
echo "\n"; |
|
474
|
|
|
} |
|
475
|
|
|
|
|
476
|
|
|
/** |
|
477
|
|
|
* Start master. |
|
478
|
|
|
* @return void |
|
479
|
|
|
*/ |
|
480
|
|
|
public static function start() |
|
481
|
|
|
{ |
|
482
|
|
|
if (Bootstrap::$pid && Thread\Generic::ifExistsByPid(Bootstrap::$pid)) { |
|
|
|
|
|
|
483
|
|
|
Daemon::log('[START] phpDaemon with pid-file \'' . Daemon::$config->pidfile->value . '\' is running already (PID ' . Bootstrap::$pid . ')'); |
|
|
|
|
|
|
484
|
|
|
exit(6); |
|
|
|
|
|
|
485
|
|
|
} |
|
486
|
|
|
|
|
487
|
|
|
Daemon::init(); |
|
488
|
|
|
$pid = Daemon::spawnMaster(); |
|
489
|
|
|
file_put_contents(Daemon::$config->pidfile->value, $pid); |
|
490
|
|
|
} |
|
491
|
|
|
|
|
492
|
|
|
/** |
|
493
|
|
|
* Runworker. |
|
494
|
|
|
* @return void |
|
495
|
|
|
*/ |
|
496
|
|
|
public static function runworker() |
|
497
|
|
|
{ |
|
498
|
|
|
Daemon::log('PLEASE USE runworker COMMAND ONLY FOR DEBUGGING PURPOSES.'); |
|
499
|
|
|
Daemon::init(); |
|
500
|
|
|
Daemon::runWorker(); |
|
501
|
|
|
} |
|
502
|
|
|
|
|
503
|
|
|
/** |
|
504
|
|
|
* Stop script. |
|
505
|
|
|
* @param int $mode |
|
506
|
|
|
* @return void |
|
507
|
|
|
*/ |
|
508
|
|
|
public static function stop($mode = 1) |
|
509
|
|
|
{ |
|
510
|
|
|
$ok = Bootstrap::$pid && posix_kill( |
|
|
|
|
|
|
511
|
|
|
Bootstrap::$pid, |
|
|
|
|
|
|
512
|
|
|
$mode === 3 ? SIGINT : (($mode === 4) ? SIGTSTP : SIGTERM) |
|
513
|
|
|
); |
|
514
|
|
|
|
|
515
|
|
|
if (!$ok) { |
|
516
|
|
|
echo '[WARN]. It seems that phpDaemon is not running' . (Bootstrap::$pid ? ' (PID ' . Bootstrap::$pid . ')' : '') . ".\n"; |
|
|
|
|
|
|
517
|
|
|
} |
|
518
|
|
|
|
|
519
|
|
|
if ($ok && ($mode > 1)) { |
|
520
|
|
|
$i = 0; |
|
521
|
|
|
|
|
522
|
|
|
while ($r = Thread\Generic::ifExistsByPid(Bootstrap::$pid)) { |
|
|
|
|
|
|
523
|
|
|
usleep(10000); |
|
524
|
|
|
++$i; |
|
525
|
|
|
} |
|
526
|
|
|
} |
|
527
|
|
|
} |
|
528
|
|
|
|
|
529
|
|
|
/** |
|
530
|
|
|
* Parses command-line arguments. |
|
531
|
|
|
* @param array $args $_SERVER ['argv'] |
|
532
|
|
|
* @return array Arguments |
|
533
|
|
|
*/ |
|
534
|
|
|
public static function getArgs($args) |
|
535
|
|
|
{ |
|
536
|
|
|
$out = []; |
|
537
|
|
|
$last_arg = null; |
|
538
|
|
|
|
|
539
|
|
|
for ($i = 1, $il = sizeof($args); $i < $il; ++$i) { |
|
540
|
|
|
if (preg_match('~^--(.+)~', $args[$i], $match)) { |
|
541
|
|
|
$parts = explode('=', $match[1]); |
|
542
|
|
|
$key = preg_replace('~[^a-z0-9]+~', '', $parts[0]); |
|
543
|
|
|
|
|
544
|
|
|
if (isset($parts[1])) { |
|
545
|
|
|
$out[$key] = $parts[1]; |
|
546
|
|
|
} else { |
|
547
|
|
|
$out[$key] = true; |
|
548
|
|
|
} |
|
549
|
|
|
|
|
550
|
|
|
$last_arg = $key; |
|
551
|
|
|
} elseif (preg_match('~^-([a-zA-Z0-9]+)~', $args[$i], $match)) { |
|
552
|
|
|
$key = null; |
|
553
|
|
|
for ($j = 0, $jl = mb_orig_strlen($match[1]); $j < $jl; ++$j) { |
|
554
|
|
|
$key = $match[1]{$j}; |
|
555
|
|
|
$out[$key] = true; |
|
556
|
|
|
} |
|
557
|
|
|
|
|
558
|
|
|
$last_arg = $key; |
|
559
|
|
|
} elseif ($last_arg !== null) { |
|
560
|
|
|
$out[$last_arg] = $args[$i]; |
|
561
|
|
|
} |
|
562
|
|
|
} |
|
563
|
|
|
|
|
564
|
|
|
return $out; |
|
565
|
|
|
} |
|
566
|
|
|
} |
|
567
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: