Skip to content

Commit

Permalink
Add horizon:reset-metrics command (#1318)
Browse files Browse the repository at this point in the history
* 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 <taylor@laravel.com>
  • Loading branch information
trevorgehman and taylorotwell committed Sep 18, 2023
1 parent f6f80b3 commit 3a1474a
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/Console/ClearMetricsCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Laravel\Horizon\Console;

use Illuminate\Console\Command;
use Laravel\Horizon\Contracts\MetricsRepository;

class ClearMetricsCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'horizon:clear-metrics';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Delete metrics for all jobs and queues';

/**
* Execute the console command.
*
* @param \Laravel\Horizon\Contracts\MetricsRepository $metrics
* @return void
*/
public function handle(MetricsRepository $metrics)
{
$metrics->reset();

$this->info('Metrics cleared successfully.');
}
}
7 changes: 7 additions & 0 deletions src/Contracts/MetricsRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,11 @@ public function acquireWaitTimeMonitorLock();
* @return void
*/
public function forget($key);

/**
* Delete all stored metrics information.
*
* @return void
*/
public function clear();
}
1 change: 1 addition & 0 deletions src/HorizonServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
28 changes: 28 additions & 0 deletions src/Repositories/RedisMetricsRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*
Expand Down

0 comments on commit 3a1474a

Please sign in to comment.