/** * 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); }