1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Laravoole\Commands; |
4
|
|
|
|
5
|
|
|
use ReflectionClass; |
6
|
|
|
|
7
|
|
|
use Illuminate\Console\Command; |
8
|
|
|
use Symfony\Component\Console\Input\InputOption; |
9
|
|
|
|
10
|
|
|
class LaravooleCommand extends Command |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* The name and signature of the console command. |
14
|
|
|
* |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
protected $signature = 'laravoole {action : start | stop | reload | reload_task | restart | quit}'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The console command description. |
21
|
|
|
* |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $description = 'Start laravoole'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Create a new command instance. |
28
|
|
|
* |
29
|
|
|
* @return void |
|
|
|
|
30
|
|
|
*/ |
31
|
|
|
public function __construct() |
32
|
|
|
{ |
33
|
|
|
parent::__construct(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Execute the console command. |
38
|
|
|
* |
39
|
|
|
* @return mixed |
40
|
|
|
*/ |
41
|
|
|
public function handle() |
42
|
|
|
{ |
43
|
|
|
switch ($action = $this->argument('action')) { |
44
|
|
|
|
45
|
|
|
case 'start': |
46
|
|
|
$this->start(); |
47
|
|
|
break; |
48
|
|
|
case 'restart': |
49
|
|
|
$pid = $this->sendSignal(SIGTERM); |
|
|
|
|
50
|
|
|
$time = 0; |
51
|
|
|
while (posix_getpgid($pid) && $time <= 10) { |
52
|
|
|
usleep(100000); |
53
|
|
|
$time++; |
54
|
|
|
} |
55
|
|
|
if ($time > 100) { |
56
|
|
|
echo 'timeout' . PHP_EOL; |
57
|
|
|
exit(1); |
|
|
|
|
58
|
|
|
} |
59
|
|
|
$this->start(); |
60
|
|
|
break; |
61
|
|
|
case 'stop': |
62
|
|
|
case 'quit': |
63
|
|
|
case 'reload': |
64
|
|
|
case 'reload_task': |
|
|
|
|
65
|
|
|
|
66
|
|
|
$map = [ |
67
|
|
|
'stop' => SIGTERM, |
68
|
|
|
'quit' => SIGQUIT, |
69
|
|
|
'reload' => SIGUSR1, |
70
|
|
|
'reload_task' => SIGUSR2, |
71
|
|
|
]; |
72
|
|
|
$this->sendSignal($map[$action]); |
73
|
|
|
break; |
74
|
|
|
|
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
protected function sendSignal($sig) |
79
|
|
|
{ |
80
|
|
|
if ($pid = $this->getPid()) { |
81
|
|
|
|
82
|
|
|
posix_kill($pid, $sig); |
83
|
|
|
} else { |
84
|
|
|
|
85
|
|
|
echo "not running!" . PHP_EOL; |
86
|
|
|
exit(1); |
|
|
|
|
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
protected function start() |
91
|
|
|
{ |
92
|
|
|
if ($this->getPid()) { |
|
|
|
|
93
|
|
|
echo 'already running' . PHP_EOL; |
94
|
|
|
exit(1); |
|
|
|
|
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$mode = config('laravoole.base_config.mode'); |
98
|
|
|
if (!$mode) { |
99
|
|
|
echo "Laravoole needs Swoole or Workerman." . PHP_EOL . |
100
|
|
|
"You can install Swoole by command:" . PHP_EOL . |
101
|
|
|
" pecl install swoole" . PHP_EOL . |
102
|
|
|
"Or you can install Workerman by command:" . PHP_EOL . |
103
|
|
|
" composer require workerman/workerman" . PHP_EOL; |
104
|
|
|
exit; |
|
|
|
|
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
if(!class_exists($wrapper = "Laravoole\\Wrapper\\{$mode}Wrapper")) { |
108
|
|
|
$wrapper = $mode; |
109
|
|
|
} |
110
|
|
|
$ref = new ReflectionClass($wrapper); |
111
|
|
|
$wrapper_file = $ref->getFileName(); |
112
|
|
|
|
113
|
|
|
$handler_config = []; |
114
|
|
|
$params = $wrapper::getParams(); |
115
|
|
|
foreach ($params as $paramName => $default) { |
116
|
|
|
if (is_int($paramName)) { |
117
|
|
|
$paramName = $default; |
118
|
|
|
$default = null; |
119
|
|
|
} |
120
|
|
|
$key = $paramName; |
121
|
|
|
$value = config("laravoole.handler_config.{$key}", function () use ($key, $default) { |
122
|
|
|
return env("LARAVOOLE_" . strtoupper($key), $default); |
123
|
|
|
}); |
124
|
|
|
if ($value !== null) { |
125
|
|
|
if ((is_array($value) || is_object($value)) && is_callable($value)) { |
126
|
|
|
$value = $value(); |
127
|
|
|
} |
128
|
|
|
$handler_config[$paramName] = $value; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
$host = config('laravoole.base_config.host'); |
134
|
|
|
$port = config('laravoole.base_config.port'); |
135
|
|
|
$socket = @stream_socket_server("tcp://{$host}:{$port}"); |
136
|
|
|
if(!$socket) { |
137
|
|
|
throw new \Exception("Address {$host}:{$port} already in use", 1); |
138
|
|
|
} else { |
139
|
|
|
fclose($socket); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
$configs = [ |
143
|
|
|
'host' => $host, |
144
|
|
|
'port' => $port, |
145
|
|
|
'wrapper_file' => $wrapper_file, |
146
|
|
|
'wrapper' => $wrapper, |
147
|
|
|
'pid_file' => config('laravoole.base_config.pid_file'), |
148
|
|
|
'root_dir' => base_path(), |
149
|
|
|
'callbacks' => config('laravoole.base_config.callbacks'), |
150
|
|
|
// for swoole / workerman |
151
|
|
|
'handler_config' => $handler_config, |
152
|
|
|
// for wrapper, like http / fastcgi / websocket |
153
|
|
|
'wrapper_config' => config('laravoole.wrapper_config'), |
154
|
|
|
'base_config' => config('laravoole.base_config'), |
155
|
|
|
]; |
156
|
|
|
|
157
|
|
|
$handle = popen(PHP_BINARY . ' ' . __DIR__ . '/../../src/Entry.php', 'w'); |
158
|
|
|
fwrite($handle, serialize($configs)); |
159
|
|
|
fclose($handle); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
protected function getPid() |
163
|
|
|
{ |
164
|
|
|
|
165
|
|
|
$pid_file = config('laravoole.base_config.pid_file'); |
166
|
|
|
if (file_exists($pid_file)) { |
167
|
|
|
$pid = file_get_contents($pid_file); |
168
|
|
|
if (posix_getpgid($pid)) { |
169
|
|
|
return $pid; |
170
|
|
|
} else { |
171
|
|
|
unlink($pid_file); |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
return false; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
} |
178
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.