Server.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2018 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. namespace think\swoole\command;
  12. use Swoole\Http\Server as HttpServer;
  13. use Swoole\WebSocket\Server as WebsocketServer;
  14. use think\console\Command;
  15. use think\console\Input;
  16. use think\console\input\Argument;
  17. use think\console\Output;
  18. use think\swoole\Manager;
  19. use think\swoole\PidManager;
  20. /**
  21. * Swoole HTTP 命令行,支持操作:start|stop|restart|reload
  22. * 支持应用配置目录下的swoole.php文件进行参数配置
  23. */
  24. class Server extends Command
  25. {
  26. public function configure()
  27. {
  28. $this->setName('swoole')
  29. ->addArgument('action', Argument::OPTIONAL, 'start|stop|restart|reload', 'start')
  30. ->setDescription('Swoole HTTP Server for ThinkPHP');
  31. }
  32. protected function initialize(Input $input, Output $output)
  33. {
  34. $this->app->bind(\Swoole\Server::class, function () {
  35. return $this->createSwooleServer();
  36. });
  37. $this->app->bind(PidManager::class, function () {
  38. return new PidManager($this->app->config->get('swoole.server.options.pid_file'));
  39. });
  40. }
  41. public function handle()
  42. {
  43. $this->checkEnvironment();
  44. $action = $this->input->getArgument('action');
  45. if (in_array($action, ['start', 'stop', 'reload', 'restart'])) {
  46. $this->app->invokeMethod([$this, $action], [], true);
  47. } else {
  48. $this->output->writeln("<error>Invalid argument action:{$action}, Expected start|stop|restart|reload .</error>");
  49. }
  50. }
  51. /**
  52. * 检查环境
  53. */
  54. protected function checkEnvironment()
  55. {
  56. if (!extension_loaded('swoole')) {
  57. $this->output->error('Can\'t detect Swoole extension installed.');
  58. exit(1);
  59. }
  60. if (!version_compare(swoole_version(), '4.4.8', 'ge')) {
  61. $this->output->error('Your Swoole version must be higher than `4.4.8`.');
  62. exit(1);
  63. }
  64. }
  65. /**
  66. * 启动server
  67. * @access protected
  68. * @param Manager $manager
  69. * @param PidManager $pidManager
  70. * @return void
  71. */
  72. protected function start(Manager $manager, PidManager $pidManager)
  73. {
  74. if ($pidManager->isRunning()) {
  75. $this->output->writeln('<error>swoole http server process is already running.</error>');
  76. return;
  77. }
  78. $this->output->writeln('Starting swoole http server...');
  79. $host = $manager->getConfig('server.host');
  80. $port = $manager->getConfig('server.port');
  81. $this->output->writeln("Swoole http server started: <http://{$host}:{$port}>");
  82. $this->output->writeln('You can exit with <info>`CTRL-C`</info>');
  83. $manager->run();
  84. }
  85. /**
  86. * 柔性重启server
  87. * @access protected
  88. * @param PidManager $manager
  89. * @return void
  90. */
  91. protected function reload(PidManager $manager)
  92. {
  93. if (!$manager->isRunning()) {
  94. $this->output->writeln('<error>no swoole http server process running.</error>');
  95. return;
  96. }
  97. $this->output->writeln('Reloading swoole http server...');
  98. if (!$manager->killProcess(SIGUSR1)) {
  99. $this->output->error('> failure');
  100. return;
  101. }
  102. $this->output->writeln('> success');
  103. }
  104. /**
  105. * 停止server
  106. * @access protected
  107. * @param PidManager $manager
  108. * @return void
  109. */
  110. protected function stop(PidManager $manager)
  111. {
  112. if (!$manager->isRunning()) {
  113. $this->output->writeln('<error>no swoole http server process running.</error>');
  114. return;
  115. }
  116. $this->output->writeln('Stopping swoole http server...');
  117. $isRunning = $manager->killProcess(SIGTERM, 15);
  118. if ($isRunning) {
  119. $this->output->error('Unable to stop the swoole_http_server process.');
  120. return;
  121. }
  122. $this->output->writeln('> success');
  123. }
  124. /**
  125. * 重启server
  126. * @access protected
  127. * @param Manager $manager
  128. * @param PidManager $pidManager
  129. * @return void
  130. */
  131. protected function restart(Manager $manager, PidManager $pidManager)
  132. {
  133. if ($pidManager->isRunning()) {
  134. $this->stop($pidManager);
  135. }
  136. $this->start($manager, $pidManager);
  137. }
  138. /**
  139. * Create swoole server.
  140. */
  141. protected function createSwooleServer()
  142. {
  143. $isWebsocket = $this->app->config->get('swoole.websocket.enable', false);
  144. $serverClass = $isWebsocket ? WebsocketServer::class : HttpServer::class;
  145. $config = $this->app->config;
  146. $host = $config->get('swoole.server.host');
  147. $port = $config->get('swoole.server.port');
  148. $socketType = $config->get('swoole.server.socket_type', SWOOLE_SOCK_TCP);
  149. $mode = $config->get('swoole.server.mode', SWOOLE_PROCESS);
  150. /** @var \Swoole\Server $server */
  151. $server = new $serverClass($host, $port, $mode, $socketType);
  152. $options = $config->get('swoole.server.options');
  153. $server->set($options);
  154. return $server;
  155. }
  156. }