Solver.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. declare (strict_types=1);
  3. namespace app\command;
  4. use think\console\Command;
  5. use think\console\Input;
  6. use think\console\input\Argument;
  7. use think\console\input\Option;
  8. use think\console\Output;
  9. use think\facade\Db;
  10. use think\facade\Log;
  11. use app\common\api\XwCrontab;
  12. /**
  13. * 主要处理短信的命令行程序
  14. */
  15. class Solver extends Command {
  16. protected function configure() {
  17. // 指令配置
  18. $this->setName('solver')
  19. ->setDescription('the solver command');
  20. }
  21. protected function execute(Input $input, Output $output) {
  22. // 指令输出
  23. $lockFileName = "task_solver.lock";
  24. $lockFile = fopen($lockFileName, "a");
  25. if (flock($lockFile, LOCK_EX | LOCK_NB)) {//文件锁(独占)
  26. set_time_limit(0);
  27. $root = app()->getRootPath();
  28. $schedules = Db::table("new_schedule")->select();
  29. foreach ($schedules as $schedule) {
  30. //检查时间到了没
  31. if (XwCrontab::check(time(), $schedule["timeStr"]) && $schedule["state"] == 1) {
  32. exec("php {$root}think " . $schedule["action"]);
  33. }
  34. }
  35. flock($lockFile, LOCK_UN);
  36. } else {
  37. $output->writeln('每分钟执行一次,执行时间超过1分钟下个1分钟跳过等待再下1分钟');
  38. }
  39. }
  40. }