From e3cc1d2c706cb7d4927f1af28e8aa2c211dcd13a Mon Sep 17 00:00:00 2001 From: lkddi Date: Fri, 27 Feb 2026 00:12:16 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=EF=BC=9A=E8=81=8A=E5=A4=A9?= =?UTF-8?q?=E8=AE=B0=E5=BD=95=E5=AE=9A=E6=9C=9F=E6=B8=85=E7=90=86=E4=BB=BB?= =?UTF-8?q?=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增 messages:purge Artisan 命令,分批删除旧消息 - 默认保留最近 30 天,可通过 sysparam message_retention_days 配置 - 支持 --days 参数覆盖和 --dry-run 预览模式 - 每天凌晨 3 点自动执行 - 线上需配置 crontab: * * * * * php artisan schedule:run --- app/Console/Commands/PurgeOldMessages.php | 91 +++++++++++++++++++++++ routes/console.php | 4 + 2 files changed, 95 insertions(+) create mode 100644 app/Console/Commands/PurgeOldMessages.php diff --git a/app/Console/Commands/PurgeOldMessages.php b/app/Console/Commands/PurgeOldMessages.php new file mode 100644 index 0000000..525f535 --- /dev/null +++ b/app/Console/Commands/PurgeOldMessages.php @@ -0,0 +1,91 @@ + sysparam 配置 > 默认 30 天 + $days = (int) ($this->option('days') + ?: Sysparam::getValue('message_retention_days', '30')); + + $cutoff = Carbon::now()->subDays($days); + $isDryRun = $this->option('dry-run'); + + // 统计待清理数量 + $totalCount = Message::where('sent_at', '<', $cutoff)->count(); + + if ($totalCount === 0) { + $this->info("✅ 没有超过 {$days} 天的聊天记录需要清理。"); + + return self::SUCCESS; + } + + if ($isDryRun) { + $this->warn("🔍 [预览模式] 将删除 {$totalCount} 条超过 {$days} 天的聊天记录(截止 {$cutoff->toDateTimeString()})"); + + return self::SUCCESS; + } + + $this->info("🧹 开始清理超过 {$days} 天的聊天记录(截止 {$cutoff->toDateTimeString()})..."); + $this->info(" 待清理数量:{$totalCount} 条"); + + // 分批删除,每批 1000 条,避免长时间锁表 + $deleted = 0; + $batchSize = 1000; + + do { + $batch = Message::where('sent_at', '<', $cutoff) + ->limit($batchSize) + ->delete(); + + $deleted += $batch; + + if ($batch > 0) { + $this->line(" 已删除 {$deleted}/{$totalCount} 条..."); + } + } while ($batch === $batchSize); + + $this->info("✅ 清理完成!共删除 {$deleted} 条聊天记录。"); + + return self::SUCCESS; + } +} diff --git a/routes/console.php b/routes/console.php index 3c9adf1..16042f6 100644 --- a/routes/console.php +++ b/routes/console.php @@ -2,7 +2,11 @@ use Illuminate\Foundation\Inspiring; use Illuminate\Support\Facades\Artisan; +use Illuminate\Support\Facades\Schedule; Artisan::command('inspire', function () { $this->comment(Inspiring::quote()); })->purpose('Display an inspiring quote'); + +// 每天凌晨 3 点清理超过 30 天的聊天记录 +Schedule::command('messages:purge')->dailyAt('03:00');