From 9d56d06352a09d0bc36928b53320d9176ae6b47d Mon Sep 17 00:00:00 2001 From: Andras Bacsai <5845193+andrasbacsai@users.noreply.github.com> Date: Wed, 7 May 2025 22:09:25 +0200 Subject: [PATCH] feat(redis): add scheduled cleanup command for Redis keys and enhance cleanup logic --- app/Console/Commands/CleanupRedis.php | 23 +++++++++++++---------- app/Console/Kernel.php | 1 + 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/app/Console/Commands/CleanupRedis.php b/app/Console/Commands/CleanupRedis.php index e16a82be4..315d1adc7 100644 --- a/app/Console/Commands/CleanupRedis.php +++ b/app/Console/Commands/CleanupRedis.php @@ -13,17 +13,20 @@ class CleanupRedis extends Command public function handle() { - $prefix = config('database.redis.options.prefix'); - - $keys = Redis::connection()->keys('*:laravel*'); - collect($keys)->each(function ($key) use ($prefix) { + $redis = Redis::connection('horizon'); + $keys = $redis->keys('*'); + $prefix = config('horizon.prefix'); + foreach ($keys as $key) { $keyWithoutPrefix = str_replace($prefix, '', $key); - Redis::connection()->del($keyWithoutPrefix); - }); + $type = $redis->command('type', [$keyWithoutPrefix]); - $queueOverlaps = Redis::connection()->keys('*laravel-queue-overlap*'); - collect($queueOverlaps)->each(function ($key) { - Redis::connection()->del($key); - }); + if ($type === 5) { + $data = $redis->command('hgetall', [$keyWithoutPrefix]); + $status = data_get($data, 'status'); + if ($status === 'completed') { + $redis->command('del', [$keyWithoutPrefix]); + } + } + } } } diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index a6f24aaad..1c5d3e70b 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -51,6 +51,7 @@ class Kernel extends ConsoleKernel } // $this->scheduleInstance->job(new CleanupStaleMultiplexedConnections)->hourly(); + $this->scheduleInstance->command('cleanup:redis')->everyTenMinutes(); if (isDev()) { // Instance Jobs