PHP Gets the job queue, creates the execution process, waits for the end _php tutorial

Source: Internet
Author: User
A back-office job management instance that obtains the job queue from the database, creates a new process for execution, and waits for the job to end.

Code snippet:

Copy to ClipboardWhat to refer to: [www.bkjia.com] 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);

}
}
}

}

http://www.bkjia.com/PHPjc/363967.html www.bkjia.com true http://www.bkjia.com/PHPjc/363967.html techarticle a back-office job management instance that obtains the job queue from the database, creates a new process for execution, and waits for the job to end. Code snippet: Copy to Clipboard reference: ...

  • 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.