複製程式碼程式碼如下:
class Controller_Jobs extends Controller_Base{
public function before(){
parent::before();
if(Request::$protocol != "cli"){
die("僅允許cli! >private function _execJobCommand($joburi,$paras){
$php_exec = Kohana::config("picsou.php_exec");
$php_index = APPINDEX;
$command_args = array();
$command_args = array();
$command_args = array();
$command_args = array();
$command_args = array();
$command_args = array();
$command_args = array();
$command_args[] = $php_index;
$command_args[] = "--uri=".$joburi;
foreach ($paras as $para => $value){
$command_args[] = "--".$para."=".$value;
}
//var_dump($command_args);退出;
echo "exec 指令:".$php_exec."n";
pcntl_exec($php_exec,$command_args);
}
/*
* 在佇列中執行作業
*/
public function action_run(){
*/
public function action_run(){
$request = 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));
echo「完成Childn」;
退出(0);
//在這裡運行作業
} else{
echo "等待jobn";
ob_flush();
$ child_pid = pcntl_waitpid($job_pid,$status, WUNTRACED);
echo "waitpid end:".$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 "休息一下,我已經處理了10個作業n";
退出;
}
//沒有要運行的作業
//echo "No jobn";
ob_flush();
睡覺(5);
}
}
}
}
以上就介紹了jyxiaozhi.taobao.com php取得後台作業管理的實作程式碼,包括jyxiaozhi.taobao.com方面的內容,希望對PHP教學有興趣的朋友有幫助。