jyxiaozhi.taobao.com PHP Get the implementation code for background job management

Source: Internet
Author: User
Copy CodeThe code is as follows:


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 "has a rest, I have processed jobs\n";
Exit
}
No job to run
echo "No job\n";
Ob_flush ();
Sleep (5);
}
}
}
}

The above describes the jyxiaozhi.taobao.com PHP access to the background job management implementation code, including the jyxiaozhi.taobao.com aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.