Subversion Repositories php-qbpwcf

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
398 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_threads::proc_streams_select_20260718";
30
 
31
#要執行的php程式內容
32
$cmdString1=<<<'PHP'
33
#迴圈
34
for($i=10;$i>0;$i--){
35
 
36
	#提示還要跑幾秒
37
	echo "Program end in ".$i." sec(s)";
38
 
39
	#sleep 1 sec
40
	sleep(1);
41
 
42
	}#for end
43
PHP;
44
 
45
#要執行的php程式內容
46
$cmdString2=<<<'PHP'
47
#迴圈
48
for($i=1;$i<=10;$i++){
49
 
50
	#sleep 1 sec
51
	sleep(1);
52
 
53
	#提示還要跑幾秒
54
	echo "Program end when count to 10(".$i.")";
55
 
56
	}#for end
57
PHP;
58
 
59
#函式說明:
60
#透過proc來多執行序運作.
61
#回傳結果:
62
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
63
#$reuslt["error"],執行不正常結束的錯訊息陣列.
64
#$result["function"],當前執行的函式名稱.
65
#$result["argu"],使用的參數.
66
#$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"代表程序的資訊.
67
#必填參數:
68
#$conf["cmds"],字串陣列,每個元素代表單一程序要執行的指令與參數.
69
$conf["cmds"]=array("php -r ".escapeshellarg($cmdString1),"php -r ".escapeshellarg($cmdString2));
70
#可省略參數:
71
#$conf["wait"],字串,是否需要等待所有程序結束,預設為"true"要等待;反之為"false"不要等待.
72
$conf["wait"]="false";
73
#$conf["timeout"],字串陣列,每個元素代表單一程序執行的最大等待秒數,超過後將會強迫停止執行,僅當wait參數為"true"時生效.
74
#$conf["timeout"]=array("10");
75
#$conf["workingDir"],字串陣列,個別程式執行時的家目錄,預設不指定.
76
#$conf["workingDir"]=array("path");
77
#$conf["envs"],2維字串陣列,每個元素代表個別程式執行時的指定環境變數,key變數名稱;value為變數內容.預設為array("QBPWCF" => "Quick Build PHP Website Componment base on Fedora Linux");
78
#$conf["envs"]=array(array("key"=>"value"));
79
#$conf["executeBy"],字串陣列,每個元素代表個別指令要用什麼程式執行,預設為"bash".
80
#$conf["executeBy"]=array("bash");
81
#參考資料:
82
#https://www.php.net/manual/en/function.proc-open.php
83
#https://www.php.net/manual/en/function.proc-get-status.php
84
#https://www.php.net/manual/en/function.proc-terminate.php
85
#備註:
86
#當wait參數為"true"時,會自動解析stdout與stderr,因此不用再透過 self::proc_update 來更新.
87
#當wait參數不為"true"時,若需要取得當下的執行狀況,請使用 self::proc_update 來更新.
88
$proc=threads::proc($conf);
89
unset($conf);
90
 
91
#如果執行異常
92
if($proc["status"]==="false"){
93
 
94
	#debug
95
	var_dump($proc);
96
 
97
	#結束執行回傳1
98
	exit(1);
99
 
100
	}#if end
101
 
102
#無窮迴圈
103
while(true){
104
 
105
	#函式說明:
106
	#類似stream_select的替代方案, 用來檢查透過proc運行的多執行序其標準輸出、錯誤輸出是否有訊息,程序是否已經結束.
107
	#回傳結果:
108
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
109
	#$reuslt["error"],執行不正常結束的錯訊息陣列.
110
	#$result["function"],當前執行的函式名稱.
111
	#$result["argu"],使用的參數.
112
	#$result["content"],陣列,運行中的程序,可作為下次呼叫本函式的procs參數.
113
	#$result["streams"],陣列,元素為對應channelName索引的輸出.
114
	#$result["allStreamsClosed"],字串,"true"代表streams參數的通道已經全部關閉;反之為"false".
115
	#$result["gotMsg"],字串,"true"代表有訊息輸出;反之為"false".
116
	#必填參數:
117
	#$conf["procs"],陣列,threads::proc回傳的content.
118
	$conf["procs"]=$proc["content"];
119
	#可省略參數:
120
	#$conf["channelName"],陣列,每個元素為兩個元素的陣列,第一個元素為對應procs元素的標準輸出的stream/channel名稱,預設為std_$index,第二個元素為對應procs元素的錯誤輸出,預設為err_$index,$index為從0開始的整數.
121
	#$conf["channelName"]=array();
122
	#$conf["periodToWait"],整數,要等待多少秒來嘗試取得訊息,預設為1秒鐘.
123
	#$conf["periodToWait"]=1;
124
	#參考資料:
125
	#https://www.php.net/manual/en/function.proc-open.php
126
	#https://www.php.net/manual/en/function.proc-get-status.php
127
	#https://www.php.net/manual/en/function.proc-terminate.php
128
	#備註:
129
	#無.
130
	$proc_streams_select=threads::proc_streams_select($conf);
131
	unset($conf);
132
 
133
	#如果執行異常
134
	if($proc_streams_select["status"]==="false"){
135
 
136
		#debug
137
		var_dump($proc_streams_select);
138
 
139
		#結束並回傳shell 1
140
		exit(1);
141
 
142
		}#if end
143
 
144
	#如果有訊息
145
	if($proc_streams_select["gotMsg"]==="true"){
146
 
147
		#針對有輸出的通道
148
		foreach($proc_streams_select["streams"] as $channel => $msg){
149
 
150
			#debug
151
			#var_dump(__LINE__,$msg);
152
 
153
			#輸出 channel 名稱與其訊息
154
			echo $channel." > ".$msg.PHP_EOL;
155
 
156
			}#foreach end
157
 
158
		}#if end
159
 
160
	#如果執行的程序都結束了
161
	if($proc_streams_select["allStreamsClosed"]==="true"){
162
 
163
		#提示結束運行了
164
		echo "exit".PHP_EOL;
165
 
166
		#正常結束運行
167
		exit;
168
 
169
		}#if end
170
 
171
	#取得尚在運行中的程序
172
	$proc["content"]=$proc_streams_select["content"];
173
 
174
	}#while end