feat: delete

This commit is contained in:
hashbro
2026-09-01 06:19:33 +08:00
parent 4ece122625
commit 5370e3700f
+32 -13
View File
@@ -70,12 +70,16 @@ class AppServiceProvider extends ServiceProvider
} }
/** /**
* Overwrite last-sql.json before each query so an OOM still leaves the dying SQL. * Keep the current process's last SQL in memory. On memory-exhaustion
* shutdown, append it to oom-sql.log (shared last-sql.json is useless
* under many FPM workers).
*/ */
private function recordLastSql(): void private function recordLastSql(): void
{ {
$path = storage_path('logs/last-sql.json'); $oomPath = storage_path('logs/oom-sql.log');
DB::beforeExecuting(function (string $query, array $bindings) use ($path): void { $snap = (object) ['line' => ''];
DB::beforeExecuting(function (string $query, array $bindings) use ($snap): void {
$uri = 'cli'; $uri = 'cli';
try { try {
if (app()->runningInConsole()) { if (app()->runningInConsole()) {
@@ -86,26 +90,41 @@ class AppServiceProvider extends ServiceProvider
} }
} catch (\Throwable) { } catch (\Throwable) {
} }
@file_put_contents($path, json_encode([ $snap->line = json_encode([
'at' => date('c'), 'at' => date('c'),
'pid' => getmypid(),
'mem' => memory_get_usage(true), 'mem' => memory_get_usage(true),
'uri' => $uri, 'uri' => $uri,
'sql' => substr($query, 0, 2000), 'sql' => substr($query, 0, 1500),
'bindings' => array_slice($bindings, 0, 12), 'bindings' => array_slice($bindings, 0, 8),
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)); ], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) ?: '';
}); });
DB::listen(function ($event) use ($path): void { register_shutdown_function(function () use ($oomPath, $snap): void {
if ($event->time < 500 && memory_get_usage(true) < 64 * 1024 * 1024) { $err = error_get_last();
$msg = (string) ($err['message'] ?? '');
if ($msg === '' || ! str_contains($msg, 'Allowed memory size')) {
return;
}
$line = $snap->line !== '' ? $snap->line : json_encode([
'at' => date('c'),
'pid' => getmypid(),
'mem' => memory_get_usage(true),
'uri' => 'unknown',
'sql' => '',
'error' => substr($msg, 0, 240),
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
@file_put_contents($oomPath, $line."\n", FILE_APPEND | LOCK_EX);
});
DB::listen(function ($event): void {
if ($event->time < 800 && memory_get_usage(true) < 80 * 1024 * 1024) {
return; return;
} }
$raw = @file_get_contents($path);
$last = is_string($raw) ? json_decode($raw, true) : null;
Log::warning('slow_or_heavy_query', [ Log::warning('slow_or_heavy_query', [
'ms' => $event->time, 'ms' => $event->time,
'mem' => memory_get_usage(true), 'mem' => memory_get_usage(true),
'uri' => is_array($last) ? ($last['uri'] ?? '') : '', 'sql' => substr((string) $event->sql, 0, 400),
'sql' => substr((string) $event->sql, 0, 500),
]); ]);
}); });
} }