/** * @param mixed $parameter * @throws \InvalidArgumentException */ public function __construct($parameter = null) { if (is_null($parameter)) { $this->config = new Config(); } elseif (is_int($parameter) && $parameter >= 1) { $this->config = new Config(array('concurrency' => $parameter)); } elseif (is_array($parameter)) { $this->config = new Config($parameter); } else { throw new \InvalidArgumentException(); } $this->ownerPid = getmypid(); $this->log = new Log($this->ownerPid); $this->pcntl = new Pcntl(); $this->container = new Container($this->ownerPid, $this->log, $this->config); $log = $this->log; $self = $this; foreach ($this->signals as $sig) { $this->pcntl->signal($sig, function ($sig) use($log, $self) { $log->info('received signal. signo: ' . $sig); $self->setReceivedSignal($sig); $log->info('--> sending a signal " to children.'); $self->container->sendSignalToMaster($sig); $log->info('<-- signal handling has been completed successfully.'); exit; }, false); } $this->log->info('parent pid: ' . $this->ownerPid); }
/** * fork worker process * * @param \Ackintosh\Snidel\Task * @return \Ackintosh\Snidel\Worker * @throws \RuntimeException */ private function forkWorker($task) { try { $fork = $this->fork(); } catch (\RuntimeException $e) { $this->log->error($e->getMessage()); throw $e; } $worker = new Worker($fork, $task); if (getmypid() === $this->masterPid) { // master $this->log->info('forked worker. pid: ' . $worker->getPid()); return $worker; } else { // worker // @codeCoverageIgnoreStart $this->log->info('has forked. pid: ' . getmypid()); foreach ($this->signals as $sig) { $this->pcntl->signal($sig, SIG_DFL, true); } $worker->setResultQueue($this->queueFactory->createResultQueue()); $resultHasQueued = false; register_shutdown_function(function () use(&$resultHasQueued, $worker) { if (!$resultHasQueued) { $worker->error(); } }); $this->log->info('----> started the function.'); try { $worker->run(); } catch (\RuntimeException $e) { $this->log->error($e->getMessage()); exit; } $this->log->info('<---- completed the function.'); $resultHasQueued = true; $this->log->info('queued the result and exit.'); exit; // @codeCoverageIgnoreEnd } }