From 3a1474a398c90d1bf7bc07eff25ef912143452dd Mon Sep 17 00:00:00 2001 From: Trevor Gehman Date: Mon, 18 Sep 2023 11:00:51 -0500 Subject: [PATCH] Add `horizon:reset-metrics` command (#1318) * Add reset-metric artisan command * Be more specific to only delete the lock key * Update ResetMetricsCommand.php * Update RedisMetricsRepository.php * formatting * formatting --------- Co-authored-by: Taylor Otwell --- src/Console/ClearMetricsCommand.php | 36 +++++++++++++++++++++ src/Contracts/MetricsRepository.php | 7 ++++ src/HorizonServiceProvider.php | 1 + src/Repositories/RedisMetricsRepository.php | 28 ++++++++++++++++ 4 files changed, 72 insertions(+) create mode 100644 src/Console/ClearMetricsCommand.php diff --git a/src/Console/ClearMetricsCommand.php b/src/Console/ClearMetricsCommand.php new file mode 100644 index 00000000..45057de7 --- /dev/null +++ b/src/Console/ClearMetricsCommand.php @@ -0,0 +1,36 @@ +reset(); + + $this->info('Metrics cleared successfully.'); + } +} diff --git a/src/Contracts/MetricsRepository.php b/src/Contracts/MetricsRepository.php index c996a1b6..32b10df3 100644 --- a/src/Contracts/MetricsRepository.php +++ b/src/Contracts/MetricsRepository.php @@ -133,4 +133,11 @@ public function acquireWaitTimeMonitorLock(); * @return void */ public function forget($key); + + /** + * Delete all stored metrics information. + * + * @return void + */ + public function clear(); } diff --git a/src/HorizonServiceProvider.php b/src/HorizonServiceProvider.php index a814f82b..6ef421c3 100644 --- a/src/HorizonServiceProvider.php +++ b/src/HorizonServiceProvider.php @@ -110,6 +110,7 @@ protected function registerCommands() if ($this->app->runningInConsole()) { $this->commands([ Console\ClearCommand::class, + Console\ClearMetricsCommand::class, Console\ContinueCommand::class, Console\ContinueSupervisorCommand::class, Console\ForgetFailedCommand::class, diff --git a/src/Repositories/RedisMetricsRepository.php b/src/Repositories/RedisMetricsRepository.php index 9fffe147..486ef276 100644 --- a/src/Repositories/RedisMetricsRepository.php +++ b/src/Repositories/RedisMetricsRepository.php @@ -4,6 +4,7 @@ use Carbon\CarbonImmutable; use Illuminate\Contracts\Redis\Factory as RedisFactory; +use Illuminate\Support\Str; use Laravel\Horizon\Contracts\MetricsRepository; use Laravel\Horizon\Lock; use Laravel\Horizon\LuaScripts; @@ -372,6 +373,33 @@ public function forget($key) $this->connection()->del($key); } + /** + * Delete all stored metrics information. + * + * @return void + */ + public function clear() + { + $this->forget('last_snapshot_at'); + $this->forget('measured_jobs'); + $this->forget('measured_queues'); + $this->forget('metrics:snapshot'); + + foreach (['queue:*', 'job:*', 'snapshot:*'] as $pattern) { + $cursor = null; + + do { + [$cursor, $keys] = $this->connection()->scan( + $cursor, ['match' => config('horizon.prefix').$pattern] + ); + + foreach ($keys as $key) { + $this->forget(Str::after($key, config('horizon.prefix'))); + } + } while ($cursor > 0); + } + } + /** * Get the Redis connection instance. *