Swoole.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Http\Seller\Services\Chat\Service;
  4. use Illuminate\Console\Command;
  5. use Swoole\Process;
  6. class Swoole extends Command
  7. {
  8. /**
  9. * The name and signature of the console command.
  10. *
  11. * @var string
  12. */
  13. protected $signature = 'swoole:serve {action? : "命令:start|stop|restart|reload"} {--not|not_daemon : 进程不进入后台执行}';
  14. protected $description = '启动客服服务';
  15. protected $is_not_daemon = false;
  16. /**
  17. * Create a new command instance.
  18. *
  19. * @return void
  20. */
  21. public function __construct()
  22. {
  23. parent::__construct();
  24. }
  25. /**
  26. * Execute the console command.
  27. *
  28. * @return mixed
  29. */
  30. public function handle()
  31. {
  32. $action = $this->argument('action');
  33. $this->is_not_daemon = $this->option('not_daemon');
  34. if ($action) {
  35. $this->$action();
  36. } else {
  37. $this->start();
  38. }
  39. }
  40. /**
  41. * 启动server
  42. */
  43. private function start()
  44. {
  45. $pid = $this->getMasterPid();
  46. if ($this->isRunning($pid)) {
  47. $this->warn('<error>swoole server process is already running.</error>');
  48. return false;
  49. }
  50. $this->info('Starting swoole server...');
  51. if ($this->is_not_daemon) {
  52. app('config')->set('swoole.daemonize', false);
  53. }
  54. (new Service())->run();
  55. }
  56. private function restart()
  57. {
  58. $pid = $this->getMasterPid();
  59. $this->stop();
  60. sleep(1);
  61. if (!$this->isRunning($pid)) {
  62. $this->start();
  63. }
  64. }
  65. /**
  66. * 柔性重启server
  67. *
  68. * @return bool
  69. */
  70. private function reload()
  71. {
  72. // 柔性重启使用管理PID
  73. $pid = $this->getMasterPid();
  74. if (!$this->isRunning($pid)) {
  75. $this->warn('no swoole server process running.');
  76. return false;
  77. }
  78. $this->info('Reloading swoole server...');
  79. Process::kill($pid, SIGUSR1);
  80. $this->info('> success');
  81. }
  82. private function stop()
  83. {
  84. $pid = $this->getMasterPid();
  85. if (!$this->isRunning($pid)) {
  86. $this->warn('no swoole server process running.');
  87. return false;
  88. }
  89. $this->info('Stopping swoole server...');
  90. Process::kill($pid, SIGTERM);
  91. $this->removePid();
  92. $this->info('> success');
  93. }
  94. /**
  95. * 获取主进程PID
  96. * @access protected
  97. * @return int
  98. */
  99. private function getMasterPid()
  100. {
  101. $pidFile = config('swoole.pid_file');
  102. if (is_file($pidFile)) {
  103. $masterPid = (int)file_get_contents($pidFile);
  104. } else {
  105. $masterPid = 0;
  106. }
  107. return $masterPid;
  108. }
  109. /**
  110. * 判断PID是否在运行
  111. * @access protected
  112. * @param int $pid
  113. * @return bool
  114. */
  115. protected function isRunning($pid)
  116. {
  117. if (empty($pid)) {
  118. return false;
  119. }
  120. return Process::kill($pid, 0);
  121. }
  122. /**
  123. * 删除PID文件
  124. * @access protected
  125. * @return void
  126. */
  127. protected function removePid()
  128. {
  129. $masterPid = config('swoole.pid_file');
  130. if (is_file($masterPid)) {
  131. unlink($masterPid);
  132. }
  133. }
  134. }