CommandSecond.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. namespace UCore\Command;
  3. use UCore\Helper\Logger;
  4. use UCore\Helper\Helper;
  5. use Illuminate\Console\Command;
  6. /**
  7. * 按秒执行的命令基类
  8. */
  9. abstract class CommandSecond extends Command implements CommandSecondFace
  10. {
  11. /**
  12. * 等待秒数(最后几秒不执行)
  13. * @var int $waitSecond
  14. */
  15. protected $waitSecond = 2;
  16. /**
  17. * 间隔时长
  18. * @var int $sleepSecond
  19. */
  20. protected $sleepSecond = 1;
  21. /**
  22. * Execute the console command.
  23. *
  24. * @return int
  25. */
  26. public function handle()
  27. {
  28. Helper::add_log('run-s' , 'Console', static::class, []);
  29. foreach (range(1, 60) as $a) {
  30. $s = date('s');
  31. if ($s >= (60-$this->waitSecond)) {
  32. // 最后几秒 不执行
  33. break;
  34. }
  35. try {
  36. $this->handleSecond($s);
  37. } catch (\Exception $exception) {
  38. Logger::exception('CommandSecond'.static::class,$exception);
  39. echo $exception->getMessage();
  40. echo $exception->getTraceAsString();
  41. }
  42. sleep($this->sleepSecond);
  43. }
  44. Helper::add_log('run-s-end' , 'Console', static::class, []);
  45. return Command::SUCCESS;
  46. }
  47. }