Subversion Repositories php-qbpwcf

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
397 liveuser 1
<?php
2
 
3
/*
4
 
5
        QBPWCF, Quick Build PHP website Component base on Fedora Linux.
6
    Copyright (C) 2014~2026 MIN ZHI, CHEN
7
 
8
    This file is part of QBPWCF.
9
 
10
    QBPWCF is free software: you can redistribute it and/or modify
11
    it under the terms of the GNU General Public License as published by
12
    the Free Software Foundation, either version 3 of the License, or
13
    (at your option) any later version.
14
 
15
    QBPWCF is distributed in the hope that it will be useful,
16
    but WITHOUT ANY WARRANTY; without even the implied warranty of
17
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
    GNU General Public License for more details.
19
 
20
    You should have received a copy of the GNU General Public License
21
    along with QBPWCF.  If not, see <http://www.gnu.org/licenses/>.
22
 
23
*/
24
 
25
#使用命名空間qbpwcf
26
namespace qbpwcf;
27
 
28
#assets dir
29
$assetsDir="assets of resource::server_20260701";
30
 
31
#working dir
32
$workingDir=pathinfo(__FILE__)["dirname"]."/../".$assetsDir;
33
 
34
#server 端的指令
35
$server_cmd="php -f server.php";
36
 
37
#clent 端寫的指令
38
$client_w_cmd="php -f client_w.php";
39
 
40
#clent 端讀的指令
41
$client_r_cmd="php -f client_r.php";
42
 
43
#設置 server 的 std channel 名稱
44
$server_std_ChannelName="server_std";
45
 
46
#設置 server 的 err channel 名稱
47
$server_err_ChannelName="server_err";
48
 
49
#設置 client 寫入操作 的 std channel 名稱
50
$client_w_std_ChannelName="client(W)_std";
51
 
52
#設置 client 寫入操作 的 err channel 名稱
53
$client_w_err_ChannelName="client(W)_err";
54
 
55
#設置 client 讀取操作 的 std channel 名稱
56
$client_r_std_ChannelName="client(R)_std";
57
 
58
#設置 client 讀取操作 的 err channel 名稱
59
$client_r_err_ChannelName="client(R)_err";
60
 
61
#要監控輸出的streams
62
$streams=array();
63
 
64
#運行server端程式
65
#函式說明:
66
#透過proc來多執行序運作.
67
#回傳結果:
68
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
69
#$reuslt["error"],執行不正常結束的錯訊息陣列.
70
#$result["function"],當前執行的函式名稱.
71
#$result["argu"],使用的參數.
72
#$result["content"],陣列,每個元素為其指令執行的結果訊息陣列,key為"status"代表執行是否正常的識別;key為"statusCode"代表程式結束後回傳給對應executeBy程式的數值,若為"?"則代表程序尚未結束,若為整數0,則代表順利執行結束,可透過proc_update函式進行資訊的更新與取得;key為"content"代表標準輸出,若為resource,則代表為pipe;key為"error"代表非標準輸出,若為resource,則代表為pipe;key為"input"代表成功輸入的指令;key為"process"代表該程序經proc_open後的process source;key為"proc_get_status"代表程序的資訊.
73
#必填參數:
74
#$conf["cmds"],字串陣列,每個元素代表單一程序要執行的指令與參數.
75
$conf["cmds"]=array($server_cmd);
76
#可省略參數:
77
#$conf["wait"],字串,是否需要等待所有程序結束,預設為"true"要等待;反之為"false"不要等待.
78
$conf["wait"]="false";
79
#$conf["timeout"],字串陣列,每個元素代表單一程序執行的最大等待秒數,超過後將會強迫停止執行,僅當wait參數為"true"時生效.
80
#$conf["timeout"]=array("10");
81
#$conf["workingDir"],字串陣列,個別程式執行時的家目錄,預設不指定.
82
$conf["workingDir"]=array($workingDir);
83
#$conf["envs"],2維字串陣列,每個元素代表個別程式執行時的指定環境變數,key變數名稱;value為變數內容.預設為array("QBPWCF" => "Quick Build PHP Website Componment base on Fedora Linux");
84
#$conf["envs"]=array(array("key"=>"value"));
85
#$conf["executeBy"],字串陣列,每個元素代表個別指令要用什麼程式執行,預設為"bash".
86
#$conf["executeBy"]=array("bash");
87
#參考資料:
88
#https://www.php.net/manual/en/function.proc-open.php
89
#https://www.php.net/manual/en/function.proc-get-status.php
90
#https://www.php.net/manual/en/function.proc-terminate.php
91
#備註:
92
#當wait參數為"true"時,會自動解析stdout與stderr,因此不用再透過 self::proc_update 來更新.
93
#當wait參數不為"true"時,若需要取得當下的執行狀況,請使用 self::proc_update 來更新.
94
$proc_s=threads::proc($conf);
95
unset($conf);
96
 
