'force', 'v' => 'version', ]; } /** * 初始化 * @throws Yii\base\ErrorException */ public function init() { parent::init(); $this->prepareSettings(); } /** * 默认执行的action */ public function actionDefault(){ if($this->version){ echo($this->versionNumber.PHP_EOL); } } /** * 初始化配置信息 * @throws Yii\base\ErrorException */ protected function prepareSettings() { $runtimePath = Yii::$app->getRuntimePath(); $this->settings = [ 'host' => '127.0.0.1', 'port' => '9512', 'serverHost' => '127.0.0.1', 'serverPort' => '9512', 'process_name' => 'swooleServ', 'with_timer' => true, 'timer_interval' => 30000, 'open_tcp_nodelay' => '1', 'daemonize' => '1', 'worker_num' => '2', 'task_worker_num' => '2', 'task_max_request' => '10000', 'task_enable_coroutine' => true, 'debug' => true, 'pidfile' => $runtimePath.'/tmp/yii2-swoole-async-timer.pid', 'log_dir' => $runtimePath.'/yii2-swoole-async-timer/log', 'task_tmpdir' => $runtimePath.'/yii2-swoole-async-timer/task', 'log_file' => $runtimePath.'/yii2-swoole-async-timer/log/http.log', 'log_size' => 204800000, ]; try { $settings = Yii::$app->params['swooleAsyncTimer']; }catch (yii\base\ErrorException $e) { throw new yii\base\ErrorException('Empty param swooleAsyncTimer in params. ',8); } $this->settings = yii\helpers\ArrayHelper::merge( $this->settings, $settings ); } /** * 启动服务 * @param string $mode */ public function actionRun($mode='start'){ $swooleService = new SwooleService($this->settings,Yii::$app, $this); switch ($mode) { case 'start': $swooleService->serviceStart(); break; case 'restart': $swooleService->serviceStop(!!$this->force, function() use ($swooleService){ $swooleService->serviceStart(); }); break; case 'stop': $swooleService->serviceStop(!!$this->force); break; case 'stats': $swooleService->serviceStats(); break; case 'list': $swooleService->serviceList(); break; default: exit('error:参数错误'); break; } } }