Task.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\command;
  4. use app\admin\model\TaxiChauffeur;
  5. use think\console\Command;
  6. use think\console\Input;
  7. use think\console\input\Argument;
  8. use think\console\input\Option;
  9. use think\console\Output;
  10. class Task extends Command
  11. {
  12. protected function configure()
  13. {
  14. // 指令配置
  15. $this->setName('task')
  16. //增加一个命令参数
  17. ->addArgument('action', Argument::OPTIONAL, "action", '')
  18. ->addArgument('force', Argument::OPTIONAL, "force", '');
  19. }
  20. protected function execute(Input $input, Output $output)
  21. {
  22. //获取输入参数
  23. $action = trim($input->getArgument('action'));
  24. $force = trim($input->getArgument('force'));
  25. $task = new \EasyTask\Task();
  26. $task->setDaemon(true);
  27. //$task->setPrefix('task_test');
  28. $task->setRunTimePath('./runtime/');
  29. // 配置任务,开启1个进程每隔10秒进行一次
  30. $task->addFunc(function () {
  31. (new TaxiChauffeur())->getChauffeurData();
  32. }, 'data_save', 30, 1);
  33. // 根据命令执行
  34. if ($action == 'start') {
  35. $task->start();
  36. } elseif ($action == 'status') {
  37. $task->status();
  38. } elseif ($action == 'stop') {
  39. $force = ($force == 'force'); //是否强制停止
  40. $task->stop($force);
  41. } else {
  42. exit('Command is not exist');
  43. }
  44. }
  45. }