|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Console\Commands; |
| 4 | + |
| 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 Illuminate\Console\Command; |
| 10 | +use App\Http\Models\Config; |
| 11 | +use App\Http\Models\UserTrafficHourly; |
| 12 | +use Log; |
| 13 | + |
| 14 | +class AutoClearLogJob extends Command |
| 15 | +{ |
| 16 | + protected $signature = 'command:autoClearLogJob'; |
| 17 | + protected $description = '自动清除日志'; |
| 18 | + |
| 19 | + protected static $config; |
| 20 | + |
| 21 | + public function __construct() |
| 22 | + { |
| 23 | + parent::__construct(); |
| 24 | + |
| 25 | + $config = Config::query()->get(); |
| 26 | + $data = []; |
| 27 | + foreach ($config as $vo) { |
| 28 | + $data[$vo->name] = $vo->value; |
| 29 | + } |
| 30 | + |
| 31 | + self::$config = $data; |
| 32 | + } |
| 33 | + |
| 34 | + public function handle() |
| 35 | + { |
| 36 | + if (self::$config['is_clear_log']) { |
| 37 | + // 自动清除12小时以前的节点负载信息日志 |
| 38 | + SsNodeInfo::query()->where('log_time', '<=', strtotime(date('Y-m-d H:i:s', strtotime("-12 hours"))))->delete(); |
| 39 | + |
| 40 | + // 自动清除1小时以前的节点负载信息日志 |
| 41 | + SsNodeOnlineLog::query()->where('log_time', '<=', strtotime(date('Y-m-d H:i:s', strtotime("-60 minutes"))))->delete(); |
| 42 | + |
| 43 | + // 自动清除30天以前的用户流量日志 |
| 44 | + UserTrafficLog::query()->where('log_time', '<=', strtotime(date('Y-m-d H:i:s', strtotime("-30 days"))))->delete(); |
| 45 | + |
| 46 | + // 自动清除10天以前的用户每小时流量数据日志 |
| 47 | + UserTrafficHourly::query()->where('created_at', '<=', date('Y-m-d H:i:s', strtotime('-10 days')))->delete(); |
| 48 | + |
| 49 | + // 自动清除10天以前的节点每小时流量数据日志 |
| 50 | + SsNodeTrafficHourly::query()->where('created_at', '<=', date('Y-m-d H:i:s', strtotime('-10 days')))->delete(); |
| 51 | + } |
| 52 | + |
| 53 | + Log::info('定时任务:' . $this->description); |
| 54 | + } |
| 55 | +} |
0 commit comments