97
#如果執行異常
98
if($proc_s["status"]==="false"){
99
 
100
	#debug
101
	var_dump($proc_s);
102
 
103
	#結束執行,回傳shell 1.
104
	exit(1);
105
 
106
	}#if end
107
 
108
#如果程序不是持續運行中
109
if($proc_s["content"][0]["statusCode"]!=="?"){
110
 
111
	#debug
112
	var_dump($proc_s);
113
 
114
	#結束執行,回傳shell 1.
115
	exit(1);
116
 
117
	}#if end
118
 
119
/*
120
 
121
#取得 server.php 的標準輸出
122
$serverStdOut=&$proc_s["content"][0]["content"];
123
 
124
#儲存 server.php 標準輸出的 channel
125
$streams[$server_std_ChannelName]=$serverStdOut;
126
 
127
#取得 server.php 的錯誤輸出
128
$serverErrOut=&$proc_s["content"][0]["error"];
129
 
130
#儲存 server.php 錯誤輸出的 channel
131
$streams[$server_err_ChannelName]=$serverErrOut;
132
 
133
*/
134
 
135
#提示 server 運行中且提供 pid
136
echo "server(pid:".$proc_s["content"][0]["proc_get_status"]["pid"].") is running....".PHP_EOL;
137
 
138
#運行 client_w.php
139
#函式說明:
140
#透過proc來多執行序運作.
141
#回傳結果:
142
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
143
#$reuslt["error"],執行不正常結束的錯訊息陣列.
144
#$result["function"],當前執行的函式名稱.
145
#$result["argu"],使用的參數.
146
#$result["content"],陣列,每個元素為其指令執行的結果訊息陣列,key為"status"代表執行是否正常的識別;key為"statusCode"代表程式結束後回傳給對應executeBy程式的數值,若為"?"則代表程序尚未結束,若為整數0,則代表順利執行結束,可透過proc_update函式進行資訊的更新與取得;key為"content"代表標準輸出,若為resource,則代表為pipe;key為"error"代表非標準輸出,若為resource,則代表為pipe;key為"input"代表成功輸入的指令;key為"process"代表該程序經proc_open後的process source;key為"proc_get_status"代表程序的資訊.
147
#必填參數:
148
#$conf["cmds"],字串陣列,每個元素代表單一程序要執行的指令與參數.
149
$conf["cmds"]=array($client_w_cmd);
150
#可省略參數:
151
#$conf["wait"],字串,是否需要等待所有程序結束,預設為"true"要等待;反之為"false"不要等待.
152
$conf["wait"]="false";
153
#$conf["timeout"],字串陣列,每個元素代表單一程序執行的最大等待秒數,超過後將會強迫停止執行,僅當wait參數為"true"時生效.
154
#$conf["timeout"]=array("10");
155
#$conf["workingDir"],字串陣列,個別程式執行時的家目錄,預設不指定.
156
$conf["workingDir"]=array($workingDir);
157
#$conf["envs"],2維字串陣列,每個元素代表個別程式執行時的指定環境變數,key變數名稱;value為變數內容.預設為array("QBPWCF" => "Quick Build PHP Website Componment base on Fedora Linux");
158
#$conf["envs"]=array(array("key"=>"value"));
159
#$conf["executeBy"],字串陣列,每個元素代表個別指令要用什麼程式執行,預設為"bash".
160
#$conf["executeBy"]=array("bash");
161
#參考資料:
162
#https://www.php.net/manual/en/function.proc-open.php
163
#https://www.php.net/manual/en/function.proc-get-status.php
164
#https://www.php.net/manual/en/function.proc-terminate.php
165
#備註:
166
#當wait參數為"true"時,會自動解析stdout與stderr,因此不用再透過 self::proc_update 來更新.
167
#當wait參數不為"true"時,若需要取得當下的執行狀況,請使用 self::proc_update 來更新.
168
$proc_cw=threads::proc($conf);
169
unset($conf);
170
 
