public function run($daemon = false) { if ($daemon) { ProcessUtil::daemon(); } $rand = mt_rand(); $title = $this->title_; $ipcs = array(sprintf('ipc:///%s/ipc%d_%s_0.sock', $this->sock_dir_, $rand, $this->title_), sprintf('ipc:///%s/ipc%d_%s_1.sock', $this->sock_dir_, $rand, $this->title_)); ProcessUtil::setTitle(sprintf('%s<controller>', $title)); PeriodicGc::enable(300); $controller = new Controller(); $controller->run(array('groups' => array(array('proxy' => array('routine' => function () use($ipcs, $title) { ProcessUtil::setTitle(sprintf('%s<proxy>', $title)); $proxy = new Proxy($this->listen_addr_, $ipcs); $proxy->run(); }), 'worker' => array('routine' => function () use($ipcs, $title) { ProcessUtil::setTitle(sprintf('%s<worker>', $title)); if (isset($this->init_callback_)) { if (false === call_user_func($this->init_callback_)) { \ServerBench\syslog_error('init_callback returns false.'); return; } } $worker = new Worker($ipcs, $this->message_callback_); $worker->run(); }, 'num' => $this->process_num_ < 1 ? 1 : $this->process_num_)))), $this->dir_); }
} $app_path = realpath($app_path); if (!file_exists($app_path)) { \ServerBench\Cli\printf_and_exit("app(%s) does not exist.\n", $app_path); } $ipcs = $arguments->get('ipcs'); if (!isset($ipcs)) { $rand = mt_rand(); $ipcs = sprintf('ipc://%s/ipc%d_%s_0.sock,ipc://%s/ipc%d_%s_1.sock', $dir, $rand, $title, $dir, $rand, $title); } \ServerBench\syslog_info('app %s', [$app_path]); $workers = $arguments->get('workers', 1); \ServerBench\syslog_info('start workers %d', [$workers]); if (isset($arguments['daemonize'])) { \ServerBench\syslog_info('run as daemon.'); ProcessUtil::daemon(); } $pid = getmypid(); \ServerBench\syslog_info('pid of controller is %d', [$pid]); $pidfile = $arguments->get('pidfile'); if (isset($pidfile)) { $realpath_pidfile = realpath($pidfile); if (file_exists($realpath_pidfile) && file_get_contents($pidfile) != '') { \ServerBench\syslog_error('there is a running instance already.'); exit; } file_put_contents($pidfile, $pid); register_shutdown_function(function () use($pidfile) { @unlink($pidfile); }); }