Subversion Repositories php-qbpwcf

Rev

Rev 66 | Rev 159 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 66 Rev 140
Line 327... Line 327...
327
	#$conf["cmds"],字串陣列,每個元素代表單一程序要執行的指令與參數.
327
	#$conf["cmds"],字串陣列,每個元素代表單一程序要執行的指令與參數.
328
	#$conf["cmds"]=array();
328
	#$conf["cmds"]=array();
329
	#可省略參數:
329
	#可省略參數:
330
	#$conf["wait"],字串,是否需要等待所有程序結束,預設為"true"要等待;反之為"false"不要等待.
330
	#$conf["wait"],字串,是否需要等待所有程序結束,預設為"true"要等待;反之為"false"不要等待.
331
	#$conf["wait"]="false";
331
	#$conf["wait"]="false";
-
 
332
	#$conf["timeout"],字串陣列,每個元素代表單一程序執行的最大等待秒數,超過後將會強迫停止執行,僅當wait參數為"true"時生效.
-
 
333
	#$conf["timeout"]=array("10");
332
	#$conf["workingDir"],字串陣列,個別程式執行時的家目錄,預設不指定.
334
	#$conf["workingDir"],字串陣列,個別程式執行時的家目錄,預設不指定.
333
	#$conf["workingDir"]=array("path");
335
	#$conf["workingDir"]=array("path");
334
	#$conf["envs"],2維字串陣列,每個元素代表個別程式執行時的指定環境變數,key變數名稱;value為變數內容.預設為array("QBPWCF" => "Quick Build PHP Website Componment base on Fedora Linux");
336
	#$conf["envs"],2維字串陣列,每個元素代表個別程式執行時的指定環境變數,key變數名稱;value為變數內容.預設為array("QBPWCF" => "Quick Build PHP Website Componment base on Fedora Linux");
335
	#$conf["envs"]=array(array("key"=>"value"));
337
	#$conf["envs"]=array(array("key"=>"value"));
336
	#$conf["executeBy"],字串陣列,每個元素代表個別指令要用什麼程式執行,預設為"bash".
338
	#$conf["executeBy"],字串陣列,每個元素代表個別指令要用什麼程式執行,預設為"bash".
337
	#$conf["executeBy"]=array("bash");
339
	#$conf["executeBy"]=array("bash");
338
	#參考資料:
340
	#參考資料:
339
	#https://www.php.net/manual/en/function.proc-open.php
341
	#https://www.php.net/manual/en/function.proc-open.php
340
	#https://www.php.net/manual/en/function.proc-get-status.php
342
	#https://www.php.net/manual/en/function.proc-get-status.php
-
 
343
	#https://www.php.net/manual/en/function.proc-terminate.php
341
	#備註:
344
	#備註:
342
	#若需要取得當下的執行狀況,請使用 self::proc_update 來更新.
345
	#若需要取得當下的執行狀況,請使用 self::proc_update 來更新.
343
	*/
346
	*/
