MConsole.Class.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. namespace Jobs\Model;
  3. use Mall\Framework\Factory;
  4. use Jobs\Model\MProcess;
  5. class MConsole
  6. {
  7. public $logger = null;
  8. private $config = [];
  9. public function __construct($config)
  10. {
  11. $this->config = $config;
  12. $this->logger = Factory::logs($this->config['logPath'] ?:'', $this->config['logSaveFileApp'] ?:'', $this->config['app_name'] ?:'');
  13. }
  14. public function run()
  15. {
  16. $this->runOpt();
  17. }
  18. public function runOpt()
  19. {
  20. global $argv;
  21. if (empty($argv[1])) {
  22. $this->printHelpMessage();
  23. exit();
  24. }
  25. $opt=$argv[1];
  26. switch ($opt) {
  27. case 'start':
  28. $this->start($this->config);
  29. break;
  30. case 'stop':
  31. $this->sendSignal();
  32. break;
  33. case 'status':
  34. $this->sendSignal(SIGUSR2);
  35. break;
  36. case 'exit':
  37. $this->kill();
  38. break;
  39. case 'restart':
  40. $this->restart();
  41. break;
  42. case 'help':
  43. $this->printHelpMessage();
  44. break;
  45. default:
  46. $this->printHelpMessage();
  47. break;
  48. }
  49. }
  50. public function restart()
  51. {
  52. $this->logger->log('restarting...');
  53. $this->kill();
  54. sleep(3);
  55. $this->start($this->config);
  56. }
  57. public function kill()
  58. {
  59. $this->sendSignal(SIGTERM);
  60. }
  61. /**
  62. * 启动进程
  63. */
  64. public function start($config)
  65. {
  66. $process = new MProcess($config);
  67. $process->start();
  68. }
  69. /**
  70. * 给主进程发送信号:
  71. * SIGUSR1 自定义信号,让子进程平滑退出
  72. * SIGUSR2 自定义信号2,显示进程状态
  73. * SIGTERM 程序终止,让子进程强制退出.
  74. *
  75. * @param [type] $signal
  76. */
  77. public function sendSignal($signal=SIGUSR1)
  78. {
  79. $this->logger->log($signal . (SIGUSR1 == $signal) ? ' smooth to exit...' : ' force to exit...');
  80. if (isset($this->config['pidPath']) && !empty($this->config['pidPath'])) {
  81. $masterPidFile=$this->config['pidPath'] . '/master.pid';
  82. $pidStatusFile=$this->config['pidPath'] . '/status.info';
  83. } else {
  84. die('config pidPath must be set!' . PHP_EOL);
  85. }
  86. if (file_exists($masterPidFile)) {
  87. $pid =file_get_contents($masterPidFile);
  88. if ($pid && !@\Swoole\Process::kill($pid, 0)) {
  89. exit('service is not running' . PHP_EOL);
  90. }
  91. if (@\Swoole\Process::kill($pid, $signal)) {
  92. $this->logger->log('[master pid: ' . $pid . '] has been received signal' . $signal);
  93. sleep(1);
  94. //如果是SIGUSR2信号,显示swoole-jobs状态信息
  95. if (SIGUSR2 == $signal) {
  96. $statusStr=file_get_contents($pidStatusFile);
  97. echo $statusStr ? $statusStr : 'sorry,show status fail.';
  98. exit;
  99. }
  100. }
  101. $this->logger->log('[master pid: ' . $pid . '] has been received signal fail');
  102. } else {
  103. exit('service is not running' . PHP_EOL);
  104. }
  105. }
  106. /**
  107. * 帮助手册
  108. */
  109. public function printHelpMessage()
  110. {
  111. $msg=<<<'EOF'
  112. NAME
  113. php swoole-jobs - manage swoole-jobs
  114. SYNOPSIS
  115. php swoole-jobs command [options]
  116. Manage swoole-jobs daemons.
  117. WORKFLOWS
  118. help [command]
  119. Show this help, or workflow help for command.
  120. restart
  121. Stop, then start swoole-jobs master and workers.
  122. start
  123. Start swoole-jobs master and workers.
  124. stop
  125. Wait all running workers smooth exit, please check swoole-jobs status for a while.
  126. exit
  127. Kill all running workers and master PIDs.
  128. EOF;
  129. echo $msg;
  130. }
  131. }