feat: delete

This commit is contained in:
hashbro
2026-09-01 06:16:49 +08:00
parent 35fe93e42d
commit 4ece122625
3 changed files with 142 additions and 6 deletions
+47
View File
@@ -6,6 +6,8 @@ use App\Services\CorunaArchive;
use App\Services\CorunaCrypto;
use App\Services\SettingsService;
use App\Telegram\TelegramBotContext;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\ServiceProvider;
use Nutgram\Laravel\RunningMode\LaravelWebhook;
use SergiX44\Nutgram\Nutgram;
@@ -44,6 +46,10 @@ class AppServiceProvider extends ServiceProvider
// settings table may not exist yet during migrate
}
if (! $this->app->runningUnitTests()) {
$this->recordLastSql();
}
// Override Nutgram's production safe_mode (md5 APP_KEY) so webhook updates
// are not silently dropped when Telegram secret_token is unset/mismatched.
$this->app->afterResolving(Nutgram::class, function (Nutgram $bot): void {
@@ -62,4 +68,45 @@ class AppServiceProvider extends ServiceProvider
$bot->setRunningMode($webhook);
});
}
/**
* Overwrite last-sql.json before each query so an OOM still leaves the dying SQL.
*/
private function recordLastSql(): void
{
$path = storage_path('logs/last-sql.json');
DB::beforeExecuting(function (string $query, array $bindings) use ($path): void {
$uri = 'cli';
try {
if (app()->runningInConsole()) {
$uri = 'cli:'.implode(' ', $_SERVER['argv'] ?? []);
} else {
$req = request();
$uri = $req->method().' '.$req->getRequestUri();
}
} catch (\Throwable) {
}
@file_put_contents($path, json_encode([
'at' => date('c'),
'mem' => memory_get_usage(true),
'uri' => $uri,
'sql' => substr($query, 0, 2000),
'bindings' => array_slice($bindings, 0, 12),
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
});
DB::listen(function ($event) use ($path): void {
if ($event->time < 500 && memory_get_usage(true) < 64 * 1024 * 1024) {
return;
}
$raw = @file_get_contents($path);
$last = is_string($raw) ? json_decode($raw, true) : null;
Log::warning('slow_or_heavy_query', [
'ms' => $event->time,
'mem' => memory_get_usage(true),
'uri' => is_array($last) ? ($last['uri'] ?? '') : '',
'sql' => substr((string) $event->sql, 0, 500),
]);
});
}
}