From 3d436e6d72cc711db1debcee406c9f94a7b296a1 Mon Sep 17 00:00:00 2001 From: Tim MacDonald Date: Fri, 1 Sep 2023 14:12:42 +1000 Subject: [PATCH] wip --- src/PulseServiceProvider.php | 8 ++--- tests/Feature/HttpRequestsTest.php | 1 - tests/Feature/JobsTest.php | 54 +++++++++--------------------- 3 files changed, 18 insertions(+), 45 deletions(-) diff --git a/src/PulseServiceProvider.php b/src/PulseServiceProvider.php index e163dda5..9a0d67b8 100644 --- a/src/PulseServiceProvider.php +++ b/src/PulseServiceProvider.php @@ -8,10 +8,8 @@ use Illuminate\Events\Dispatcher; use Illuminate\Foundation\Application; use Illuminate\Foundation\Console\Kernel as ConsoleKernel; -use Illuminate\Queue\Events\JobExceptionOccurred; -use Illuminate\Queue\Events\JobFailed; -use Illuminate\Queue\Events\JobProcessed; use Illuminate\Queue\Events\Looping; +use Illuminate\Queue\Events\WorkerStopping; use Illuminate\Routing\Router; use Illuminate\Support\ServiceProvider; use Illuminate\View\Compilers\BladeCompiler; @@ -122,9 +120,7 @@ protected function listenForEvents(): void // TODO: consider moving this registration to the "Booted" event to ensure, for sure, that our stuff is registered last? $event->listen([ Looping::class, - JobFailed::class, - JobProcessed::class, - JobExceptionOccurred::class, + WorkerStopping::class, ], function ($event) use ($app) { $app[Pulse::class]->store($app[Ingest::class]); }); diff --git a/tests/Feature/HttpRequestsTest.php b/tests/Feature/HttpRequestsTest.php index 688ce5df..d8ac8e5d 100644 --- a/tests/Feature/HttpRequestsTest.php +++ b/tests/Feature/HttpRequestsTest.php @@ -14,7 +14,6 @@ use Illuminate\Support\Facades\Route; use Laravel\Pulse\Facades\Pulse; use Laravel\Pulse\Pulse as PulseInstance; - use function Pest\Laravel\actingAs; use function Pest\Laravel\get; use function Pest\Laravel\post; diff --git a/tests/Feature/JobsTest.php b/tests/Feature/JobsTest.php index eedb0816..5a258a4a 100644 --- a/tests/Feature/JobsTest.php +++ b/tests/Feature/JobsTest.php @@ -10,7 +10,6 @@ use Illuminate\Support\Facades\Queue; use Illuminate\Support\Str; use Laravel\Pulse\Contracts\Ingest; -use Laravel\Pulse\Entries\Entry; use Laravel\Pulse\Facades\Pulse; it('ingests bus dispatched jobs', function () { @@ -80,7 +79,7 @@ 'slow' => 0, 'slowest' => null, ]); -})->only(); +}); it('handles a job throwing exceptions and failing', function () { Config::set('queue.default', 'database'); @@ -111,7 +110,7 @@ */ Carbon::setTestNow('2000-01-02 03:04:10'); - Artisan::call('queue:work', ['--once' => true, '--stop-when-empty' => true]); + Artisan::call('queue:work', ['--max-jobs' => 1, '--stop-when-empty' => true]); expect(Queue::size())->toBe(1); $jobs = Pulse::ignore(fn () => DB::table('pulse_jobs')->get()); @@ -129,7 +128,7 @@ * Work the job for the second time. */ - Artisan::call('queue:work', ['--once' => true, '--stop-when-empty' => true]); + Artisan::call('queue:work', ['--max-jobs' => 1, '--stop-when-empty' => true]); expect(Queue::size())->toBe(1); $jobs = Pulse::ignore(fn () => DB::table('pulse_jobs')->get()); @@ -147,7 +146,7 @@ * Work the job for the third time. */ - Artisan::call('queue:work', ['--once' => true, '--stop-when-empty' => true]); + Artisan::call('queue:work', ['--max-jobs' => 1, '--stop-when-empty' => true]); expect(Queue::size())->toBe(0); $jobs = Pulse::ignore(fn () => DB::table('pulse_jobs')->get()); @@ -160,14 +159,7 @@ 'slow' => 3, 'slowest' => 33, ]); - - // // we expect there to be a single exception in the queue. This would - // // be stored on the next loop or while the command terminates. - expect(Pulse::entries())->toHaveCount(1); - expect(Pulse::entries()[0])->toBeInstanceOf(Entry::class); - expect(Pulse::entries()[0]->table)->toBe('pulse_exceptions'); - Pulse::flushEntries(); -})->only(); +}); it('only remembers the slowest duration', function () { Config::set('queue.default', 'database'); @@ -198,7 +190,7 @@ */ Carbon::setTestNow('2000-01-02 03:04:10'); - Artisan::call('queue:work', ['--once' => true, '--stop-when-empty' => true]); + Artisan::call('queue:work', ['--max-jobs' => 1, '--stop-when-empty' => true]); expect(Queue::size())->toBe(1); $jobs = Pulse::ignore(fn () => DB::table('pulse_jobs')->get()); @@ -216,7 +208,7 @@ * Work the job for the second time. */ - Artisan::call('queue:work', ['--once' => true, '--stop-when-empty' => true]); + Artisan::call('queue:work', ['--max-jobs' => 1, '--stop-when-empty' => true]); expect(Queue::size())->toBe(1); $jobs = Pulse::ignore(fn () => DB::table('pulse_jobs')->get()); @@ -234,7 +226,7 @@ * Work the job for the third time. */ - Artisan::call('queue:work', ['--once' => true, '--stop-when-empty' => true]); + Artisan::call('queue:work', ['--max-jobs' => 1, '--stop-when-empty' => true]); expect(Queue::size())->toBe(0); $jobs = Pulse::ignore(fn () => DB::table('pulse_jobs')->get()); @@ -247,14 +239,7 @@ 'slow' => 3, 'slowest' => 99, ]); - - // // we expect there to be a single exception in the queue. This would - // // be stored on the next loop or while the command terminates. - expect(Pulse::entries())->toHaveCount(1); - expect(Pulse::entries()[0])->toBeInstanceOf(Entry::class); - expect(Pulse::entries()[0]->table)->toBe('pulse_exceptions'); - Pulse::flushEntries(); -})->only(); +}); it('handles a failure and then a successful job', function () { Config::set('queue.default', 'database'); @@ -285,7 +270,7 @@ */ Carbon::setTestNow('2000-01-02 03:04:10'); - Artisan::call('queue:work', ['--once' => true, '--stop-when-empty' => true]); + Artisan::call('queue:work', ['--max-jobs' => 1, '--stop-when-empty' => true]); expect(Queue::size())->toBe(1); $jobs = Pulse::ignore(fn () => DB::table('pulse_jobs')->get()); @@ -303,7 +288,7 @@ * Work the job for the second time. */ - Artisan::call('queue:work', ['--once' => true, '--stop-when-empty' => true]); + Artisan::call('queue:work', ['--max-jobs' => 1, '--stop-when-empty' => true]); expect(Queue::size())->toBe(0); $jobs = Pulse::ignore(fn () => DB::table('pulse_jobs')->get()); @@ -316,7 +301,7 @@ 'slow' => 2, 'slowest' => 99, ]); -})->only(); +}); it('handles a slow successful job', function () { Config::set('queue.default', 'database'); @@ -347,7 +332,7 @@ */ Carbon::setTestNow('2000-01-02 03:04:10'); - Artisan::call('queue:work', ['--once' => true, '--stop-when-empty' => true]); + Artisan::call('queue:work', ['--max-jobs' => 1, '--stop-when-empty' => true]); expect(Queue::size())->toBe(0); $jobs = Pulse::ignore(fn () => DB::table('pulse_jobs')->get()); @@ -360,7 +345,7 @@ 'slow' => 1, 'slowest' => 100, ]); -})->only(); +}); it('handles a job that was manually failed', function () { Config::set('queue.default', 'database'); @@ -391,7 +376,7 @@ */ Carbon::setTestNow('2000-01-02 03:04:10'); - Artisan::call('queue:work', ['--once' => true, '--stop-when-empty' => true]); + Artisan::call('queue:work', ['--max-jobs' => 1, '--stop-when-empty' => true]); expect(Queue::size())->toBe(0); $jobs = Pulse::ignore(fn () => DB::table('pulse_jobs')->get()); @@ -404,14 +389,7 @@ 'slow' => 1, 'slowest' => 100, ]); - - // // we expect there to be a single exception in the queue. This would - // // be stored on the next loop or while the command terminates. - expect(Pulse::entries())->toHaveCount(1); - expect(Pulse::entries()[0])->toBeInstanceOf(Entry::class); - expect(Pulse::entries()[0]->table)->toBe('pulse_exceptions'); - Pulse::flushEntries(); -})->todo(); +}); class MyJob implements ShouldQueue {