Skip to content

[5.x] Exclude jobs from completed overview #1230

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions config/horizon.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,21 @@
],
],

/*
|--------------------------------------------------------------------------
| Jobs
|--------------------------------------------------------------------------
|
| Here you can configure which jobs to exclude from the completed jobs list.
|
*/

'jobs' => [
'exclude' => [
//
],
],

/*
|--------------------------------------------------------------------------
| Fast Termination
Expand Down
11 changes: 6 additions & 5 deletions src/Contracts/JobRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,34 +31,35 @@ public function totalFailed();
/**
* Get a chunk of recent jobs.
*
* @param string $afterIndex
* @param string|null $afterIndex
* @return \Illuminate\Support\Collection
*/
public function getRecent($afterIndex = null);

/**
* Get a chunk of failed jobs.
*
* @param string $afterIndex
* @param string|null $afterIndex
* @return \Illuminate\Support\Collection
*/
public function getFailed($afterIndex = null);

/**
* Get a chunk of pending jobs.
*
* @param string $afterIndex
* @param string|null $afterIndex
* @return \Illuminate\Support\Collection
*/
public function getPending($afterIndex = null);

/**
* Get a chunk of completed jobs.
*
* @param string $afterIndex
* @param string|null $afterIndex
* @param array $exclude
* @return \Illuminate\Support\Collection
*/
public function getCompleted($afterIndex = null);
public function getCompleted($afterIndex = null, array $exclude = []);

/**
* Get the count of recent jobs.
Expand Down
11 changes: 7 additions & 4 deletions src/Http/Controllers/CompletedJobsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,14 @@ public function __construct(JobRepository $jobs)
*/
public function index(Request $request)
{
$jobs = $this->jobs->getCompleted($request->query('starting_at', -1))->map(function ($job) {
$job->payload = json_decode($job->payload);
$jobs = $this->jobs
->getCompleted($request->query('starting_at', -1), config('horizon.jobs.exclude', []))
->map(function ($job) {
$job->payload = json_decode($job->payload);

return $job;
})->values();
return $job;
})
->values();

return [
'jobs' => $jobs,
Expand Down
32 changes: 24 additions & 8 deletions src/Repositories/RedisJobRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

class RedisJobRepository implements JobRepository
{
const PER_PAGE = 50;

/**
* The Redis connection instance.
*
Expand Down Expand Up @@ -156,11 +158,12 @@ public function getPending($afterIndex = null)
* Get a chunk of completed jobs.
*
* @param string|null $afterIndex
* @param array $exclude
* @return \Illuminate\Support\Collection
*/
public function getCompleted($afterIndex = null)
public function getCompleted($afterIndex = null, array $exclude = [])
{
return $this->getJobsByType('completed_jobs', $afterIndex);
return $this->getJobsByType('completed_jobs', $afterIndex, $exclude);
}

/**
Expand Down Expand Up @@ -217,16 +220,29 @@ public function countRecentlyFailed()
* Get a chunk of jobs from the given type set.
*
* @param string $type
* @param string $afterIndex
* @param string|null $afterIndex
* @param array $exclude
* @return \Illuminate\Support\Collection
*/
protected function getJobsByType($type, $afterIndex)
protected function getJobsByType($type, $afterIndex, array $exclude = [])
{
$afterIndex = $afterIndex === null ? -1 : $afterIndex;

return $this->getJobs($this->connection()->zrange(
$type, $afterIndex + 1, $afterIndex + 50
), $afterIndex + 1);
$jobs = collect();

do {
$jobs = $jobs->merge($newJobs = $this->getJobs($this->connection()->zrange(
$type, $afterIndex + 1, $afterIndex + self::PER_PAGE
), $afterIndex + 1));

$afterIndex = $afterIndex + $newJobs->count();

$jobs = $jobs->reject(function ($job) use ($exclude) {
return in_array($job->name, $exclude);
});
} while ($jobs->count() < self::PER_PAGE && $jobs->count() > 0 && $newJobs->count() > 0);

return $jobs->take(self::PER_PAGE);
}

/**
Expand All @@ -239,7 +255,7 @@ protected function countJobsByType($type)
{
$minutes = $this->minutesForType($type);

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