344
	public static function proc(&$conf){
347
	public static function proc(&$conf){
345
	
348
	
Line 405... Line 408...
405
		#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.
408
		#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.
406
		#$conf["canNotBeEmpty"]=array();
409
		#$conf["canNotBeEmpty"]=array();
407
		#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.
410
		#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.
408
		#$conf["canBeEmpty"]=array();
411
		#$conf["canBeEmpty"]=array();
409
		#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
412
		#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
410
		$conf["variableCheck::checkArguments"]["skipableVariableCanNotBeEmpty"]=array("wait","workingDir","envs","executeBy");
413
		$conf["variableCheck::checkArguments"]["skipableVariableCanNotBeEmpty"]=array("wait","workingDir","envs","executeBy","timeout");
411
		#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
414
		#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
412
		$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("wait","workingDir","envs","executeBy");
415
		$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("wait","workingDir","envs","executeBy","timeout");
413
		#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
416
		#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
414
		$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string","array","array","array");
417
		$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string","array","array","array","array");
415
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
418
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
416
		$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array("true",null,null,null);
419
		$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array("true",null,null,null,null);
417
		#$conf["disallowAllSkipableVarIsEmpty"],字串,是否允許每個可省略參數都為空字串,預設為"true"允許,反之為"false".
420
		#$conf["disallowAllSkipableVarIsEmpty"],字串,是否允許每個可省略參數都為空字串,預設為"true"允許,反之為"false".
418
		#$conf["disallowAllSkipableVarIsEmpty"]="";
421
		#$conf["disallowAllSkipableVarIsEmpty"]="";
419
		#$conf["disallowAllSkipableVarIsEmptyArray"],字串,是否允許每個可省略參數都為空陣列,預設為"true"允許,反之為"false".
422
		#$conf["disallowAllSkipableVarIsEmptyArray"],字串,是否允許每個可省略參數都為空陣列,預設為"true"允許,反之為"false".
420
		#$conf["disallowAllSkipableVarIsEmptyArray"]="";
423
		#$conf["disallowAllSkipableVarIsEmptyArray"]="";
421
		#$conf["disallowAllSkipableVarNotExist"],字串,是否不允許每個可省略參數都不存在,預設為"false"代表允許,反之為"true".
424
		#$conf["disallowAllSkipableVarNotExist"],字串,是否不允許每個可省略參數都不存在,預設為"false"代表允許,反之為"true".
Line 454... Line 457...
454
			
457
			
455
			#回傳結果
458
			#回傳結果
456
			return $result;
459
			return $result;
457
			
460
			
458
			}#if end
461
			}#if end
459
	
462
		
460
		#初始化 descriptorspec
463
		#初始化 descriptorspec
461
		$descriptorspec = array();
464
		$descriptorspec = array();
462
		
465
		
463
		#stdin is a pipe that the child will read from
466
		#stdin is a pipe that the child will read from
464
		$descriptorspec[]=array("pipe", "r");
467
		$descriptorspec[]=array("pipe", "r");
Line 466... Line 469...
466
		#stdout is a pipe that the child will write to
469
		#stdout is a pipe that the child will write to
467
		$descriptorspec[]=array("pipe", "w");
470
		$descriptorspec[]=array("pipe", "w");
468
		
471
		
469
		#stderr is a pipe that the child will write to
472
		#stderr is a pipe that the child will write to
470
		$descriptorspec[]=array("pipe", "w");
473
		$descriptorspec[]=array("pipe", "w");
471
				
474
		
472
		#The initial working dir for the command. This must be an absolute directory path, or null if you want to use the default value (the working dir of the current PHP process) 
475
		#The initial working dir for the command. This must be an absolute directory path, or null if you want to use the default value (the working dir of the current PHP process) 
473
		$defaultCWD = null;
476
		$defaultCWD = null;
474
		
477
		
475
		#初始化儲存每個指令的working dir.
478
		#初始化儲存每個指令的working dir.
476
		$cwds=array();
479
		$cwds=array();
Line 656... Line 659...
656
			$stderr=$process[$index]["pipes"][2];
659
			$stderr=$process[$index]["pipes"][2];
657
		
660
		
658
			#如果需要等待程序執行完
661
			#如果需要等待程序執行完
659
			if($conf["wait"]==="true"){
662
			if($conf["wait"]==="true"){
660
			
663
			
661
				#取得標準輸出
664
				#如果有設置  timeout
-
 
665
				if(isset($conf["timeout"])){
-
 
666
				
-
 
667
					#如果有設置該程序的 timeout
662
				$stdout=stream_get_contents($process[$index]["pipes"][1]);
668
					if(isset($conf["timeout"][$index])){
-
 
669
						
-
 
670
						#避免 timeout 仍然會等待程序結束 - start
663
			
671
					
-
 
672
						#不讓標準輸出會阻擋程式執行
-
 
673
						stream_set_blocking($stdout,false);
-
 
674
						
664
				#取得錯誤輸出
675
						#不讓錯誤輸出會阻擋程式執行
665
				$stderr=stream_get_contents($process[$index]["pipes"][2]);
676
						stream_set_blocking($stderr,false);
-
 
677
						
-
 
678
						#避免 timeout 仍然會等待程序結束 - end
-
 
679
						
-
 
680
						}#if end
-
 
681
					
-
 
682
					}#if end
666
			
683
				
667
				}#if end
684
				}#if end
