複製程式碼如下:
class Controller_Jobs 擴充了Controller_Base{
public function before(){
parent;
if(Request::$protocol != "cli"){
die("只允許cli!n");
}
}
公用函數after(){
parent::after();
公用函數after(){
parent::after();
公用函數after(){
parent::after();
公用程式碼
//做
//做
//做
//做
//做
/做一些清理任務
}
private function _execJobCommand($joburi,$paras){
$php_exec = Kohana::config("picsou.php_exec");
$php_index = APPINDEX;picsou.php_exec");
$php_index = APPINDEX;picsou.php_exec");
$php_index = APPINDEX;picsou.php_exec");
$php_index = APPINDEX;; [] = $php_index;
$command_args[] = "--uri=".$joburi;
foreach ($paras as $para => $value){
$command_args[] = "--".$para. "=".$value;
}
//var_dump($command_args);退出;
echo "執行指令:".$php_exec."n";
pcntl_exec($php_exec,$command_args);
* 在佇列中執行作業
*/
public function action_run(){
$requestCount = 0;
while(true){
$sql = "從job_queue 中選擇*,其中status='1' 且已核准'1' 依id 排序";
$jobs = DB::query(Database::SELECT,$sql)->execute()->as_array();
if($jobs){
foreach ($jobs as $ job){
$requestCount ++;
//將作業狀態更新為正在執行
DB::update('job_queue')->set(array('status'=>'2'))
->where( 'id','=' ,$job['id'])->execute();
$job_pid = pcntl_fork();
if($job_pid == -1){
die("無法分叉子進程" );
} else if($job_pid == 0 ){
$this->_execJobCommand($job['job_uri'],json_decode($job['paras'],true));
退出(0);
//在這裡運行作業
} else{
echo "等待jobn";
ob_flush();
$child_pid = pcntl_waitpid($job_pid, 00b_pidD,$job. ".$status."n";
if($status == 0){
//作業完成
DB::update('job_queue')->set(array('status'=>'999'))
->where(' id','=',$job['id'])->execute();
echo「孩子完成了」;
ob_flush();
}else{
DB::update( 'job_queue')->set(array('status'=>'-1'))
->where('id','=',$job[ 'id'])->execute();
echo 「孩子失敗了」;
ob_flush();
}
}
}
}
else{
if($requestCount >=10){
echo " ;
}
//沒有要運作的作業
//echo "No jobn";