AutoClearLog.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use App\Http\Models\Config;
  5. use App\Http\Models\SsNodeInfo;
  6. use App\Http\Models\SsNodeOnlineLog;
  7. use App\Http\Models\SsNodeTrafficHourly;
  8. use App\Http\Models\UserTrafficLog;
  9. use App\Http\Models\UserTrafficHourly;
  10. use Log;
  11. class AutoClearLog extends Command
  12. {
  13. protected $signature = 'autoClearLog';
  14. protected $description = '自动清除日志';
  15. public function __construct()
  16. {
  17. parent::__construct();
  18. }
  19. public function handle()
  20. {
  21. $jobStartTime = microtime(true);
  22. $config = $this->systemConfig();
  23. if ($config['is_clear_log']) {
  24. // 自动清除30分钟以前的节点负载信息日志
  25. SsNodeInfo::query()->where('log_time', '<=', strtotime(date('Y-m-d H:i:s', strtotime("-30 minutes"))))->delete();
  26. // 自动清除1小时以前的节点在线用户数日志
  27. SsNodeOnlineLog::query()->where('log_time', '<=', strtotime(date('Y-m-d H:i:s', strtotime("-60 minutes"))))->delete();
  28. // 自动清除30天以前的用户流量日志
  29. UserTrafficLog::query()->where('log_time', '<=', strtotime(date('Y-m-d H:i:s', strtotime("-30 days"))))->delete();
  30. // 自动清除10天以前的用户每小时流量数据日志
  31. UserTrafficHourly::query()->where('created_at', '<=', date('Y-m-d H:i:s', strtotime('-10 days')))->delete();
  32. // 自动清除60天以前的节点每小时流量数据日志
  33. SsNodeTrafficHourly::query()->where('created_at', '<=', date('Y-m-d H:i:s', strtotime('-60 days')))->delete();
  34. }
  35. $jobEndTime = microtime(true);
  36. $jobUsedTime = round(($jobEndTime - $jobStartTime) , 4);
  37. Log::info('定时任务【' . $this->description . '】耗时' . $jobUsedTime . '秒');
  38. }
  39. // 系统配置
  40. private function systemConfig() {
  41. $config = Config::query()->get();
  42. $data = [];
  43. foreach ($config as $vo) {
  44. $data[$vo->name] = $vo->value;
  45. }
  46. return $data;
  47. }
  48. }