From b6efd1be4f75f234734e06334c99e55d4d2e6f2f Mon Sep 17 00:00:00 2001 From: Tim MacDonald Date: Tue, 24 Oct 2023 17:13:52 +1100 Subject: [PATCH 1/2] Update slow jobs --- .../2023_06_07_000001_create_pulse_tables.php | 8 ++++++-- resources/views/livewire/slow-jobs.blade.php | 6 +++++- src/Queries/SlowJobs.php | 16 +++++++++++++--- src/Recorders/Jobs.php | 9 ++++++--- tests/Feature/Livewire/SlowJobsTest.php | 7 ++++--- 5 files changed, 34 insertions(+), 12 deletions(-) diff --git a/database/migrations/2023_06_07_000001_create_pulse_tables.php b/database/migrations/2023_06_07_000001_create_pulse_tables.php index 840c01c8..79ae0f0b 100644 --- a/database/migrations/2023_06_07_000001_create_pulse_tables.php +++ b/database/migrations/2023_06_07_000001_create_pulse_tables.php @@ -67,7 +67,7 @@ public function up(): void $table->index(['sql_hash']); // slow_queries $table->index([ - 'date', // trim, slow_queries + 'date', // slow_queries, trim 'sql_hash', // slow_queries 'duration', // slow_queries ]); @@ -88,10 +88,14 @@ public function up(): void $table->datetime('processed_at')->nullable(); $table->datetime('failed_at')->nullable(); $table->unsignedInteger('duration')->nullable(); + $table->boolean('slow')->default(false); // TODO: verify this update index. Needs to find job quickly. does attempts have any benefit here? $table->index(['job_uuid']); - $table->index(['date', 'job_hash', 'duration']); // slow_jobs + + $table->index(['date']); // trim + $table->index(['job_hash']); // slow_jobs + $table->index(['slow', 'date', 'job_hash', 'duration']); // slow_jobs $table->index(['queued_at', 'user_id']); // user_usage }); diff --git a/resources/views/livewire/slow-jobs.blade.php b/resources/views/livewire/slow-jobs.blade.php index 41325a1c..0040d0fd 100644 --- a/resources/views/livewire/slow-jobs.blade.php +++ b/resources/views/livewire/slow-jobs.blade.php @@ -43,7 +43,7 @@ class="min-h-full flex flex-col" - @foreach ($slowJobs as $job) + @foreach ($slowJobs->take(100) as $job) @@ -70,6 +70,10 @@ class="min-h-full flex flex-col" @endif + + @if ($slowJobs->count() > 100) +
Limited to 100 entries
+ @endif diff --git a/src/Queries/SlowJobs.php b/src/Queries/SlowJobs.php index 8503c9c9..d9ae6dd8 100644 --- a/src/Queries/SlowJobs.php +++ b/src/Queries/SlowJobs.php @@ -6,6 +6,7 @@ use Carbon\CarbonInterval as Interval; use Illuminate\Config\Repository; use Illuminate\Database\DatabaseManager; +use Illuminate\Database\Query\Builder; use Illuminate\Support\Collection; use Laravel\Pulse\Recorders\Jobs; @@ -35,12 +36,21 @@ public function __invoke(Interval $interval): Collection { $now = new CarbonImmutable; - return $this->connection()->table('pulse_jobs') - ->selectRaw('MAX(`job`) AS `job`, COUNT(*) AS `count`, MAX(`duration`) AS `slowest`') + return $this->connection()->query()->select([ + 'count', + 'slowest', + 'job' => fn (Builder $query) => $query->select('job') + ->from('pulse_jobs', as: 'child') + ->whereRaw('`child`.`job_hash` = `parent`.`job_hash`') + ->limit(1), + ])->fromSub(fn (Builder $query) => $query->selectRaw('`job_hash`, MAX(`duration`) as `slowest`, COUNT(*) as `count`') + ->from('pulse_jobs') + ->where('slow', true) ->where('date', '>', $now->subSeconds((int) $interval->totalSeconds)->toDateTimeString()) - ->where('duration', '>=', $this->config->get('pulse.recorders.'.Jobs::class.'.threshold')) ->groupBy('job_hash') ->orderByDesc('slowest') + ->orderByDesc('count') + ->limit(101), as: 'parent') ->get(); } } diff --git a/src/Recorders/Jobs.php b/src/Recorders/Jobs.php index 13eccba3..05e76225 100644 --- a/src/Recorders/Jobs.php +++ b/src/Recorders/Jobs.php @@ -118,7 +118,8 @@ public function record(JobReleasedAfterException|JobFailed|JobProcessed|JobProce [ 'date' => $now->toDateTimeString(), 'released_at' => $now->toDateTimeString(), - 'duration' => $this->lastJobStartedProcessingAt->diffInMilliseconds($now), // @phpstan-ignore method.nonObject + 'duration' => $duration = $this->lastJobStartedProcessingAt->diffInMilliseconds($now), // @phpstan-ignore method.nonObject + 'slow' => $duration >= $this->config->get('pulse.recorders.'.static::class.'.threshold'), ], ), new Entry($this->table, [ @@ -140,7 +141,8 @@ public function record(JobReleasedAfterException|JobFailed|JobProcessed|JobProce [ 'date' => $now->toDateTimeString(), 'processed_at' => $now->toDateTimeString(), - 'duration' => $this->lastJobStartedProcessingAt->diffInMilliseconds($now), // @phpstan-ignore method.nonObject + 'duration' => $duration = $this->lastJobStartedProcessingAt->diffInMilliseconds($now), // @phpstan-ignore method.nonObject + 'slow' => $duration >= $this->config->get('pulse.recorders.'.static::class.'.threshold'), ], ), fn () => $this->lastJobStartedProcessingAt = null); } @@ -152,7 +154,8 @@ public function record(JobReleasedAfterException|JobFailed|JobProcessed|JobProce [ 'date' => $now->toDateTimeString(), 'failed_at' => $now->toDateTimeString(), - 'duration' => $this->lastJobStartedProcessingAt->diffInMilliseconds($now), // @phpstan-ignore method.nonObject + 'duration' => $duration = $this->lastJobStartedProcessingAt->diffInMilliseconds($now), // @phpstan-ignore method.nonObject + 'slow' => $duration >= $this->config->get('pulse.recorders.'.static::class.'.threshold'), ], ), fn () => $this->lastJobStartedProcessingAt = null); } diff --git a/tests/Feature/Livewire/SlowJobsTest.php b/tests/Feature/Livewire/SlowJobsTest.php index 48917fbe..f457659b 100644 --- a/tests/Feature/Livewire/SlowJobsTest.php +++ b/tests/Feature/Livewire/SlowJobsTest.php @@ -17,9 +17,10 @@ it('renders slow jobs', function () { Pulse::ignore(fn () => DB::table('pulse_jobs')->insert([ - ['date' => '2000-01-02 03:04:05', 'job' => 'App\Jobs\MyJob', 'job_uuid' => Str::uuid(), 'attempt' => 1, 'connection' => 'database', 'queue' => 'default', 'queued_at' => '2000-01-02 03:04:05', 'duration' => 1234], - ['date' => '2000-01-02 03:04:05', 'job' => 'App\Jobs\MyJob', 'job_uuid' => Str::uuid(), 'attempt' => 1, 'connection' => 'database', 'queue' => 'default', 'queued_at' => '2000-01-02 03:04:05', 'duration' => 2468], - ['date' => '2000-01-02 03:04:05', 'job' => 'App\Jobs\MyOtherJob', 'job_uuid' => Str::uuid(), 'attempt' => 1, 'connection' => 'database', 'queue' => 'default', 'queued_at' => '2000-01-02 03:04:05', 'duration' => 1234], + ['date' => '2000-01-02 03:04:05', 'job' => 'App\Jobs\MyJob', 'job_uuid' => Str::uuid(), 'attempt' => 1, 'connection' => 'database', 'queue' => 'default', 'queued_at' => '2000-01-02 03:04:05', 'duration' => 1234, 'slow' => true], + ['date' => '2000-01-02 03:04:05', 'job' => 'App\Jobs\MyJob', 'job_uuid' => Str::uuid(), 'attempt' => 1, 'connection' => 'database', 'queue' => 'default', 'queued_at' => '2000-01-02 03:04:05', 'duration' => 2468, 'slow' => true], + ['date' => '2000-01-02 03:04:05', 'job' => 'App\Jobs\MyOtherJob', 'job_uuid' => Str::uuid(), 'attempt' => 1, 'connection' => 'database', 'queue' => 'default', 'queued_at' => '2000-01-02 03:04:05', 'duration' => 1234, 'slow' => true], + ['date' => '2000-01-02 03:04:05', 'job' => 'App\Jobs\AnotherJob', 'job_uuid' => Str::uuid(), 'attempt' => 1, 'connection' => 'database', 'queue' => 'default', 'queued_at' => '2000-01-02 03:04:05', 'duration' => 900, 'slow' => false], ])); Carbon::setTestNow('2000-01-02 03:04:15'); From 47427a1bca6769f11103a8e74494d30c1256d077 Mon Sep 17 00:00:00 2001 From: timacdonald Date: Tue, 24 Oct 2023 06:16:18 +0000 Subject: [PATCH 2/2] Fix code styling --- src/Queries/SlowJobs.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Queries/SlowJobs.php b/src/Queries/SlowJobs.php index d9ae6dd8..1da51b8a 100644 --- a/src/Queries/SlowJobs.php +++ b/src/Queries/SlowJobs.php @@ -8,7 +8,6 @@ use Illuminate\Database\DatabaseManager; use Illuminate\Database\Query\Builder; use Illuminate\Support\Collection; -use Laravel\Pulse\Recorders\Jobs; /** * @internal