示例#1
0
 /**
  * @return \Generator|void
  * @throws \Aurora\Console\Exception
  */
 public function start()
 {
     if (0 < ($pid = pcntl_fork())) {
         return;
     } elseif (-1 == $pid) {
         throw new Exception("Unable to create daemon process");
     }
     posix_setsid();
     if (0 < ($pid = pcntl_fork())) {
         // 禁止进程重新打开控制终端
         exit(0);
     } elseif (-1 == $pid) {
         throw new Exception("Unable to create the process of leaving the terminal");
     }
     chdir('/');
     umask(0);
     $user = $this->config->get('master_user', 'nobody');
     $group = $this->config->get('master_user_group', '');
     Posix::setUser($user, $group);
     $filename = $this->config->get('pid_file_save_path', '/var/run/aurora.pid');
     if (!($fd = @fopen($filename, 'w+'))) {
         throw new Exception("PID file creation failed" . error_get_last()['message']);
     } elseif (!flock($fd, LOCK_EX | LOCK_NB)) {
         throw new Exception("Unable to lock the PID file");
     }
     fwrite($fd, posix_getpid());
     // $this->closeStdDescriptors();
     call_user_func(yield, $this->config);
     flock($fd, LOCK_UN);
     fclose($fd);
     unlink($filename);
     exit(0);
 }
示例#2
0
 public function __construct(Server $server, EventDispatcher $event, $socket, WorkerConfig $config = null)
 {
     if (-1 === ($this->pid = pcntl_fork())) {
         throw new Exception('Failed to create a work process');
     } elseif ($this->pid) {
         return;
     }
     try {
         $server->setType(Server::WORKER);
         $event->reset();
         $this->server = $server;
         $this->event = $event;
         $this->socket = $socket;
         $this->timestamp = $server->getTimestamp();
         $this->timestamp->mark(ServerTimestampType::WorkerStart);
         $this->config = $config ?? new WorkerConfig();
         Posix::setUser($this->config->worker_user, $this->config->worker_user_group);
         $pipeline = $this->server->getPipeline();
         $pipeline->bind('worker', $this);
         if ($pipeline instanceof EventManageable && !$pipeline->getEvent()) {
             $pipeline->setEvent($event);
         }
         $this->createClient();
     } catch (\Throwable $ex) {
         echo sprintf('"%s" in "%s:%d"', $ex->getMessage(), $ex->getFile(), $ex->getLine()), "\n";
         echo $ex->getTraceAsString();
         if (!$this->event->getBase()->gotStop()) {
             $this->event->stop();
         }
     }
 }