Task.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. /**
  3. * Created by PhpStorm
  4. * User Julyssn
  5. * Date 2022/12/14 16:12
  6. */
  7. namespace task\command;
  8. use easyTask\Helper;
  9. use easyTask\Task as EasyTask;
  10. use think\console\Command;
  11. use think\console\Input;
  12. use think\console\input\Argument;
  13. use think\console\Output;
  14. use think\helper\Str;
  15. class Task extends Command
  16. {
  17. protected function configure()
  18. {
  19. // 指令配置
  20. $this->setName('task')
  21. ->addArgument('action', Argument::OPTIONAL, "action", '')
  22. ->addArgument('force', Argument::OPTIONAL, "force", '')
  23. ->setDescription('the task command');
  24. }
  25. protected function execute(Input $input, Output $output)
  26. {
  27. //获取输入参数
  28. $action = trim($input->getArgument('action'));
  29. $force = trim($input->getArgument('force'));
  30. $rootPath = root_path();
  31. $task = new EasyTask();
  32. // 设置常驻内存
  33. $task->setDaemon(!Helper::isWin());
  34. // 设置项目名称 获取运行目录文件夹名称
  35. $task->setPrefix('easy_task');
  36. // 设置子进程挂掉自动重启
  37. $task->setAutoRecover(true);
  38. // 设置运行时目录(日志或缓存目录)
  39. $task->setRunTimePath($rootPath . 'runtime');
  40. // 消息推送
  41. $task->addCommand('php think worker:gateway start', 'worker', 0);
  42. // 定时任务
  43. $task->addCommand('php think cron:run', 'schedule', 60);
  44. // 消息队列
  45. $task->addCommand('php think queue:listen --sleep 0.3 --queue lvzhe', 'queue', 0);
  46. // 定时删除运行日志
  47. $task->addFunc(function () {
  48. $rootPath = root_path();
  49. $stdPath=$rootPath . 'runtime'.DIRECTORY_SEPARATOR.'easy_task'.DIRECTORY_SEPARATOR.'Std';
  50. foreach (glob($stdPath . DIRECTORY_SEPARATOR . '*.std') as $file) {
  51. if (is_file($file)) {
  52. print $file."清理文件\n";
  53. unlink($file);
  54. }
  55. }
  56. print $stdPath." 文件清理成功\n";
  57. }, 'clearStd', 86400);
  58. // 根据命令执行
  59. if ($action == 'start') {
  60. $task->start();
  61. } elseif ($action == 'status') {
  62. $task->status();
  63. } elseif ($action == 'stop') {
  64. $force = ($force == 'force'); //是否强制停止
  65. $task->stop($force);
  66. } else {
  67. exit('Command is not exist');
  68. }
  69. }
  70. }