Start.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkAdmin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2019 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://demo.thinkadmin.top
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | gitee 代码仓库:https://gitee.com/zoujingli/ThinkAdmin
  12. // | github 代码仓库:https://github.com/zoujingli/ThinkAdmin
  13. // +----------------------------------------------------------------------
  14. namespace app\admin\queue\task;
  15. use library\command\Task;
  16. use think\console\Input;
  17. use think\console\Output;
  18. /**
  19. * 检查并创建异步任务监听主进程
  20. * Class Start
  21. * @package app\admin\queue\task
  22. */
  23. class Start extends Task
  24. {
  25. /**
  26. * 指令属性配置
  27. */
  28. protected function configure()
  29. {
  30. $this->setName('xtask:start')->setDescription('[控制]创建异步任务守护监听主进程');
  31. }
  32. /**
  33. * 执行启动操作
  34. * @param Input $input
  35. * @param Output $output
  36. */
  37. protected function execute(Input $input, Output $output)
  38. {
  39. $this->setBaseProcess();
  40. if (($pid = $this->checkProcess()) > 0) {
  41. $output->info("异步任务监听主进程{$pid}已经启动!");
  42. } else {
  43. $this->setWinProcess();
  44. $this->createProcess();
  45. $this->setBaseProcess();
  46. $output->writeln('正在检查异步任务监听主进程状态...');
  47. sleep(2);
  48. if (($pid = $this->checkProcess()) > 0) {
  49. $output->info("异步任务监听主进程{$pid}启动成功!");
  50. } else {
  51. $output->error('异步任务监听主进程创建失败!');
  52. }
  53. }
  54. }
  55. private function setBaseProcess()
  56. {
  57. $this->cmd = "{$this->bin} xtask:listen";
  58. }
  59. private function setWinProcess()
  60. {
  61. if ($this->isWin()) {
  62. $command = env('runtime_path') . "queue" . DIRECTORY_SEPARATOR . "listen.cmd";
  63. file_exists(dirname($command)) or mkdir(dirname($command), 0755, true);
  64. file_put_contents($command, $this->cmd . PHP_EOL . 'del %~dp0%0 /y');
  65. $this->cmd = __DIR__ . DIRECTORY_SEPARATOR . "/bin/process.exe {$command}";
  66. }
  67. }
  68. }