171
#如果執行異常
172
if($proc_cw["status"]==="false"){
173
 
174
	#debug
175
	var_dump($proc_cw);
176
 
177
	#結束執行,回傳shell 1.
178
	exit(1);
179
 
180
	}#if end
181
 
182
/*
183
 
184
#取得 client_w.php 的標準輸出
185
$client_w_StdOut=&$proc_cw["content"][0]["content"];
186
 
187
#儲存 client_w.php 標準輸出的 channel
188
$streams[$client_w_std_ChannelName]=$client_w_StdOut;
189
 
190
#取得 client_w.php 的錯誤輸出
191
$client_w_ErrOut=&$proc_cw["content"][0]["error"];
192
 
193
#儲存 client_w.php 錯誤輸出的 channel
194
$streams[$client_w_err_ChannelName]=$client_w_ErrOut;
195
 
196
#提示 client_w.php 有運行且提供 pid
197
echo "client for writing(pid:".$proc_cw["content"][0]["proc_get_status"]["pid"].") is running....".PHP_EOL;
198
 
199
*/
200
 
201
#初始化給予 threads::proc_streams_select 的 procs 參數
202
$procs=array($proc_s["content"][0],$proc_cw["content"][0]);
203
 
204
#無窮迴圈
205
while(true){
206
 
207
	#函式說明:
208
	#類似stream_select的替代方案, 用來檢查透過proc運行的多執行序其標準輸出、錯誤輸出是否有訊息,程序是否已經結束.
209
	#回傳結果:
210
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
211
	#$reuslt["error"],執行不正常結束的錯訊息陣列.
212
	#$result["function"],當前執行的函式名稱.
213
	#$result["argu"],使用的參數.
214
	#$result["content"],陣列,運行中的程序,可作為下次呼叫本函式的procs參數.
215
	#$result["streams"],陣列,元素為對應channelName索引的輸出.
216
	#$result["allStreamsClosed"],字串,"true"代表streams參數的通道已經全部關閉;反之為"false".
217
	#$result["gotMsg"],字串,"true"代表有訊息輸出;反之為"false".
218
	#必填參數:
219
	#$conf["procs"],陣列,threads::proc回傳的content.
220
	$conf["procs"]=$procs;
221
	#可省略參數:
222
	#$conf["channelName"],陣列,每個元素為兩個元素的陣列,第一個元素為對應procs元素的標準輸出的stream/channel名稱,預設為std_$index,第二個元素為對應procs元素的錯誤輸出,預設為err_$index,$index為從0開始的整數.
223
	$conf["channelName"]=array(array("server_std","server_err"),array("client_w_std","client_w_err"));
224
	#$conf["periodToWait"],整數,要等待多少秒來嘗試取得訊息,預設為1秒鐘.
225
	#$conf["periodToWait"]=1;
226
	#參考資料:
227
	#https://www.php.net/manual/en/function.proc-open.php
228
	#https://www.php.net/manual/en/function.proc-get-status.php
229
	#https://www.php.net/manual/en/function.proc-terminate.php
230
	#備註:
231
	#無.
232
	$proc_streams_select=threads::proc_streams_select($conf);
233
	unset($conf);
234
 
235
	#如果執行異常
236
	if($proc_streams_select["status"]==="false"){
237
 
238
		#debug
239
		var_dump($proc_streams_select);
240
 
241
		#結束執行,回傳shell 1.
242
		exit(1);
243
 
244
		}#if end
245
 
246
	#如果有輸出的訊息
247
	if($proc_streams_select["gotMsg"]==="true"){
248
 
249
		#針對每個 streams 的輸出
250
		foreach($proc_streams_select["streams"] as $channelName => $msg){
251
 
252
			#輸出訊息
253
			echo $channelName." > ".$msg.PHP_EOL;
254
 
255
			}#foreach end
256
 
257
		}#if end
258
 
259
	#取得剩下依然有開啟的 streams
260
	$procs=$proc_streams_select["content"];
261
 
262
	#如果只剩下一個 streams
263
	if(count($procs)===1){
264
 
265
		#跳出迴圈
266
		break;
267
 
268
		}#if end
269
 
270
	}#while end
