9
9
namespace feehi \console ;
10
10
11
11
use yii ;
12
-
13
- /**
14
- * Class SwooleController
15
- *
16
- * @package feehi\console\controllers
17
- *
18
- * @description
19
- *
20
- * 支持的命令
21
- *
22
- * ./yii swoole/start 启动前台swoole
23
- * ./yii swoole/stop 关闭前台swoole
24
- * ./yii swoole/restart 重启前台swoole
25
- *
26
- * ./yii swoole-backend/start 启动后台swoole
27
- * ./yii swoole-backend/stop 关闭后台swoole
28
- * ./yii swoole-backend/restart 重启后台swoole
29
- *
30
- *
31
- * 配置示例
32
- 'controllerMap'=>[
33
- ...
34
- 'swoole' => [
35
- 'class' => feehi\console\SwooleController::className(),
36
- 'rootDir' => str_replace('console/config', '', __DIR__ ),//yii2项目根路径
37
- 'app' => 'frontend',//app目录地址
38
- 'host' => '127.0.0.1',//监听地址
39
- 'port' => 9999,//监听端口
40
- 'swooleConfig' => [//标准的swoole配置项都可以再此加入
41
- 'reactor_num' => 2,
42
- 'worker_num' => 4,
43
- 'daemonize' => false,
44
- 'log_file' => __DIR__ . '/../../frontend/runtime/logs/swoole.log',
45
- 'log_level' => 0,
46
- 'pid_file' => __DIR__ . '/../../frontend/runtime/server.pid',
47
- ],
48
- ],
49
- 'swoole-backend' => [
50
- 'class' => feehi\console\SwooleController::className(),
51
- 'rootDir' => str_replace('console/config', '', __DIR__ ),//yii2项目根路径
52
- 'app' => 'backend',
53
- 'host' => '127.0.0.1',
54
- 'port' => 9998,
55
- 'swooleConfig' => [
56
- 'reactor_num' => 2,
57
- 'worker_num' => 4,
58
- 'daemonize' => false,
59
- 'log_file' => __DIR__ . '/../../backend/runtime/logs/swoole.log',
60
- 'log_level' => 0,
61
- 'pid_file' => __DIR__ . '/../../backend/runtime/server.pid',
62
- ],
63
- ]
64
- ...
65
- ]
66
- *
67
- * nginx 配置示列 //虽然swoole从1.9.8开始支持静态资源,但是性能很差,线上环境务必搭配nginx使用
68
- *
69
- * 前台
70
- *
71
- server {
72
- set $web /www/cms-swoole/frontend/web;
73
- root $web;
74
- server_name swoole.cms.test.docker;
75
-
76
- location ~* .(ico|gif|bmp|jpg|jpeg|png|swf|js|css|mp3) {
77
- root $web;
78
- }
79
-
80
- location ~ timthumb\.php$ {//若部分功能仍需要使用php-fpm则做类似配置,否则删除此段
81
- fastcgi_pass 127.0.0.1:9000;
82
- fastcgi_index index.php;
83
- fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
84
- include fastcgi_params;
85
- }
86
-
87
- location / {
88
- proxy_http_version 1.1;
89
- proxy_set_header Connection "keep-alive";
90
- proxy_set_header X-Real-IP $remote_addr;
91
- proxy_set_header Host http://swoole.cms.test.docker;
92
- proxy_pass http://127.0.0.1:9999;
93
- }
94
- }
95
- *
96
- 后台
97
- server {
98
- set $web /www/cms-swoole/backend/web;
99
- root $web;
100
- server_name swoole-admin.cms.test.docker;
101
-
102
- location ~* .(ico|gif|bmp|jpg|jpeg|png|swf|js|css|mp3) {
103
- root $web;
104
- }
105
-
106
- location / {
107
- proxy_http_version 1.1;
108
- proxy_set_header Connection "keep-alive";
109
- proxy_set_header X-Real-IP $remote_addr;
110
- proxy_set_header Host http://swoole-admin.cms.test.docker;
111
- proxy_pass http://127.0.0.1:9998;
112
- }
113
- }
114
- */
12
+ use yii \helpers \ArrayHelper ;
13
+ use feehi \web \Request ;
14
+ use feehi \web \Response ;
15
+ use feehi \web \Session ;
16
+ use feehi \swoole \SwooleServer ;
17
+ use yii \web \AssetManager ;
18
+ use yii \web \Application ;
115
19
116
20
class SwooleController extends \yii \console \Controller
117
21
{
@@ -120,12 +24,18 @@ class SwooleController extends \yii\console\Controller
120
24
121
25
public $ port = 9999 ;
122
26
27
+ public $ mode = SWOOLE_PROCESS ;
28
+
29
+ public $ socketType = SWOOLE_TCP ;
30
+
123
31
public $ rootDir = "" ;
124
32
125
33
public $ app = "frontend " ;
126
34
127
35
public $ swooleConfig = [];
128
36
37
+ public $ gcSessionInterval = 60000 ;//启动session回收的间隔时间,单位为毫秒
38
+
129
39
130
40
131
41
public function actionStart ()
@@ -142,7 +52,7 @@ public function actionStart()
142
52
require ($ rootDir . '/common/config/bootstrap.php ' );
143
53
require ($ rootDir . $ this ->app . '/config/bootstrap.php ' );
144
54
145
- $ config = yii \ helpers \ ArrayHelper::merge (
55
+ $ config = ArrayHelper::merge (
146
56
require ($ rootDir . '/common/config/main.php ' ),
147
57
require ($ rootDir . '/common/config/main-local.php ' ),
148
58
require ($ rootDir . $ this ->app . '/config/main.php ' ),
@@ -154,54 +64,55 @@ public function actionStart()
154
64
'enable_static_handler ' => true ,
155
65
], $ this ->swooleConfig );
156
66
157
- $ server = new \feehi \swoole \SwooleServer ($ this ->host , $ this ->port , $ this ->swooleConfig );
67
+ $ server = new SwooleServer ($ this ->host , $ this ->port , $ this ->mode , $ this ->socketType , $ this ->swooleConfig , ['gcSessionInterval ' =>$ this ->gcSessionInterval ]);
68
+
69
+ function dump ($ var ){
70
+ if ( is_array ($ var ) || is_object ($ var ) ){
71
+ $ body = print_r ($ var , true );
72
+ }else {
73
+ $ body = $ var ;
74
+ }
75
+ if ( isset (yii::$ app ->getResponse ()->swooleResponse ) ){
76
+ echo "dump function must called in request period " . PHP_EOL ;
77
+ }
78
+ yii::$ app ->getResponse ()->swooleResponse ->end ($ body );
79
+ }
158
80
159
81
/**
160
82
* @param \swoole_http_request $request
161
83
* @param \swoole_http_response $response
162
84
*/
163
85
$ server ->runApp = function ($ request , $ response ) use ($ config , $ web ) {
164
- /*$uri = $request->server['request_uri'];
165
- if (strpos($uri, 'timthumb')) {
166
- $image = new \feehi\components\PicFilter();
167
- $image->initialize([
168
- 'source_image' => $web . "/uploads/article/thumb/5998ec3c119ea_a6.jpg",
169
- 'width' => 200,
170
- 'height' => 200,
171
- ]);
172
- $image->resize();
173
- exit;
174
- }*/
175
86
$ aliases = [
176
87
'@web ' => $ web ,
177
88
'@webroot ' => $ web ,
178
89
];
179
90
$ config ['aliases ' ] = isset ($ config ['aliases ' ]) ? array_merge ($ aliases , $ config ['aliases ' ]) : $ aliases ;
180
91
181
92
$ requestComponent = [
182
- 'class ' => \ feehi \ web \ Request::className (),
93
+ 'class ' => Request::className (),
183
94
'swooleRequest ' => $ request ,
184
95
];
185
96
$ config ['components ' ]['request ' ] = isset ($ config ['components ' ]['request ' ]) ? array_merge ($ config ['components ' ]['request ' ], $ requestComponent ) : $ requestComponent ;
186
97
187
98
$ responseComponent = [
188
- 'class ' => \ feehi \ web \ Response::className (),
99
+ 'class ' => Response::className (),
189
100
'swooleResponse ' => $ response ,
190
101
];
191
102
$ config ['components ' ]['response ' ] = isset ($ config ['components ' ]['response ' ]) ? array_merge ($ config ['components ' ]['response ' ], $ responseComponent ) : $ responseComponent ;
192
103
193
104
$ authManagerComponent = [
194
- 'class ' => yii \ web \ AssetManager::className (),
105
+ 'class ' => AssetManager::className (),
195
106
'baseUrl ' => '/assets '
196
107
];
197
108
$ config ['components ' ]['assetManager ' ] = isset ( $ config ['components ' ]['assetManager ' ] ) ? array_merge ($ authManagerComponent , $ config ['components ' ]['assetManager ' ]) : $ authManagerComponent ;
198
109
199
110
$ config ['components ' ]['session ' ] = [
200
- "class " => \ feehi \ web \ Session::className ()
111
+ "class " => Session::className ()
201
112
];
202
113
203
114
try {
204
- $ application = new \ yii \ web \ Application ($ config );
115
+ $ application = new Application ($ config );
205
116
yii::setAlias ('@web ' , $ web );
206
117
yii::$ app ->setAliases ($ aliases );
207
118
$ application ->run ();
@@ -251,7 +162,7 @@ private function sendSignal($sig)
251
162
if ($ pid = $ this ->getPid ()) {
252
163
posix_kill ($ pid , $ sig );
253
164
} else {
254
- $ this ->stdout ("not running! " . PHP_EOL );
165
+ $ this ->stdout ("server is not running! " . PHP_EOL );
255
166
exit (1 );
256
167
}
257
168
}
0 commit comments