Skip to content
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

Search by tags in pending and completed jobs #1513

Closed
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
56 changes: 28 additions & 28 deletions dist/app.js

Large diffs are not rendered by default.

36 changes: 35 additions & 1 deletion resources/js/screens/recentJobs/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
*/
data() {
return {
tagSearchPhrase: '',
currentSearchPhrase: '',
searchTimeout: null,
ready: false,
loadingNewEntries: false,
hasNewEntries: false,
Expand Down Expand Up @@ -53,7 +56,18 @@
this.page = 1;

this.loadJobs();
},

tagSearchPhrase() {
clearTimeout(this.searchTimeout);
clearInterval(this.interval);

this.searchTimeout = setTimeout(() => {
this.loadJobs();
this.refreshJobsPeriodically();
}, 500);
}

},


Expand All @@ -66,11 +80,21 @@
this.ready = false;
}

this.$http.get(Horizon.basePath + '/api/jobs/' + this.$route.params.type + '?starting_at=' + starting + '&limit=' + this.perPage)
var tagQuery = this.tagSearchPhrase ? '&tag=' + this.tagSearchPhrase + '&' : '';

this.$http.get(Horizon.basePath + '/api/jobs/' + this.$route.params.type + '?starting_at=' + starting + tagQuery + '&limit=' + this.perPage)
.then(response => {

if (!this.$root.autoLoadsNewEntries && refreshing && !response.data.jobs.length) {
return;
}

if (!this.$root.autoLoadsNewEntries && refreshing && this.jobs.length && response.data.jobs[0]?.id !== this.jobs[0]?.id) {

this.hasNewEntries = true;

} else {

this.jobs = response.data.jobs;

this.totalPages = Math.ceil(response.data.total / this.perPage);
Expand Down Expand Up @@ -154,6 +178,16 @@
<h2 class="h6 m-0" v-if="$route.params.type == 'pending'">Pending Jobs</h2>
<h2 class="h6 m-0" v-if="$route.params.type == 'completed'">Completed Jobs</h2>
<h2 class="h6 m-0" v-if="$route.params.type == 'silenced'">Silenced Jobs</h2>

<div class="form-control-with-icon" v-if="$route.params.type !='silenced'">
<div class="icon-wrapper">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" class="icon">
<path fill-rule="evenodd" d="M9 3.5a5.5 5.5 0 100 11 5.5 5.5 0 000-11zM2 9a7 7 0 1112.452 4.391l3.328 3.329a.75.75 0 11-1.06 1.06l-3.329-3.328A7 7 0 012 9z" clip-rule="evenodd" />
</svg>
</div>

<input type="text" class="form-control w-100" v-model="tagSearchPhrase" placeholder="Search Tags">
</div>
</div>

<div v-if="!ready"
Expand Down
3 changes: 3 additions & 0 deletions src/EventMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ trait EventMap
Events\JobPushed::class => [
Listeners\StoreJob::class,
Listeners\StoreMonitoredTags::class,
Listeners\StoreTagsForPendingJob::class,
],

Events\JobReserved::class => [
Expand All @@ -26,6 +27,8 @@ trait EventMap

Events\JobDeleted::class => [
Listeners\MarkJobAsComplete::class,
Listeners\StoreTagsForCompletedJob::class,
Listeners\ForgetJobInPendingTags::class,
Listeners\UpdateJobMetrics::class,
],

Expand Down
60 changes: 52 additions & 8 deletions src/Http/Controllers/CompletedJobsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Http\Request;
use Laravel\Horizon\Contracts\JobRepository;
use Laravel\Horizon\Contracts\TagRepository;

class CompletedJobsController extends Controller
{
Expand All @@ -14,17 +15,25 @@ class CompletedJobsController extends Controller
*/
public $jobs;

/**
* The tag repository implementation.
*
* @var \Laravel\Horizon\Contracts\TagRepository
*/
public $tags;

/**
* Create a new controller instance.
*
* @param \Laravel\Horizon\Contracts\JobRepository $jobs
* @return void
* @param JobRepository $jobs
* @param TagRepository $tags
*/
public function __construct(JobRepository $jobs)
public function __construct(JobRepository $jobs, TagRepository $tags)
{
parent::__construct();

$this->jobs = $jobs;
$this->tags = $tags;
}

/**
Expand All @@ -35,18 +44,53 @@ 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 = $request->query('tag')
? $this->paginateByTag($request, $request->query('tag'))
: $this->paginate($request);

return $job;
})->values();
$total = $request->query('tag')
? $this->tags->count('completed:'.$request->query('tag'))
: $this->jobs->countCompleted();

return [
'jobs' => $jobs,
'total' => $this->jobs->countCompleted(),
'total' => $total,
];
}

/**
* Paginate the completed jobs for the request.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Support\Collection
*/
protected function paginate(Request $request)
{
return $this->jobs->getCompleted($request->query('starting_at') ?: -1)->map(function ($job) {
return $this->decode($job);
});
}

/**
* Paginate the completed jobs for the request and tag.
*
* @param \Illuminate\Http\Request $request
* @param $tag
* @return \Illuminate\Support\Collection
*/
protected function paginateByTag(Request $request, $tag)
{
$jobIds = $this->tags->paginate(
'completed:'.$tag, ($request->query('starting_at') ?: -1) + 1, 50
);

$startingAt = $request->query('starting_at', 0);

return $this->jobs->getJobs($jobIds, $startingAt)->map(function ($job) {
return $this->decode($job);
});
}

/**
* Decode the given job.
*
Expand Down
59 changes: 52 additions & 7 deletions src/Http/Controllers/PendingJobsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Http\Request;
use Laravel\Horizon\Contracts\JobRepository;
use Laravel\Horizon\Contracts\TagRepository;

class PendingJobsController extends Controller
{
Expand All @@ -14,39 +15,83 @@ class PendingJobsController extends Controller
*/
public $jobs;

/**
* The tag repository implementation.
*
* @var \Laravel\Horizon\Contracts\TagRepository
*/
public $tags;

/**
* Create a new controller instance.
*
* @param \Laravel\Horizon\Contracts\JobRepository $jobs
* @param \Laravel\Horizon\Contracts\TagRepository $tags
* @return void
*/
public function __construct(JobRepository $jobs)
public function __construct(JobRepository $jobs, TagRepository $tags)
{
parent::__construct();

$this->jobs = $jobs;
$this->tags = $tags;
}

/**
* Get all of the pending jobs.
* Get all the pending jobs.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function index(Request $request)
{
$jobs = $this->jobs->getPending($request->query('starting_at', -1))->map(function ($job) {
$job->payload = json_decode($job->payload);
$jobs = ! $request->query('tag')
? $this->paginate($request)
: $this->paginateByTag($request, $request->query('tag'));

return $job;
})->values();
$total = $request->query('tag')
? $this->tags->count('pending:'.$request->query('tag'))
: $this->jobs->countPending();

return [
'jobs' => $jobs,
'total' => $this->jobs->countPending(),
'total' => $total,
];
}

/**
* Paginate the pending jobs for the request.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Support\Collection
*/
protected function paginate(Request $request)
{
return $this->jobs->getPending($request->query('starting_at') ?: -1)->map(function ($job) {
return $this->decode($job);
});
}

/**
* Paginate the pending jobs for the request and tag.
*
* @param \Illuminate\Http\Request $request
* @param $tag
* @return \Illuminate\Support\Collection
*/
protected function paginateByTag(Request $request, $tag)
{
$jobIds = $this->tags->paginate(
'pending:'.$tag, ($request->query('starting_at') ?: -1) + 1, 50
);

$startingAt = $request->query('starting_at', 0);

return $this->jobs->getJobs($jobIds, $startingAt)->map(function ($job) {
return $this->decode($job);
});
}

/**
* Decode the given job.
*
Expand Down
44 changes: 44 additions & 0 deletions src/Listeners/ForgetJobInPendingTags.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Laravel\Horizon\Listeners;

use Laravel\Horizon\Contracts\TagRepository;
use Laravel\Horizon\Events\JobDeleted;

class ForgetJobInPendingTags
{
/**
* The tag repository implementation.
*
* @var \Laravel\Horizon\Contracts\TagRepository
*/
public $tags;

/**
* Create a new listener instance.
*
* @param \Laravel\Horizon\Contracts\TagRepository $tags
* @return void
*/
public function __construct(TagRepository $tags)
{
$this->tags = $tags;
}

/**
* Handle the event.
*
* @param \Laravel\Horizon\Events\JobDeleted $event
* @return void
*/
public function handle(JobDeleted $event): void
{
$pendingTags = collect($event->payload->tags())->map(function ($tag) {
return 'pending:'.$tag;
})->all();

if (! empty($pendingTags)) {
$this->tags->forgetJobs($pendingTags, $event->payload->id());
}
}
}
50 changes: 50 additions & 0 deletions src/Listeners/StoreTagsForCompletedJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Laravel\Horizon\Listeners;

use Laravel\Horizon\Contracts\TagRepository;
use Laravel\Horizon\Events\JobDeleted;

class StoreTagsForCompletedJob
{
/**
* The tag repository implementation.
*
* @var \Laravel\Horizon\Contracts\TagRepository
*/
public $tags;

/**
* Create a new listener instance.
*
* @param \Laravel\Horizon\Contracts\TagRepository $tags
* @return void
*/
public function __construct(TagRepository $tags)
{
$this->tags = $tags;
}

/**
* Handle the event.
*
* @param \Laravel\Horizon\Events\JobDeleted $event
* @return void
*/
public function handle(JobDeleted $event): void
{
if ($event->job->hasFailed()) {
return;
}

$tags = collect($event->payload->tags())->map(function ($tag) {
return 'completed:'.$tag;
})->all();

if (! empty($tags)) {
$this->tags->addTemporary(
config('horizon.trim.completed', 2880), $event->payload->id(), $tags
);
}
}
}
Loading