271
 
272
#運行 client_r.php
273
#函式說明:
274
#透過proc來多執行序運作.
275
#回傳結果:
276
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
277
#$reuslt["error"],執行不正常結束的錯訊息陣列.
278
#$result["function"],當前執行的函式名稱.
279
#$result["argu"],使用的參數.
280
#$result["content"],陣列,每個元素為其指令執行的結果訊息陣列,key為"status"代表執行是否正常的識別;key為"statusCode"代表程式結束後回傳給對應executeBy程式的數值,若為"?"則代表程序尚未結束,若為整數0,則代表順利執行結束,可透過proc_update函式進行資訊的更新與取得;key為"content"代表標準輸出,若為resource,則代表為pipe;key為"error"代表非標準輸出,若為resource,則代表為pipe;key為"input"代表成功輸入的指令;key為"process"代表該程序經proc_open後的process source;key為"proc_get_status"代表程序的資訊.
281
#必填參數:
282
#$conf["cmds"],字串陣列,每個元素代表單一程序要執行的指令與參數.
283
$conf["cmds"]=array($client_r_cmd);
284
#可省略參數:
285
#$conf["wait"],字串,是否需要等待所有程序結束,預設為"true"要等待;反之為"false"不要等待.
286
$conf["wait"]="false";
287
#$conf["timeout"],字串陣列,每個元素代表單一程序執行的最大等待秒數,超過後將會強迫停止執行,僅當wait參數為"true"時生效.
288
#$conf["timeout"]=array("10");
289
#$conf["workingDir"],字串陣列,個別程式執行時的家目錄,預設不指定.
290
$conf["workingDir"]=array($workingDir);
291
#$conf["envs"],2維字串陣列,每個元素代表個別程式執行時的指定環境變數,key變數名稱;value為變數內容.預設為array("QBPWCF" => "Quick Build PHP Website Componment base on Fedora Linux");
292
#$conf["envs"]=array(array("key"=>"value"));
293
#$conf["executeBy"],字串陣列,每個元素代表個別指令要用什麼程式執行,預設為"bash".
294
#$conf["executeBy"]=array("bash");
295
#參考資料:
296
#https://www.php.net/manual/en/function.proc-open.php
297
#https://www.php.net/manual/en/function.proc-get-status.php
298
#https://www.php.net/manual/en/function.proc-terminate.php
299
#備註:
300
#當wait參數為"true"時,會自動解析stdout與stderr,因此不用再透過 self::proc_update 來更新.
301
#當wait參數不為"true"時,若需要取得當下的執行狀況,請使用 self::proc_update 來更新.
302
$proc_cr=threads::proc($conf);
303
unset($conf);
304
 
305
#如果執行異常
306
if($proc_cr["status"]==="false"){
307
 
308
	#debug
309
	var_dump($proc_cr);
310
 
311
	#結束執行,回傳shell 1.
312
	exit(1);
313
 
314
	}#if end
315
 
