AutoClearLogJob.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 = 'command:autoClearLogJob';
  14. protected $description = '自动清除日志';
  15. protected static $config;
  16. public function __construct()
  17. {
  18. parent::__construct();
  19. $config = Config::query()->get();
  20. $data = [];
  21. foreach ($config as $vo) {
  22. $data[$vo->name] = $vo->value;
  23. }
  24. self::$config = $data;
  25. }
  26. public function handle()
  27. {
  28. if (self::$config['is_clear_log']) {
  29. // 自动清除12小时以前的节点负载信息日志
  30. SsNodeInfo::query()->where('log_time', '<=', strtotime(date('Y-m-d H:i:s', strtotime("-12 hours"))))->delete();
  31. // 自动清除1小时以前的节点负载信息日志
  32. SsNodeOnlineLog::query()->where('log_time', '<=', strtotime(date('Y-m-d H:i:s', strtotime("-60 minutes"))))->delete();
  33. // 自动清除30天以前的用户流量日志
  34. UserTrafficLog::query()->where('log_time', '<=', strtotime(date('Y-m-d H:i:s', strtotime("-30 days"))))->delete();
  35. // 自动清除10天以前的用户每小时流量数据日志
  36. UserTrafficHourly::query()->where('created_at', '<=', date('Y-m-d H:i:s', strtotime('-10 days')))->delete();
  37. // 自动清除10天以前的节点每小时流量数据日志
  38. SsNodeTrafficHourly::query()->where('created_at', '<=', date('Y-m-d H:i:s', strtotime('-10 days')))->delete();
  39. }
  40. Log::info('定时任务:' . $this->description);
  41. }
  42. }