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_); }
if (!isset($arguments['listen'])) { \ServerBench\Cli\printf_and_exit("argument --listen or -l should be set.\n"); } $listen_addr = explode(',', $arguments['listen']); if (!isset($arguments['ipcs'])) { \ServerBench\Cli\printf_and_exit("argument --ipcs should be set.\n"); } $ipcs = explode(',', $arguments['ipcs']); if (count($ipcs) != 2) { \ServerBench\Cli\printf_and_exit("argument --ipcs should be a couple of sock files.\n"); } $api = null; $app_path = $arguments->get('app'); ProcessUtil::setTitle($arguments->get('title', 'serverbench<proxy>')); try { if (isset($app_path) && file_exists($app_path)) { $api = new Api('proxy', include $app_path); } if ($api && false === $api->init()) { \ServerBench\syslog_error('app->init(\'proxy\') returns false.'); exit; } PeriodicGc::enable(300); $proxy = new Proxy($listen_addr, $ipcs); $proxy->run(); if ($api && false === $api->fini()) { \ServerBench\syslog_error('app->fini(\'proxy\') returns false.'); } } catch (Exception $e) { \ServerBench\syslog_error('uncaught exception from proxy: %s', [$e]); }