llmbackend/app/Console/Commands/CleanupExpiredTokens.php
2024-12-04 12:55:07 +08:00

35 lines
1 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Console\Commands;
use App\Models\AuthToken;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
class CleanupExpiredTokens extends Command
{
protected $signature = 'auth:cleanup-expired-tokens';
protected $description = 'Clean up expired auth tokens from the database';
public function handle(): void
{
try {
$count = AuthToken::where('expires_at', '<', now())->delete();
if ($count > 0) {
Log::info("Cleaned up {$count} expired auth tokens");
$this->info("Successfully cleaned up {$count} expired auth tokens.");
} else {
$this->info('No expired auth tokens found.');
}
} catch (\Exception $e) {
Log::error('Error cleaning up expired auth tokens', [
'error' => $e->getMessage(),
'trace' => $e->getTraceAsString(),
]);
$this->error('Failed to clean up expired auth tokens.');
}
}
}