668
			
685
			
669
			#反之
686
			#反之
670
			else{
687
			else{
671
			
688
			
Line 698... Line 715...
698
				}#if end
715
				}#if end
699
			
716
			
700
			#反之未執行完畢,且需要等待程序執行完
717
			#反之未執行完畢,且需要等待程序執行完
701
			else if($conf["wait"]==="true"){
718
			else if($conf["wait"]==="true"){
702
			
719
			
-
 
720
				#預設已經執行0秒
-
 
721
				$runtime=0;
-
 
722
			
703
				#無窮迴圈
723
				#無窮迴圈
704
				while(true){
724
				while(true){
705
				
725
				
-
 
726
					#debug
-
 
727
					#var_dump(__LINE__,$runtime,$conf["timeout"]);
-
 
728
				
-
 
729
					#如果有設置  timeout
-
 
730
					if(isset($conf["timeout"])){
-
 
731
					
-
 
732
						#如果有設置該程序的 timeout
-
 
733
						if(isset($conf["timeout"][$index])){
-
 
734
						
-
 
735
							#如果執行秒數逾時了
-
 
736
							if( $runtime > (int)($conf["timeout"][$index]) ){
-
 
737
							
-
 
738
								#debug
-
 
739
								#var_dump(__LINE__,"timeout!");
-
 
740
							
-
 
741
								#關閉標準輸出的pipe
-
 
742
								fclose($stdout);
-
 
743
								
-
 
744
								#關閉標準錯誤的pipe
-
 
745
								fclose($stderr);
-
 
746
							
-
 
747
								#強制結束程序
-
 
748
								proc_terminate($resource);
-
 
749
							
-
 
750
								#結束proc.
-
 
751
								proc_close($resource);
-
 
752
								
-
 
753
								#設置執行異常
-
 
754
								$result["status"]="false";
-
 
755
								
-
 
756
								#設置error
-
 
757
								$result["error"][]="timeout(".$conf["timeout"][$index].")";
-
 
758
							
-
 
759
								#回傳結果
-
 
760
								return $result;
-
 
761
							
-
 
762
								}#if end
-
 
763
						
-
 
764
							}#if end
-
 
765
					
-
 
766
						}#if end
-
 
767
				
706
					#取得 process 的狀態,包含 pid 等資訊,若程式已經結束,則會自動將pipes關閉,再proc_close.
768
					#取得 process 的狀態,包含 pid 等資訊,若程式已經結束,則會自動將pipes關閉,再proc_close.
707
					$cmdRes["proc_get_status"]=proc_get_status($resource);
769
					$cmdRes["proc_get_status"]=proc_get_status($resource);
708
					
770
					
709
					#如果執行已經結束,有回傳exitcode
771
					#如果執行已經結束,有回傳exitcode
710
					if($cmdRes["proc_get_status"]["exitcode"]!==-1){
772
					if($cmdRes["proc_get_status"]["exitcode"]!==-1){
Line 717... Line 779...
717
					
779
					
718
						}#if end
780
						}#if end
719
						
781
						
720
					#等待1秒鐘
782
					#等待1秒鐘
721
					sleep(1);
783
					sleep(1);
-
 
784
					
-
 
785
					#執行秒數+1
-
 
786
					$runtime++;
722
				
787
				
723
					}#while end
788
					}#while end
724
					
789
					
725
				}#if end
790
				}#if end
726
			
791