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_); }
} $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); }); } ProcessUtil::setTitle($arguments->get('title', 'serverbench<controller>')); try { $api = new Api('controller', include $app_path); if (false === $api->init()) { \ServerBench\syslog_error('app->init(\'controller\') returns false.'); exit; } PeriodicGc::enable(300); $controller = new Controller(); $controller->run(['groups' => [['proxy' => ['routine' => [$phpbin, ['-r', sprintf('require(\'%s\');', __DIR__ . '/start_proxy.php'), '--', '--title', sprintf('%s<proxy>', $title), '--listen', $listen_addr, '--app', $app_path, '--ipcs', $ipcs]]], 'worker' => ['routine' => [$phpbin, ['-r', sprintf('require(\'%s\');', __DIR__ . '/start_worker.php'), '--', '--title', sprintf('%s<worker>', $title), '--app', $app_path, '--ipcs', $ipcs]], 'num' => $workers]]]], $dir); if (false === $api->fini()) { \ServerBench\syslog_error('app->fini(\'controller\') returns false.'); } } catch (Exception $e) { \ServerBench\syslog_error('uncaught exception from controller: %s', [$e]); }