Skip to content

Commit

Permalink
Merge pull request #644 from derekmd/dashboard-failed-job-count-confi…
Browse files Browse the repository at this point in the history
…g-filter

[3.0] Allow custom dashboard failed jobs metric period
  • Loading branch information
taylorotwell authored Aug 8, 2019
2 parents 02ddc39 + 806b301 commit ffc53a8
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 4 deletions.
1 change: 1 addition & 0 deletions config/horizon.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@

'trim' => [
'recent' => 60,
'recent_failed' => 10080,
'failed' => 10080,
'monitored' => 10080,
],
Expand Down
4 changes: 2 additions & 2 deletions src/Http/Controllers/DashboardStatsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ public function index()
'processes' => $this->totalProcessCount(),
'queueWithMaxRuntime' => app(MetricsRepository::class)->queueWithMaximumRuntime(),
'queueWithMaxThroughput' => app(MetricsRepository::class)->queueWithMaximumThroughput(),
'failedJobs' => app(JobRepository::class)->countFailed(),
'failedJobs' => app(JobRepository::class)->countRecentlyFailed(),
'recentJobs' => app(JobRepository::class)->countRecent(),
'status' => $this->currentStatus(),
'wait' => collect(app(WaitTimeCalculator::class)->calculate())->take(1),
'periods' => [
'failedJobs' => config('horizon.trim.failed'),
'failedJobs' => config('horizon.trim.recent_failed', config('horizon.trim.recent')),
'recentJobs' => config('horizon.trim.recent'),
],
];
Expand Down
33 changes: 32 additions & 1 deletion src/Repositories/RedisJobRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ class RedisJobRepository implements JobRepository
'exception', 'failed_at', 'completed_at', 'retried_by', 'reserved_at',
];

/**
* The number of minutes until recently failed jobs should be purged.
*
* @var int
*/
public $recentFailedJobExpires;

/**
* The number of minutes until recent jobs should be purged.
*
Expand Down Expand Up @@ -60,6 +67,7 @@ public function __construct(RedisFactory $redis)
$this->redis = $redis;
$this->recentJobExpires = config('horizon.trim.recent', 60);
$this->failedJobExpires = config('horizon.trim.failed', 10080);
$this->recentFailedJobExpires = config('horizon.trim.recent_failed', $this->failedJobExpires);
$this->monitoredJobExpires = config('horizon.trim.monitored', 10080);
}

Expand Down Expand Up @@ -164,17 +172,36 @@ protected function getJobsByType($type, $afterIndex)
/**
* Get the number of jobs in a given type set.
*
* @param string $type
* @return int
*/
protected function countJobsByType($type)
{
$minutes = $type === 'failed_jobs' ? $this->failedJobExpires : $this->recentJobExpires;
$minutes = $this->minutesForType($type);

return $this->connection()->zcount(
$type, '-inf', Chronos::now()->subMinutes($minutes)->getTimestamp() * -1
);
}

/**
* Get the number of minutes to count for a given type set.
*
* @param string $type
* @return int
*/
protected function minutesForType($type)
{
switch ($type) {
case 'failed_jobs':
return $this->failedJobExpires;
case 'recent_failed_jobs':
return $this->recentFailedJobExpires;
default:
return $this->recentJobExpires;
}
}

/**
* Retrieve the jobs with the given IDs.
*
Expand Down Expand Up @@ -445,6 +472,10 @@ public function trimRecentJobs()

$pipe->zremrangebyscore('recent_jobs', $score, '+inf');

if ($this->recentJobExpires !== $this->recentFailedJobExpires) {
$score = Chronos::now()->subMinutes($this->recentFailedJobExpires)->getTimestamp() * -1;
}

$pipe->zremrangebyscore('recent_failed_jobs', $score, '+inf');
});
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Controller/DashboardStatsControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function test_all_stats_are_correctly_returned()
$this->app->instance(MetricsRepository::class, $metrics);

$jobs = Mockery::mock(JobRepository::class);
$jobs->shouldReceive('countFailed')->andReturn(1);
$jobs->shouldReceive('countRecentlyFailed')->andReturn(1);
$jobs->shouldReceive('countRecent')->andReturn(1);
$this->app->instance(JobRepository::class, $jobs);

Expand Down

0 comments on commit ffc53a8

Please sign in to comment.