RPCController.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: liyunlong
  5. * Date: 2018-12-22
  6. * Time: 16:45
  7. */
  8. namespace common\libs\swoole;
  9. use yii\base\Exception;
  10. use yii\console\Controller;
  11. use yii\helpers\ArrayHelper;
  12. class RPCController extends Controller
  13. {
  14. /**
  15. * 存储配置中的所有配置项
  16. * @var array
  17. */
  18. public $settings = [];
  19. /**
  20. * 默认controller
  21. * @var string
  22. */
  23. public $defaultAction = 'help';
  24. /**
  25. * 强制执行的选项
  26. * @var
  27. */
  28. public $force;
  29. /**
  30. * 接收force选项
  31. * @param string $actionID
  32. * @return array|string[]
  33. */
  34. public function options($actionID)
  35. {
  36. return array_merge(parent::options($actionID), [
  37. 'force'
  38. ]);
  39. }
  40. /**
  41. * 设置别名
  42. * @return array
  43. */
  44. public function optionAliases()
  45. {
  46. return array_merge(parent::optionAliases(), [
  47. 'f' => 'force',
  48. ]);
  49. }
  50. /**
  51. * 初始化
  52. * @throws Exception
  53. */
  54. public function init() {
  55. parent::init();
  56. $this->prepareSettings();
  57. }
  58. /**
  59. * 初始化配置信息
  60. * @throws Exception
  61. */
  62. protected function prepareSettings()
  63. {
  64. $runtimePath = \Yii::$app->getRuntimePath();
  65. $this->settings = [
  66. 'host' => '127.0.0.1',
  67. 'port' => '9515',
  68. 'process_name' => 'swooleRPCServer',
  69. 'open_tcp_nodelay' => '1',
  70. 'daemonize' => '1',
  71. 'worker_num' => '2',
  72. 'task_worker_num' => '2',
  73. 'task_max_request' => '10000',
  74. 'debug' => true,
  75. 'pidfile' => $runtimePath.'/tmp/yii2-swoole-rpc.pid',
  76. 'log_dir' => $runtimePath.'/yii2-swoole-rpc/log',
  77. 'task_tmpdir' => $runtimePath.'/yii2-swoole-rpc/task',
  78. 'log_file' => $runtimePath.'/yii2-swoole-rpc/log/rpc.log',
  79. 'log_size' => 204800000,
  80. 'ipWhiteList' => [],
  81. 'username' => '',
  82. 'password' => '',
  83. ];
  84. try {
  85. $settings = \Yii::$app->params['swooleRPC'];
  86. }catch (\Exception $e) {
  87. throw new Exception('Empty param swooleRPC in params. ',8);
  88. }
  89. $this->settings = ArrayHelper::merge(
  90. $this->settings,
  91. $settings
  92. );
  93. }
  94. /**
  95. * 启动服务 参数:start 启动,stop 停止,restart 重启,stats 查看状态,list 查看进程列表
  96. * @param string $mode
  97. * @throws Exception
  98. */
  99. public function actionRun($mode='start'){
  100. $rpcService = new RPCService($this->settings,\Yii::$app, $this);
  101. switch ($mode) {
  102. case 'start':
  103. $rpcService->serviceStart();
  104. break;
  105. case 'restart':
  106. $rpcService->serviceStop(!!$this->force, function() use ($rpcService){
  107. $rpcService->serviceStart();
  108. });
  109. break;
  110. case 'stop':
  111. $rpcService->serviceStop(!!$this->force);
  112. break;
  113. case 'stats':
  114. $rpcService->serviceStats();
  115. break;
  116. case 'list':
  117. $rpcService->serviceList();
  118. break;
  119. default:
  120. exit('error:参数错误');
  121. break;
  122. }
  123. }
  124. }