316
/*
317
 
318
#取得 client_r.php 的標準輸出
319
$client_r_StdOut=&$proc_cr["content"][0]["content"];
320
 
321
#儲存 client_r.php 的 std channel
322
$streams[$client_r_std_ChannelName]=$client_r_StdOut;
323
 
324
#取得 client_r.php 的錯誤輸出
325
$client_r_ErrOut=&$proc_cr["content"][0]["error"];
326
 
327
#儲存 client_r.php 的 err channel
328
$streams[$client_r_err_ChannelName]=$client_r_ErrOut;
329
 
330
*/
331
 
332
#提示 client_r.php 有運行且提供 pid
333
echo "client for reading(pid:".$proc_cr["content"][0]["proc_get_status"]["pid"].") is running....".PHP_EOL;
334
 
335
#初始化給予 threads::proc_streams_select 的 procs 參數
336
$procs[]=$proc_cr["content"][0];
337
 
338
#無窮迴圈
339
while(true){
340
 
341
	#debug
342
	#var_dump(__LINE__,$procs);
343
 
344
	#函式說明:
345
	#類似stream_select的替代方案, 用來檢查透過proc運行的多執行序其標準輸出、錯誤輸出是否有訊息,程序是否已經結束.
346
	#回傳結果:
347
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
348
	#$reuslt["error"],執行不正常結束的錯訊息陣列.
349
	#$result["function"],當前執行的函式名稱.
350
	#$result["argu"],使用的參數.
351
	#$result["content"],陣列,運行中的程序,可作為下次呼叫本函式的procs參數.
352
	#$result["streams"],陣列,元素為對應channelName索引的輸出.
353
	#$result["allStreamsClosed"],字串,"true"代表streams參數的通道已經全部關閉;反之為"false".
354
	#$result["gotMsg"],字串,"true"代表有訊息輸出;反之為"false".
355
	#必填參數:
356
	#$conf["procs"],陣列,threads::proc回傳的content.
357
	$conf["procs"]=$procs;
358
	#可省略參數:
359
	#$conf["channelName"],陣列,每個元素為兩個元素的陣列,第一個元素為對應procs元素的標準輸出的stream/channel名稱,預設為std_$index,第二個元素為對應procs元素的錯誤輸出,預設為err_$index,$index為從0開始的整數.
360
	$conf["channelName"]=array(array("server_std","server_err"),array("client_r_std","client_r_err"));
361
	#$conf["periodToWait"],整數,要等待多少秒來嘗試取得訊息,預設為1秒鐘.
362
	#$conf["periodToWait"]=1;
363
	#參考資料:
364
	#https://www.php.net/manual/en/function.proc-open.php
365
	#https://www.php.net/manual/en/function.proc-get-status.php
366
	#https://www.php.net/manual/en/function.proc-terminate.php
367
	#備註:
368
	#無.
369
	$proc_streams_select=threads::proc_streams_select($conf);
370
	unset($conf);
371
 
372
	#如果執行異常
373
	if($proc_streams_select["status"]==="false"){
374
 
375
		#debug
376
		var_dump($proc_streams_select);
377
 
378
		#結束執行,回傳shell 1.
379
		exit(1);
380
 
381
		}#if end
382
 
383
	#如果有輸出的訊息
384
	if($proc_streams_select["gotMsg"]==="true"){
385
 
386
		#針對每個 streams 的輸出
387
		foreach($proc_streams_select["streams"] as $channelName => $msg){
388
 
389
			#輸出訊息
390
			echo $channelName." > ".$msg.PHP_EOL;
391
 
392
			}#foreach end
393
 
394
		}#if end
395
 
396
	#取得剩下依然執行的程序
397
	$procs=$proc_streams_select["content"];
398
 
399
	#debug
400
	#var_dump(__LINE__,$proc_streams_select);
401
 
402
	#如果只剩下一個 streams
403
	if(count($procs)===1){
404
 
405
		#跳出迴圈
406
		break;
407
 
408
		}#if end
409
 
410
	}#while end
411
 
412
#提示運行結束了
413
echo "exit".PHP_EOL;