AutoClearLogJob.php 1.9 KB

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