| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- <?php
- /**
- * Created by PhpStorm.
- * User: liyunlong
- * Date: 2018-12-22
- * Time: 16:45
- */
- namespace common\libs\swoole;
- use yii\base\Exception;
- use yii\console\Controller;
- use yii\helpers\ArrayHelper;
- class RPCController extends Controller
- {
- /**
- * 存储配置中的所有配置项
- * @var array
- */
- public $settings = [];
- /**
- * 默认controller
- * @var string
- */
- public $defaultAction = 'help';
- /**
- * 强制执行的选项
- * @var
- */
- public $force;
- /**
- * 接收force选项
- * @param string $actionID
- * @return array|string[]
- */
- public function options($actionID)
- {
- return array_merge(parent::options($actionID), [
- 'force'
- ]);
- }
- /**
- * 设置别名
- * @return array
- */
- public function optionAliases()
- {
- return array_merge(parent::optionAliases(), [
- 'f' => 'force',
- ]);
- }
- /**
- * 初始化
- * @throws Exception
- */
- public function init() {
- parent::init();
- $this->prepareSettings();
- }
- /**
- * 初始化配置信息
- * @throws Exception
- */
- protected function prepareSettings()
- {
- $runtimePath = \Yii::$app->getRuntimePath();
- $this->settings = [
- 'host' => '127.0.0.1',
- 'port' => '9515',
- 'process_name' => 'swooleRPCServer',
- 'open_tcp_nodelay' => '1',
- 'daemonize' => '1',
- 'worker_num' => '2',
- 'task_worker_num' => '2',
- 'task_max_request' => '10000',
- 'debug' => true,
- 'pidfile' => $runtimePath.'/tmp/yii2-swoole-rpc.pid',
- 'log_dir' => $runtimePath.'/yii2-swoole-rpc/log',
- 'task_tmpdir' => $runtimePath.'/yii2-swoole-rpc/task',
- 'log_file' => $runtimePath.'/yii2-swoole-rpc/log/rpc.log',
- 'log_size' => 204800000,
- 'ipWhiteList' => [],
- 'username' => '',
- 'password' => '',
- ];
- try {
- $settings = \Yii::$app->params['swooleRPC'];
- }catch (\Exception $e) {
- throw new Exception('Empty param swooleRPC in params. ',8);
- }
- $this->settings = ArrayHelper::merge(
- $this->settings,
- $settings
- );
- }
- /**
- * 启动服务 参数:start 启动,stop 停止,restart 重启,stats 查看状态,list 查看进程列表
- * @param string $mode
- * @throws Exception
- */
- public function actionRun($mode='start'){
- $rpcService = new RPCService($this->settings,\Yii::$app, $this);
- switch ($mode) {
- case 'start':
- $rpcService->serviceStart();
- break;
- case 'restart':
- $rpcService->serviceStop(!!$this->force, function() use ($rpcService){
- $rpcService->serviceStart();
- });
- break;
- case 'stop':
- $rpcService->serviceStop(!!$this->force);
- break;
- case 'stats':
- $rpcService->serviceStats();
- break;
- case 'list':
- $rpcService->serviceList();
- break;
- default:
- exit('error:参数错误');
- break;
- }
- }
- }
|