Rpc.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. namespace think\swoole\command;
  3. use think\console\Command;
  4. use think\console\Input;
  5. use think\console\input\Argument;
  6. use think\console\Output;
  7. use think\swoole\PidManager;
  8. use think\swoole\rpc\Manager;
  9. class Rpc extends Command
  10. {
  11. public function configure()
  12. {
  13. $this->setName('swoole:rpc')
  14. ->addArgument('action', Argument::OPTIONAL, "start|stop|restart|reload", 'start')
  15. ->setDescription('Swoole RPC Server for ThinkPHP');
  16. }
  17. protected function initialize(Input $input, Output $output)
  18. {
  19. $this->app->bind(\Swoole\Server::class, function () {
  20. return $this->createSwooleServer();
  21. });
  22. $this->app->bind(PidManager::class, function () {
  23. return new PidManager($this->app->config->get("swoole.server.options.pid_file"));
  24. });
  25. }
  26. public function handle()
  27. {
  28. $this->checkEnvironment();
  29. $action = $this->input->getArgument('action');
  30. if (in_array($action, ['start', 'stop', 'reload', 'restart'])) {
  31. $this->app->invokeMethod([$this, $action], [], true);
  32. } else {
  33. $this->output->writeln("<error>Invalid argument action:{$action}, Expected start|stop|restart|reload .</error>");
  34. }
  35. }
  36. /**
  37. * 检查环境
  38. */
  39. protected function checkEnvironment()
  40. {
  41. if (!extension_loaded('swoole')) {
  42. $this->output->error('Can\'t detect Swoole extension installed.');
  43. exit(1);
  44. }
  45. if (!version_compare(swoole_version(), '4.3.1', 'ge')) {
  46. $this->output->error('Your Swoole version must be higher than `4.3.1`.');
  47. exit(1);
  48. }
  49. }
  50. /**
  51. * 启动server
  52. * @access protected
  53. * @param Manager $manager
  54. * @param PidManager $pidManager
  55. * @return void
  56. */
  57. protected function start(Manager $manager, PidManager $pidManager)
  58. {
  59. if ($pidManager->isRunning()) {
  60. $this->output->writeln('<error>swoole rpc server process is already running.</error>');
  61. return;
  62. }
  63. $this->output->writeln('Starting swoole rpc server...');
  64. $host = $this->app->config->get('swoole.server.host');
  65. $port = $this->app->config->get('swoole.rpc.server.port');
  66. $this->output->writeln("Swoole rpc server started: <tcp://{$host}:{$port}>");
  67. $this->output->writeln('You can exit with <info>`CTRL-C`</info>');
  68. $manager->run();
  69. }
  70. /**
  71. * 柔性重启server
  72. * @access protected
  73. * @param PidManager $manager
  74. * @return void
  75. */
  76. protected function reload(PidManager $manager)
  77. {
  78. if (!$manager->isRunning()) {
  79. $this->output->writeln('<error>no swoole rpc server process running.</error>');
  80. return;
  81. }
  82. $this->output->writeln('Reloading swoole rpc server...');
  83. if (!$manager->killProcess(SIGUSR1)) {
  84. $this->output->error('> failure');
  85. return;
  86. }
  87. $this->output->writeln('> success');
  88. }
  89. /**
  90. * 停止server
  91. * @access protected
  92. * @param PidManager $manager
  93. * @return void
  94. */
  95. protected function stop(PidManager $manager)
  96. {
  97. if (!$manager->isRunning()) {
  98. $this->output->writeln('<error>no swoole rpc server process running.</error>');
  99. return;
  100. }
  101. $this->output->writeln('Stopping swoole rpc server...');
  102. $isRunning = $manager->killProcess(SIGTERM, 15);
  103. if ($isRunning) {
  104. $this->output->error('Unable to stop the rpc process.');
  105. return;
  106. }
  107. $this->output->writeln('> success');
  108. }
  109. /**
  110. * 重启server
  111. * @access protected
  112. * @param Manager $manager
  113. * @param PidManager $pidManager
  114. * @return void
  115. */
  116. protected function restart(Manager $manager, PidManager $pidManager)
  117. {
  118. if ($pidManager->isRunning()) {
  119. $this->stop($pidManager);
  120. }
  121. $this->start($manager, $pidManager);
  122. }
  123. /**
  124. * Create swoole server.
  125. */
  126. protected function createSwooleServer()
  127. {
  128. $config = $this->app->config;
  129. $host = $config->get('swoole.server.host');
  130. $port = $config->get('swoole.rpc.server.port');
  131. $socketType = $config->get('swoole.server.socket_type', SWOOLE_SOCK_TCP);
  132. $mode = $config->get('swoole.server.mode', SWOOLE_PROCESS);
  133. $server = new \Swoole\Server($host, $port, $mode, $socketType);
  134. $options = $config->get('swoole.server.options');
  135. $server->set($options);
  136. return $server;
  137. }
  138. }