11
11
12
12
namespace think \swoole \command ;
13
13
14
- use Swoole \Http \Server as HttpServer ;
15
- use Swoole \WebSocket \Server as WebsocketServer ;
16
14
use think \console \Command ;
17
- use think \console \Input ;
18
- use think \console \input \Argument ;
19
- use think \console \Output ;
20
15
use think \swoole \Manager ;
21
- use think \swoole \PidManager ;
22
16
23
- /**
24
- * Swoole HTTP 命令行,支持操作:start|stop|restart|reload
25
- * 支持应用配置目录下的swoole.php文件进行参数配置
26
- */
27
17
class Server extends Command
28
18
{
29
19
public function configure ()
30
20
{
31
21
$ this ->setName ('swoole ' )
32
- ->addArgument ('action ' , Argument::OPTIONAL , 'start|stop|restart|reload ' , 'start ' )
33
- ->setDescription ('Swoole HTTP Server for ThinkPHP ' );
22
+ ->setDescription ('Swoole HTTP Server for ThinkPHP ' );
34
23
}
35
24
36
- protected function initialize ( Input $ input , Output $ output )
25
+ public function handle ( Manager $ manager )
37
26
{
38
- $ this ->app ->bind (\Swoole \Server::class, function () {
39
- return $ this ->createSwooleServer ();
40
- });
27
+ $ this ->checkEnvironment ();
41
28
42
- $ this ->app ->bind (PidManager::class, function () {
43
- return new PidManager ($ this ->app ->config ->get ('swoole.server.options.pid_file ' ));
44
- });
45
- }
29
+ $ this ->output ->writeln ('Starting swoole http server... ' );
46
30
47
- public function handle ()
48
- {
49
- $ this ->checkEnvironment ();
31
+ $ host = $ manager ->getConfig ('server.host ' );
32
+ $ port = $ manager ->getConfig ('server.port ' );
50
33
51
- $ action = $ this ->input ->getArgument ('action ' );
34
+ $ this ->output ->writeln ("Swoole http server started: <http:// {$ host }: {$ port }> " );
35
+ $ this ->output ->writeln ('You can exit with <info>`CTRL-C`</info> ' );
52
36
53
- if (in_array ($ action , ['start ' , 'stop ' , 'reload ' , 'restart ' ])) {
54
- $ this ->app ->invokeMethod ([$ this , $ action ], [], true );
55
- } else {
56
- $ this ->output ->writeln ("<error>Invalid argument action: {$ action }, Expected start|stop|restart|reload .</error> " );
57
- }
37
+ $ manager ->start ();
58
38
}
59
39
60
40
/**
@@ -75,117 +55,4 @@ protected function checkEnvironment()
75
55
}
76
56
}
77
57
78
- /**
79
- * 启动server
80
- * @access protected
81
- * @param Manager $manager
82
- * @param PidManager $pidManager
83
- * @return void
84
- */
85
- protected function start (Manager $ manager , PidManager $ pidManager )
86
- {
87
- if ($ pidManager ->isRunning ()) {
88
- $ this ->output ->writeln ('<error>swoole http server process is already running.</error> ' );
89
- return ;
90
- }
91
-
92
- $ this ->output ->writeln ('Starting swoole http server... ' );
93
-
94
- $ host = $ manager ->getConfig ('server.host ' );
95
- $ port = $ manager ->getConfig ('server.port ' );
96
-
97
- $ this ->output ->writeln ("Swoole http server started: <http:// {$ host }: {$ port }> " );
98
- $ this ->output ->writeln ('You can exit with <info>`CTRL-C`</info> ' );
99
-
100
- $ manager ->run ();
101
- }
102
-
103
- /**
104
- * 柔性重启server
105
- * @access protected
106
- * @param PidManager $manager
107
- * @return void
108
- */
109
- protected function reload (PidManager $ manager )
110
- {
111
- if (!$ manager ->isRunning ()) {
112
- $ this ->output ->writeln ('<error>no swoole http server process running.</error> ' );
113
- return ;
114
- }
115
-
116
- $ this ->output ->writeln ('Reloading swoole http server... ' );
117
-
118
- if (!$ manager ->killProcess (SIGUSR1 )) {
119
- $ this ->output ->error ('> failure ' );
120
-
121
- return ;
122
- }
123
-
124
- $ this ->output ->writeln ('> success ' );
125
- }
126
-
127
- /**
128
- * 停止server
129
- * @access protected
130
- * @param PidManager $manager
131
- * @return void
132
- */
133
- protected function stop (PidManager $ manager )
134
- {
135
- if (!$ manager ->isRunning ()) {
136
- $ this ->output ->writeln ('<error>no swoole http server process running.</error> ' );
137
- return ;
138
- }
139
-
140
- $ this ->output ->writeln ('Stopping swoole http server... ' );
141
-
142
- $ isRunning = $ manager ->killProcess (SIGTERM , 15 );
143
-
144
- if ($ isRunning ) {
145
- $ this ->output ->error ('Unable to stop the swoole_http_server process. ' );
146
- return ;
147
- }
148
-
149
- $ this ->output ->writeln ('> success ' );
150
- }
151
-
152
- /**
153
- * 重启server
154
- * @access protected
155
- * @param Manager $manager
156
- * @param PidManager $pidManager
157
- * @return void
158
- */
159
- protected function restart (Manager $ manager , PidManager $ pidManager )
160
- {
161
- if ($ pidManager ->isRunning ()) {
162
- $ this ->stop ($ pidManager );
163
- }
164
-
165
- $ this ->start ($ manager , $ pidManager );
166
- }
167
-
168
- /**
169
- * Create swoole server.
170
- */
171
- protected function createSwooleServer ()
172
- {
173
- $ isWebsocket = $ this ->app ->config ->get ('swoole.websocket.enable ' , false );
174
-
175
- $ serverClass = $ isWebsocket ? WebsocketServer::class : HttpServer::class;
176
- $ config = $ this ->app ->config ;
177
- $ host = $ config ->get ('swoole.server.host ' );
178
- $ port = $ config ->get ('swoole.server.port ' );
179
- $ socketType = $ config ->get ('swoole.server.socket_type ' , SWOOLE_SOCK_TCP );
180
- $ mode = $ config ->get ('swoole.server.mode ' , SWOOLE_PROCESS );
181
-
182
- /** @var \Swoole\Server $server */
183
- $ server = new $ serverClass ($ host , $ port , $ mode , $ socketType );
184
-
185
- $ options = $ config ->get ('swoole.server.options ' );
186
-
187
- $ server ->set ($ options );
188
- return $ server ;
189
- }
190
-
191
58
}
0 commit comments