/**
  * Register a ProcessDetails as free
  *
  * @param ProcessDetails $processDetails
  * @throws \InvalidArgumentException
  * @return ProcessDetailsCollection
  */
 public function registerFreeProcess(ProcessDetails $processDetails)
 {
     $pid = $processDetails->getPid();
     if ($this->hasProcess($pid) === FALSE) {
         throw new \InvalidArgumentException(sprintf('Could not register free process. Process (%d) not in list.', $processDetails->getPid()), 1400761296);
     }
     $this->freeProcessIds[$pid] = $pid;
     return $this;
 }
示例#2
0
 /**
  * Run the worker process
  * @param \QXS\WorkerPool\WorkerInterface $worker the worker, that runs the tasks
  * @param \QXS\WorkerPool\SimpleSocket $simpleSocket the simpleSocket, that is used for the communication
  * @param int $i the number of the child
  */
 protected function runWorkerProcess(WorkerInterface $worker, SimpleSocket $simpleSocket, $i)
 {
     $replacements = array('basename' => basename($_SERVER['PHP_SELF']), 'fullname' => $_SERVER['PHP_SELF'], 'class' => get_class($worker), 'i' => $i, 'state' => 'free');
     ProcessDetails::setProcessTitle($this->childProcessTitleFormat, $replacements);
     $this->worker->onProcessCreate($this->semaphore);
     while (TRUE) {
         $output = array('pid' => getmypid());
         try {
             $replacements['state'] = 'free';
             ProcessDetails::setProcessTitle($this->childProcessTitleFormat, $replacements);
             $cmd = $simpleSocket->receive();
             // invalid response from parent?
             if (!isset($cmd['cmd'])) {
                 break;
             }
             $replacements['state'] = 'busy';
             ProcessDetails::setProcessTitle($this->childProcessTitleFormat, $replacements);
             if ($cmd['cmd'] == 'run') {
                 try {
                     $output['data'] = $this->worker->run($cmd['data']);
                 } catch (\Exception $e) {
                     $output['workerException'] = array('class' => get_class($e), 'message' => $e->getMessage(), 'trace' => $e->getTraceAsString());
                 }
                 // send back the output
                 $simpleSocket->send($output);
             } elseif ($cmd['cmd'] == 'exit') {
                 break;
             }
         } catch (SimpleSocketException $e) {
             break;
         } catch (\Exception $e) {
             // send Back the exception
             $output['poolException'] = array('class' => get_class($e), 'message' => $e->getMessage(), 'trace' => $e->getTraceAsString());
             $simpleSocket->send($output);
         }
     }
     $this->worker->onProcessDestroy();
     $this->exitPhp(0);
 }