php获取后台Job管理的实现代码
程序员文章站
2022-06-08 22:18:28
复制代码 代码如下:
<?php defined('syspath') or die('no direct access allowed.');
class controller_jobs extends controller_base{
public function before(){
parent::before();
if(request::$protocol != "cli"){
die("only cli allowed!\n");
}
}
public function after(){
parent::after();
//do some cleaning tasks
}
private function _execjobcommand($joburi,$paras){
$php_exec = kohana::config("picsou.php_exec");
$php_index = appindex;
$command_args = array();
$command_args[] = $php_index;
$command_args[] = "--uri=".$joburi;
foreach ($paras as $para => $value){
$command_args[] = "--".$para."=".$value;
}
//var_dump($command_args);exit;
echo "exec commmand:".$php_exec."\n";
pcntl_exec($php_exec,$command_args);
}
/*
* running jobs in queues
*/
public function action_run(){
$requestcount = 0;
while(true){
$sql = "select * from job_queue where status='1' and approved='1' order by id";
$jobs = db::query(database::select,$sql)->execute()->as_array();
if($jobs){
foreach ($jobs as $job){
$requestcount ++;
//update the jobs status as running
db::update('job_queue')->set(array('status'=>'2'))
->where('id','=',$job['id'])->execute();
$job_pid = pcntl_fork();
if($job_pid == -1){
die("could not fork child");
} else if($job_pid == 0 ){
$this->_execjobcommand($job['job_uri'],json_decode($job['paras'],true));
echo "finish child\n";
exit(0);
//run jobs here
} else{
echo "waiting for job\n";
ob_flush();
$child_pid = pcntl_waitpid($job_pid,$status, wuntraced);
echo "waitpid end:".$status."\n";
if($status == 0){
//job completed
db::update('job_queue')->set(array('status'=>'999'))
->where('id','=',$job['id'])->execute();
echo "child finished\n";
ob_flush();
}else{
db::update('job_queue')->set(array('status'=>'-1'))
->where('id','=',$job['id'])->execute();
echo "child failed\n";
ob_flush();
}
}
}
}
else{
if($requestcount >=10){
echo "have a rest, i have processed 10 jobs\n";
exit;
}
//no job to run
//echo "no job\n";
ob_flush();
sleep(5);
}
}
}
}
复制代码 代码如下:
<?php defined('syspath') or die('no direct access allowed.');
class controller_jobs extends controller_base{
public function before(){
parent::before();
if(request::$protocol != "cli"){
die("only cli allowed!\n");
}
}
public function after(){
parent::after();
//do some cleaning tasks
}
private function _execjobcommand($joburi,$paras){
$php_exec = kohana::config("picsou.php_exec");
$php_index = appindex;
$command_args = array();
$command_args[] = $php_index;
$command_args[] = "--uri=".$joburi;
foreach ($paras as $para => $value){
$command_args[] = "--".$para."=".$value;
}
//var_dump($command_args);exit;
echo "exec commmand:".$php_exec."\n";
pcntl_exec($php_exec,$command_args);
}
/*
* running jobs in queues
*/
public function action_run(){
$requestcount = 0;
while(true){
$sql = "select * from job_queue where status='1' and approved='1' order by id";
$jobs = db::query(database::select,$sql)->execute()->as_array();
if($jobs){
foreach ($jobs as $job){
$requestcount ++;
//update the jobs status as running
db::update('job_queue')->set(array('status'=>'2'))
->where('id','=',$job['id'])->execute();
$job_pid = pcntl_fork();
if($job_pid == -1){
die("could not fork child");
} else if($job_pid == 0 ){
$this->_execjobcommand($job['job_uri'],json_decode($job['paras'],true));
echo "finish child\n";
exit(0);
//run jobs here
} else{
echo "waiting for job\n";
ob_flush();
$child_pid = pcntl_waitpid($job_pid,$status, wuntraced);
echo "waitpid end:".$status."\n";
if($status == 0){
//job completed
db::update('job_queue')->set(array('status'=>'999'))
->where('id','=',$job['id'])->execute();
echo "child finished\n";
ob_flush();
}else{
db::update('job_queue')->set(array('status'=>'-1'))
->where('id','=',$job['id'])->execute();
echo "child failed\n";
ob_flush();
}
}
}
}
else{
if($requestcount >=10){
echo "have a rest, i have processed 10 jobs\n";
exit;
}
//no job to run
//echo "no job\n";
ob_flush();
sleep(5);
}
}
}
}
上一篇: 详谈ARM架构与ARM内核发展史
下一篇: C#快速创建自定义样式的窗体