Blame | Last modification | View Log | RSS feed
<?php/*QBPWCF, Quick Build PHP website Component base on Fedora Linux.Copyright (C) 2014~2026 MIN ZHI, CHENThis file is part of QBPWCF.QBPWCF is free software: you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation, either version 3 of the License, or(at your option) any later version.QBPWCF is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with QBPWCF. If not, see <http://www.gnu.org/licenses/>.*/#使用命名空間qbpwcfnamespace qbpwcf;#assets dir$assetsDir="assets of resource::server_20260701";#working dir$workingDir=pathinfo(__FILE__)["dirname"]."/../".$assetsDir;#server 端的指令$server_cmd="php -f server.php";#clent 端寫的指令$client_w_cmd="php -f client_w.php";#clent 端讀的指令$client_r_cmd="php -f client_r.php";#設置 server 的 std channel 名稱$server_std_ChannelName="server_std";#設置 server 的 err channel 名稱$server_err_ChannelName="server_err";#設置 client 寫入操作 的 std channel 名稱$client_w_std_ChannelName="client(W)_std";#設置 client 寫入操作 的 err channel 名稱$client_w_err_ChannelName="client(W)_err";#設置 client 讀取操作 的 std channel 名稱$client_r_std_ChannelName="client(R)_std";#設置 client 讀取操作 的 err channel 名稱$client_r_err_ChannelName="client(R)_err";#要監控輸出的streams$streams=array();#運行server端程式#函式說明:#透過proc來多執行序運作.#回傳結果:#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.#$reuslt["error"],執行不正常結束的錯訊息陣列.#$result["function"],當前執行的函式名稱.#$result["argu"],使用的參數.#$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"代表程序的資訊.#必填參數:#$conf["cmds"],字串陣列,每個元素代表單一程序要執行的指令與參數.$conf["cmds"]=array($server_cmd);#可省略參數:#$conf["wait"],字串,是否需要等待所有程序結束,預設為"true"要等待;反之為"false"不要等待.$conf["wait"]="false";#$conf["timeout"],字串陣列,每個元素代表單一程序執行的最大等待秒數,超過後將會強迫停止執行,僅當wait參數為"true"時生效.#$conf["timeout"]=array("10");#$conf["workingDir"],字串陣列,個別程式執行時的家目錄,預設不指定.$conf["workingDir"]=array($workingDir);#$conf["envs"],2維字串陣列,每個元素代表個別程式執行時的指定環境變數,key變數名稱;value為變數內容.預設為array("QBPWCF" => "Quick Build PHP Website Componment base on Fedora Linux");#$conf["envs"]=array(array("key"=>"value"));#$conf["executeBy"],字串陣列,每個元素代表個別指令要用什麼程式執行,預設為"bash".#$conf["executeBy"]=array("bash");#參考資料:#https://www.php.net/manual/en/function.proc-open.php#https://www.php.net/manual/en/function.proc-get-status.php#https://www.php.net/manual/en/function.proc-terminate.php#備註:#當wait參數為"true"時,會自動解析stdout與stderr,因此不用再透過 self::proc_update 來更新.#當wait參數不為"true"時,若需要取得當下的執行狀況,請使用 self::proc_update 來更新.$proc_s=threads::proc($conf);unset($conf);#如果執行異常if($proc_s["status"]==="false"){#debugvar_dump($proc_s);#結束執行,回傳shell 1.exit(1);}#if end#如果程序不是持續運行中if($proc_s["content"][0]["statusCode"]!=="?"){#debugvar_dump($proc_s);#結束執行,回傳shell 1.exit(1);}#if end/*#取得 server.php 的標準輸出$serverStdOut=&$proc_s["content"][0]["content"];#儲存 server.php 標準輸出的 channel$streams[$server_std_ChannelName]=$serverStdOut;#取得 server.php 的錯誤輸出$serverErrOut=&$proc_s["content"][0]["error"];#儲存 server.php 錯誤輸出的 channel$streams[$server_err_ChannelName]=$serverErrOut;*/#提示 server 運行中且提供 pidecho "server(pid:".$proc_s["content"][0]["proc_get_status"]["pid"].") is running....".PHP_EOL;#運行 client_w.php#函式說明:#透過proc來多執行序運作.#回傳結果:#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.#$reuslt["error"],執行不正常結束的錯訊息陣列.#$result["function"],當前執行的函式名稱.#$result["argu"],使用的參數.#$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"代表程序的資訊.#必填參數:#$conf["cmds"],字串陣列,每個元素代表單一程序要執行的指令與參數.$conf["cmds"]=array($client_w_cmd);#可省略參數:#$conf["wait"],字串,是否需要等待所有程序結束,預設為"true"要等待;反之為"false"不要等待.$conf["wait"]="false";#$conf["timeout"],字串陣列,每個元素代表單一程序執行的最大等待秒數,超過後將會強迫停止執行,僅當wait參數為"true"時生效.#$conf["timeout"]=array("10");#$conf["workingDir"],字串陣列,個別程式執行時的家目錄,預設不指定.$conf["workingDir"]=array($workingDir);#$conf["envs"],2維字串陣列,每個元素代表個別程式執行時的指定環境變數,key變數名稱;value為變數內容.預設為array("QBPWCF" => "Quick Build PHP Website Componment base on Fedora Linux");#$conf["envs"]=array(array("key"=>"value"));#$conf["executeBy"],字串陣列,每個元素代表個別指令要用什麼程式執行,預設為"bash".#$conf["executeBy"]=array("bash");#參考資料:#https://www.php.net/manual/en/function.proc-open.php#https://www.php.net/manual/en/function.proc-get-status.php#https://www.php.net/manual/en/function.proc-terminate.php#備註:#當wait參數為"true"時,會自動解析stdout與stderr,因此不用再透過 self::proc_update 來更新.#當wait參數不為"true"時,若需要取得當下的執行狀況,請使用 self::proc_update 來更新.$proc_cw=threads::proc($conf);unset($conf);#如果執行異常if($proc_cw["status"]==="false"){#debugvar_dump($proc_cw);#結束執行,回傳shell 1.exit(1);}#if end/*#取得 client_w.php 的標準輸出$client_w_StdOut=&$proc_cw["content"][0]["content"];#儲存 client_w.php 標準輸出的 channel$streams[$client_w_std_ChannelName]=$client_w_StdOut;#取得 client_w.php 的錯誤輸出$client_w_ErrOut=&$proc_cw["content"][0]["error"];#儲存 client_w.php 錯誤輸出的 channel$streams[$client_w_err_ChannelName]=$client_w_ErrOut;#提示 client_w.php 有運行且提供 pidecho "client for writing(pid:".$proc_cw["content"][0]["proc_get_status"]["pid"].") is running....".PHP_EOL;*/#初始化給予 threads::proc_streams_select 的 procs 參數$procs=array($proc_s["content"][0],$proc_cw["content"][0]);#無窮迴圈while(true){#函式說明:#類似stream_select的替代方案, 用來檢查透過proc運行的多執行序其標準輸出、錯誤輸出是否有訊息,程序是否已經結束.#回傳結果:#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.#$reuslt["error"],執行不正常結束的錯訊息陣列.#$result["function"],當前執行的函式名稱.#$result["argu"],使用的參數.#$result["content"],陣列,運行中的程序,可作為下次呼叫本函式的procs參數.#$result["streams"],陣列,元素為對應channelName索引的輸出.#$result["allStreamsClosed"],字串,"true"代表streams參數的通道已經全部關閉;反之為"false".#$result["gotMsg"],字串,"true"代表有訊息輸出;反之為"false".#必填參數:#$conf["procs"],陣列,threads::proc回傳的content.$conf["procs"]=$procs;#可省略參數:#$conf["channelName"],陣列,每個元素為兩個元素的陣列,第一個元素為對應procs元素的標準輸出的stream/channel名稱,預設為std_$index,第二個元素為對應procs元素的錯誤輸出,預設為err_$index,$index為從0開始的整數.$conf["channelName"]=array(array("server_std","server_err"),array("client_w_std","client_w_err"));#$conf["periodToWait"],整數,要等待多少秒來嘗試取得訊息,預設為1秒鐘.#$conf["periodToWait"]=1;#參考資料:#https://www.php.net/manual/en/function.proc-open.php#https://www.php.net/manual/en/function.proc-get-status.php#https://www.php.net/manual/en/function.proc-terminate.php#備註:#無.$proc_streams_select=threads::proc_streams_select($conf);unset($conf);#如果執行異常if($proc_streams_select["status"]==="false"){#debugvar_dump($proc_streams_select);#結束執行,回傳shell 1.exit(1);}#if end#如果有輸出的訊息if($proc_streams_select["gotMsg"]==="true"){#針對每個 streams 的輸出foreach($proc_streams_select["streams"] as $channelName => $msg){#輸出訊息echo $channelName." > ".$msg.PHP_EOL;}#foreach end}#if end#取得剩下依然有開啟的 streams$procs=$proc_streams_select["content"];#如果只剩下一個 streamsif(count($procs)===1){#跳出迴圈break;}#if end}#while end#運行 client_r.php#函式說明:#透過proc來多執行序運作.#回傳結果:#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.#$reuslt["error"],執行不正常結束的錯訊息陣列.#$result["function"],當前執行的函式名稱.#$result["argu"],使用的參數.#$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"代表程序的資訊.#必填參數:#$conf["cmds"],字串陣列,每個元素代表單一程序要執行的指令與參數.$conf["cmds"]=array($client_r_cmd);#可省略參數:#$conf["wait"],字串,是否需要等待所有程序結束,預設為"true"要等待;反之為"false"不要等待.$conf["wait"]="false";#$conf["timeout"],字串陣列,每個元素代表單一程序執行的最大等待秒數,超過後將會強迫停止執行,僅當wait參數為"true"時生效.#$conf["timeout"]=array("10");#$conf["workingDir"],字串陣列,個別程式執行時的家目錄,預設不指定.$conf["workingDir"]=array($workingDir);#$conf["envs"],2維字串陣列,每個元素代表個別程式執行時的指定環境變數,key變數名稱;value為變數內容.預設為array("QBPWCF" => "Quick Build PHP Website Componment base on Fedora Linux");#$conf["envs"]=array(array("key"=>"value"));#$conf["executeBy"],字串陣列,每個元素代表個別指令要用什麼程式執行,預設為"bash".#$conf["executeBy"]=array("bash");#參考資料:#https://www.php.net/manual/en/function.proc-open.php#https://www.php.net/manual/en/function.proc-get-status.php#https://www.php.net/manual/en/function.proc-terminate.php#備註:#當wait參數為"true"時,會自動解析stdout與stderr,因此不用再透過 self::proc_update 來更新.#當wait參數不為"true"時,若需要取得當下的執行狀況,請使用 self::proc_update 來更新.$proc_cr=threads::proc($conf);unset($conf);#如果執行異常if($proc_cr["status"]==="false"){#debugvar_dump($proc_cr);#結束執行,回傳shell 1.exit(1);}#if end/*#取得 client_r.php 的標準輸出$client_r_StdOut=&$proc_cr["content"][0]["content"];#儲存 client_r.php 的 std channel$streams[$client_r_std_ChannelName]=$client_r_StdOut;#取得 client_r.php 的錯誤輸出$client_r_ErrOut=&$proc_cr["content"][0]["error"];#儲存 client_r.php 的 err channel$streams[$client_r_err_ChannelName]=$client_r_ErrOut;*/#提示 client_r.php 有運行且提供 pidecho "client for reading(pid:".$proc_cr["content"][0]["proc_get_status"]["pid"].") is running....".PHP_EOL;#初始化給予 threads::proc_streams_select 的 procs 參數$procs[]=$proc_cr["content"][0];#無窮迴圈while(true){#debug#var_dump(__LINE__,$procs);#函式說明:#類似stream_select的替代方案, 用來檢查透過proc運行的多執行序其標準輸出、錯誤輸出是否有訊息,程序是否已經結束.#回傳結果:#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.#$reuslt["error"],執行不正常結束的錯訊息陣列.#$result["function"],當前執行的函式名稱.#$result["argu"],使用的參數.#$result["content"],陣列,運行中的程序,可作為下次呼叫本函式的procs參數.#$result["streams"],陣列,元素為對應channelName索引的輸出.#$result["allStreamsClosed"],字串,"true"代表streams參數的通道已經全部關閉;反之為"false".#$result["gotMsg"],字串,"true"代表有訊息輸出;反之為"false".#必填參數:#$conf["procs"],陣列,threads::proc回傳的content.$conf["procs"]=$procs;#可省略參數:#$conf["channelName"],陣列,每個元素為兩個元素的陣列,第一個元素為對應procs元素的標準輸出的stream/channel名稱,預設為std_$index,第二個元素為對應procs元素的錯誤輸出,預設為err_$index,$index為從0開始的整數.$conf["channelName"]=array(array("server_std","server_err"),array("client_r_std","client_r_err"));#$conf["periodToWait"],整數,要等待多少秒來嘗試取得訊息,預設為1秒鐘.#$conf["periodToWait"]=1;#參考資料:#https://www.php.net/manual/en/function.proc-open.php#https://www.php.net/manual/en/function.proc-get-status.php#https://www.php.net/manual/en/function.proc-terminate.php#備註:#無.$proc_streams_select=threads::proc_streams_select($conf);unset($conf);#如果執行異常if($proc_streams_select["status"]==="false"){#debugvar_dump($proc_streams_select);#結束執行,回傳shell 1.exit(1);}#if end#如果有輸出的訊息if($proc_streams_select["gotMsg"]==="true"){#針對每個 streams 的輸出foreach($proc_streams_select["streams"] as $channelName => $msg){#輸出訊息echo $channelName." > ".$msg.PHP_EOL;}#foreach end}#if end#取得剩下依然執行的程序$procs=$proc_streams_select["content"];#debug#var_dump(__LINE__,$proc_streams_select);#如果只剩下一個 streamsif(count($procs)===1){#跳出迴圈break;}#if end}#while end#提示運行結束了echo "exit".PHP_EOL;