Crontab.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeadmin快速开发前后端分离管理后台(PHP版)
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
  8. // | github下载:https://github.com/likeshop-github/likeadmin
  9. // | 访问官网:https://www.likeadmin.cn
  10. // | likeadmin团队 版权所有 拥有最终解释权
  11. // +----------------------------------------------------------------------
  12. // | author: likeadminTeam
  13. // +----------------------------------------------------------------------
  14. namespace app\common\command;
  15. use app\common\enum\CrontabEnum;
  16. use think\console\Command;
  17. use think\console\Input;
  18. use think\console\Output;
  19. use Cron\CronExpression;
  20. use think\facade\Console;
  21. use app\common\model\Crontab as CrontabModel;
  22. /**
  23. * 定时任务
  24. * Class Crontab
  25. * @package app\command
  26. */
  27. class Crontab extends Command
  28. {
  29. protected function configure()
  30. {
  31. $this->setName('crontab')
  32. ->setDescription('定时任务');
  33. }
  34. protected function execute(Input $input, Output $output)
  35. {
  36. $lists = CrontabModel::where('status', CrontabEnum::START)->select()->toArray();
  37. if (empty($lists)) {
  38. return false;
  39. }
  40. $time = time();
  41. foreach ($lists as $item) {
  42. if (empty($item['last_time'])) {
  43. $lastTime = (new CronExpression($item['expression']))
  44. ->getNextRunDate()
  45. ->getTimestamp();
  46. CrontabModel::where('id', $item['id'])->update([
  47. 'last_time' => $lastTime,
  48. ]);
  49. continue;
  50. }
  51. $nextTime = (new CronExpression($item['expression']))
  52. ->getNextRunDate($item['last_time'])
  53. ->getTimestamp();
  54. if ($nextTime > $time) {
  55. // 未到时间,不执行
  56. continue;
  57. }
  58. // 开始执行
  59. self::start($item);
  60. }
  61. }
  62. public static function start($item)
  63. {
  64. // 开始执行
  65. $startTime = microtime(true);
  66. try {
  67. $params = explode(' ', $item['params']);
  68. if (is_array($params) && !empty($item['params'])) {
  69. Console::call($item['command'], $params);
  70. } else {
  71. Console::call($item['command']);
  72. }
  73. // 清除错误信息
  74. CrontabModel::where('id', $item['id'])->update(['error' => '']);
  75. } catch (\Exception $e) {
  76. // 记录错误信息
  77. CrontabModel::where('id', $item['id'])->update([
  78. 'error' => $e->getMessage(),
  79. 'status' => CrontabEnum::ERROR
  80. ]);
  81. } finally {
  82. $endTime = microtime(true);
  83. // 本次执行时间
  84. $useTime = round(($endTime - $startTime), 2);
  85. // 最大执行时间
  86. $maxTime = max($useTime, $item['max_time']);
  87. // 更新最后执行时间
  88. CrontabModel::where('id', $item['id'])->update([
  89. 'last_time' => time(),
  90. 'time' => $useTime,
  91. 'max_time' => $maxTime
  92. ]);
  93. }
  94. }
  95. }