YourPaste - For your paste! Archive - Tools - Login

Posted by unknown on Wed 27 Aug 2008 17:58 59 views - Syntax: PHP - Expires: never - Report - IMG - Download -

  1. <?php
  2.  
  3. /**
  4.  * NOC cronjob
  5.  *
  6.  * @package NOC
  7.  * @version 1.0
  8.  * @author Martijn Bogaard
  9.  * @copyright 2008 SiteWK
  10.  */
  11.  
  12. require_once('../header.php');
  13.  
  14. define('__LIB_OPTIONS', __LIB_SKIP_TPL | __LIB_SKIP_ERROR_HANDLER | __LIB_SKIP_LOAD_MODULES);
  15.  
  16. require_once('../common.php');
  17.  
  18. declare(ticks = 1);
  19.  
  20. $cChilds = 0;
  21. $aChilds = array();
  22. function signal_handler($signal) {
  23.         global $cChilds, $aChilds;
  24.        
  25.         if ($signal == SIGUSR1) {
  26.                 //oelala ik ben een child die dood moet :D
  27.                 die('CronManager: child exited!'.PHP_EOL);
  28.         }
  29.         else if ($signal == SIGALRM) {
  30.                 echo 'CronManager: Send exit signal to childs...'.PHP_EOL;
  31.                
  32.                 foreach ($aChilds as $childPid) {
  33.                         posix_kill($childPid, SIGUSR1);
  34.                 }
  35.                
  36.                 sleep(1);
  37.                
  38.                 die ('Caught SIGALRM: cron terminated!'.PHP_EOL);
  39.         }
  40. }
  41.  
  42. function create_fork() {
  43.         global $cChilds, $aChilds;
  44.  
  45.         $pid = pcntl_fork();
  46.  
  47.         if ($pid == -1) {
  48.                 die('Unknown FORK ERROR!'.PHP_EOL);
  49.         } else if ($pid) {//parent
  50.                 $aChilds[$cChilds] = $pid;
  51.                 $cChilds++;
  52.         } else {//child
  53.                 run_fork_child();
  54.         }
  55. }
  56.  
  57. function wait_for_childs() {
  58.         global $cChilds, $aChilds;
  59.        
  60.         $status = null;
  61.         while ($cChilds > 0) {
  62.                 sleep(1);
  63.                
  64.                 foreach ($aChilds as $childPid) {
  65.                         var_dump(pcntl_waitpid($childPid, $status, WNOHANG));
  66.                        
  67.                         if (pcntl_wifexited($status) || pcntl_wifsignaled($status)) {
  68.                                 echo 'CronManager: child ('.$childPid.') exited.'.PHP_EOL;
  69.                                
  70.                                 unset($aChilds[$childPid]);
  71.                                 $cChilds--;
  72.                         }
  73.                 }
  74.         }
  75. }
  76.  
  77. function run_fork_child() {
  78.         pcntl_signal(SIGUSR1, "signal_handler", true); // waiting for signals of the parent process
  79.        
  80.         sleep(rand(1,25));
  81.         exit;
  82. }
  83.  
  84. pcntl_signal(SIGALRM, "signal_handler", true);
  85. pcntl_alarm(30);
  86.  
  87. for ($i = 0; $i < 5; $i++) {
  88.         create_fork();
  89. }
  90.  
  91. wait_for_childs();
  92.  
  93. echo 'CronManager: All clients exited normally.'.PHP_EOL;

Comments


Name:
Comment:

© 2008 YourPaste.net - Disclaimer