StopCommand.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Command;
  4. //use App\Model\SystemWork;
  5. use Swoole\Process;
  6. use Symfony\Component\Console\Command\Command;
  7. use Symfony\Component\Console\Input\InputInterface;
  8. use Symfony\Component\Console\Output\OutputInterface;
  9. use Symfony\Component\Console\Style\SymfonyStyle;
  10. use Hyperf\Command\Annotation\Command as HyperfCommand;
  11. /**
  12. * @HyperfCommand()
  13. */
  14. class StopCommand extends Command
  15. {
  16. public function __construct()
  17. {
  18. parent::__construct('stop');
  19. }
  20. protected function configure()
  21. {
  22. $this->setDescription('Stop hyperf servers.');
  23. }
  24. protected function execute(InputInterface $input, OutputInterface $output)
  25. {
  26. $io = new SymfonyStyle($input, $output);
  27. $pidFile = BASE_PATH . '/runtime/hyperf.pid';
  28. $pid = file_exists($pidFile) ? intval(file_get_contents($pidFile)) : false;
  29. if (!$pid) {
  30. $io->note('swoole server pid is invalid.');
  31. return -1;
  32. }
  33. if (!Process::kill($pid, SIG_DFL)) {
  34. $io->note('swoole server process does not exist.');
  35. return -1;
  36. }
  37. if (!Process::kill($pid, SIGTERM)) {
  38. $io->error('swoole server stop error.');
  39. return -1;
  40. }
  41. $io->success('swoole server stop success.');
  42. // SystemWork::shutDown();
  43. return 0;
  44. }
  45. }