public static function dispatch()
 {
     Kwf_Loader::registerAutoload();
     if (empty($_REQUEST['progressNum'])) {
         throw new Kwf_Exception('progressNum required');
     }
     $pbarAdapter = new Kwf_Util_ProgressBar_Adapter_Cache($_REQUEST['progressNum']);
     $pbarStatus = $pbarAdapter->getStatus();
     if (!$pbarStatus) {
         $pbarStatus = array();
     }
     $pbarStatus['success'] = true;
     if (isset($_REQUEST['outputFile']) && isset($_REQUEST['pid'])) {
         $processes = Kwf_Util_Process::getRunningProcesses();
         if (isset($processes[$_REQUEST['pid']])) {
             $pbarStatus['bgFinished'] = false;
         } else {
             $pbarStatus['bgFinished'] = true;
             if (!preg_match('#^bgproc[a-z0-9]+$#i', $_REQUEST['outputFile'])) {
                 throw new Kwf_Exception_AccessDenied();
             }
             $output = file_get_contents('./temp/' . $_REQUEST['outputFile']);
             $outputErr = file_get_contents('./temp/' . $_REQUEST['outputFile'] . '.err');
             $outputJson = json_decode($output);
             if (!$outputJson) {
                 //assign as string
                 $pbarStatus['bgError'] = $outputErr;
             } else {
                 $pbarStatus['bgResponse'] = $outputJson;
                 $pbarStatus['bgError'] = file_get_contents('./temp/' . $_REQUEST['outputFile'] . '.err');
                 $pbarStatus['bgError'] = preg_replace('#^(PHP )?Deprecated: .*$#m', '', $pbarStatus['bgError']);
                 //ignore errors from deprecated php.ini settings
                 $pbarStatus['bgError'] = trim($pbarStatus['bgError']);
             }
         }
     }
     echo Zend_Json::encode($pbarStatus);
     exit;
 }
 public function _stop($onlyCommandKey = null)
 {
     $killed = array();
     $processes = Kwf_Util_Process::getRunningWebProcesses();
     foreach ($this->_commands as $commandKey => $requiredCmd) {
         if ($onlyCommandKey) {
             if ($onlyCommandKey != $commandKey) {
                 continue;
             }
         }
         foreach ($processes as $p) {
             if ($p['cmd'] == $requiredCmd['cmd']) {
                 if (isset($requiredCmd['shutdownFunction'])) {
                     if (!$this->_getParam('silent')) {
                         echo "calling {$requiredCmd['shutdownFunction']} to shutdown {$p['pid']}\n";
                     }
                     $fn = explode('::', $requiredCmd['shutdownFunction']);
                     call_user_func($fn);
                     $killed[] = $p['pid'];
                 } else {
                     if (!$this->_getParam('silent')) {
                         echo "kill {$p['pid']} {$p['cmd']} {$p['args']}\n";
                     }
                     system("kill {$p['pid']}");
                     $killed[] = $p['pid'];
                     foreach ($p['childPIds'] as $pid) {
                         if (!$this->_getParam('silent')) {
                             echo "    kill child process {$pid}\n";
                         }
                         system("kill {$pid}");
                         $killed[] = $pid;
                     }
                 }
             }
         }
     }
     $start = time();
     while (true) {
         $pids = array();
         exec('ps ax -o pid', $pids);
         foreach ($pids as &$i) {
             $i = (int) $i;
         }
         $allDone = true;
         foreach ($killed as $pid) {
             if (in_array($pid, $pids)) {
                 $allDone = false;
                 break;
             }
         }
         if ($allDone) {
             break;
         }
         if (time() - $start > 10) {
             echo "timeout while waiting for: ";
             foreach ($killed as $pid) {
                 if (in_array($pid, $pids)) {
                     echo $pid . " ";
                 }
             }
             echo "\n";
             break;
         }
         usleep(100 * 1000);
     }
 }