From 42ba247ebb87171b3f5e0f9fe93ec85ed9cb4b92 Mon Sep 17 00:00:00 2001 From: Marick van Tuil Date: Thu, 18 Jan 2024 21:55:27 +0100 Subject: [PATCH] Drop support for Laravel 6, 7, 8 and 9 --- .github/workflows/run-tests.yml | 11 ------- README.md | 2 +- composer.json | 18 +----------- src/CloudTasksJob.php | 11 ------- src/CloudTasksQueue.php | 33 --------------------- src/Events/JobReleasedAfterException.php | 37 ------------------------ src/TaskHandler.php | 6 ++-- tests/CloudTasksDashboardTest.php | 16 ++++------ tests/QueueTest.php | 28 +++++------------- tests/TaskHandlerTest.php | 34 +++++++--------------- tests/TestCase.php | 12 +------- 11 files changed, 29 insertions(+), 179 deletions(-) delete mode 100644 src/Events/JobReleasedAfterException.php diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index 3ff39e7..f1909b6 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -16,17 +16,6 @@ jobs: - { queue: 'github-actions-laravel10-php83', laravel: '10.*', php: '8.3', 'testbench': '8.*'} - { queue: 'github-actions-laravel10-php82', laravel: '10.*', php: '8.2', 'testbench': '8.*'} - { queue: 'github-actions-laravel10-php81', laravel: '10.*', php: '8.1', 'testbench': '8.*'} - - { queue: 'github-actions-laravel9-php83', laravel: '9.*', php: '8.3', 'testbench': '7.*'} - - { queue: 'github-actions-laravel9-php82', laravel: '9.*', php: '8.2', 'testbench': '7.*'} - - { queue: 'github-actions-laravel9-php81', laravel: '9.*', php: '8.1', 'testbench': '7.*'} - - { queue: 'github-actions-laravel9-php80', laravel: '9.*', php: '8.0', 'testbench': '7.*'} - - { queue: 'github-actions-laravel8-php81', laravel: '8.*', php: '8.1', 'testbench': '6.*'} - - { queue: 'github-actions-laravel8-php80', laravel: '8.*', php: '8.0', 'testbench': '6.*'} - - { queue: 'github-actions-laravel8-php74', laravel: '8.*', php: '7.4', 'testbench': '6.*'} - - { queue: 'github-actions-laravel7-php80', laravel: '7.*', php: '8.0', 'testbench': '5.*' } - - { queue: 'github-actions-laravel7-php74', laravel: '7.*', php: '7.4', 'testbench': '5.*' } - - { queue: 'github-actions-laravel6-php80', laravel: '6.*', php: '8.0', 'testbench': '4.*' } - - { queue: 'github-actions-laravel6-php74', laravel: '6.*', php: '7.4', 'testbench': '4.*' } name: PHP ${{ matrix.payload.php }} - Laravel ${{ matrix.payload.laravel }} - DB ${{ matrix.db }} diff --git a/README.md b/README.md index 23b072e..24bf4e8 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ This package allows Google Cloud Tasks to be used as the queue driver.
- This package requires Laravel 6 or higher and supports MySQL 8 and PostgreSQL 14. Might support older database versions too, but package hasn't been tested for it. + This package requires Laravel 10 or higher and supports MySQL 8 and PostgreSQL 15. Might support older database versions too, but package hasn't been tested for it. Please check the [Laravel support policy](https://laravel.com/docs/master/releases#support-policy) table for supported Laravel and PHP versions. diff --git a/composer.json b/composer.json index cf4f0ba..f4d259e 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,7 @@ "thecodingmachine/safe": "^1.0|^2.0" }, "require-dev": { - "orchestra/testbench": "^4.0 || ^5.0 || ^6.0 || ^7.0 || ^8.0", + "orchestra/testbench": "^8.0", "nunomaduro/larastan": "^1.0 || ^2.0", "thecodingmachine/phpstan-safe-rule": "^1.2", "laravel/legacy-factories": "^1.3" @@ -45,21 +45,5 @@ "composer require laravel/framework:10.* orchestra/testbench:8.* --no-interaction --no-update", "composer update --prefer-stable --prefer-dist --no-interaction" ], - "l9": [ - "composer require laravel/framework:9.* orchestra/testbench:7.* --no-interaction --no-update", - "composer update --prefer-stable --prefer-dist --no-interaction" - ], - "l8": [ - "composer require laravel/framework:8.* orchestra/testbench:6.* --no-interaction --no-update", - "composer update --prefer-stable --prefer-dist --no-interaction" - ], - "l7": [ - "composer require laravel/framework:7.* orchestra/testbench:5.* --no-interaction --no-update", - "composer update --prefer-stable --prefer-dist --no-interaction" - ], - "l6": [ - "composer require laravel/framework:6.* orchestra/testbench:4.* --no-interaction --no-update", - "composer update --prefer-stable --prefer-dist --no-interaction" - ] } } diff --git a/src/CloudTasksJob.php b/src/CloudTasksJob.php index 9e804cf..3cd63bc 100644 --- a/src/CloudTasksJob.php +++ b/src/CloudTasksJob.php @@ -6,7 +6,6 @@ use Illuminate\Contracts\Queue\Job as JobContract; use Illuminate\Queue\Jobs\Job as LaravelJob; use Stackkit\LaravelGoogleCloudTasksQueue\Events\JobReleased; -use Stackkit\LaravelGoogleCloudTasksQueue\Events\JobReleasedAfterException; use function Safe\json_encode; class CloudTasksJob extends LaravelJob implements JobContract @@ -137,16 +136,6 @@ public function release($delay = 0) $properties = TaskHandler::getCommandProperties($this->job['data']['command']); $connection = $properties['connection'] ?? config('queue.default'); - // The package uses the JobReleasedAfterException provided by Laravel to grab - // the payload of the released job in tests to easily run and test a released - // job. Because the event is only accessible in Laravel 9.x, we create an - // identical event to hook into for Laravel versions older than 9.x - if (version_compare(app()->version(), '9.0.0', '<')) { - if (data_get($this->job, 'internal.errored')) { - app('events')->dispatch(new JobReleasedAfterException($connection, $this)); - } - } - if (! data_get($this->job, 'internal.errored')) { app('events')->dispatch(new JobReleased($connection, $this, $delay)); } diff --git a/src/CloudTasksQueue.php b/src/CloudTasksQueue.php index cd8da1d..905522d 100644 --- a/src/CloudTasksQueue.php +++ b/src/CloudTasksQueue.php @@ -45,25 +45,6 @@ public function size($queue = null) return 0; } - /** - * Fallback method for Laravel 6x and 7x - * - * @param \Closure|string|object $job - * @param string $payload - * @param string $queue - * @param \DateTimeInterface|\DateInterval|int|null $delay - * @param callable $callback - * @return mixed - */ - protected function enqueueUsing($job, $payload, $queue, $delay, $callback) - { - if (method_exists(parent::class, 'enqueueUsing')) { - return parent::enqueueUsing($job, $payload, $queue, $delay, $callback); - } - - return $callback($payload, $queue, $delay); - } - /** * Push a new job onto the queue. * @@ -142,11 +123,6 @@ protected function pushToCloudTasks($queue, $payload, $delay = 0) $payload = json_decode($payload, true); - // Laravel 7+ jobs have a uuid, but Laravel 6 doesn't have it. - // Since we are using and expecting the uuid in some places - // we will add it manually here if it's not present yet. - $payload = $this->withUuid($payload); - // Since 3.x tasks are released back onto the queue after an exception has // been thrown. This means we lose the native [X-CloudTasks-TaskRetryCount] header // value and need to manually set and update the number of times a task has been attempted. @@ -183,15 +159,6 @@ protected function pushToCloudTasks($queue, $payload, $delay = 0) return $payload['uuid']; } - private function withUuid(array $payload): array - { - if (!isset($payload['uuid'])) { - $payload['uuid'] = (string) Str::uuid(); - } - - return $payload; - } - private function taskName(string $queueName, array $payload): string { $displayName = $this->sanitizeTaskName($payload['displayName']); diff --git a/src/Events/JobReleasedAfterException.php b/src/Events/JobReleasedAfterException.php deleted file mode 100644 index 603fbe3..0000000 --- a/src/Events/JobReleasedAfterException.php +++ /dev/null @@ -1,37 +0,0 @@ -job = $job; - $this->connectionName = $connectionName; - } -} diff --git a/src/TaskHandler.php b/src/TaskHandler.php index 8be84c3..1a986f8 100644 --- a/src/TaskHandler.php +++ b/src/TaskHandler.php @@ -191,9 +191,9 @@ public function getWorkerOptions(): WorkerOptions { $options = new WorkerOptions(); - $prop = version_compare(app()->version(), '8.0.0', '<') ? 'delay' : 'backoff'; - - $options->$prop = $this->config['backoff'] ?? 0; + if (isset($this->config['backoff'])) { + $options->backoff = $this->config['backoff']; + } return $options; } diff --git a/tests/CloudTasksDashboardTest.php b/tests/CloudTasksDashboardTest.php index 6cb7b99..e01f863 100644 --- a/tests/CloudTasksDashboardTest.php +++ b/tests/CloudTasksDashboardTest.php @@ -487,17 +487,11 @@ public function test_publish() // Act & Assert $expectedPublishBase = dirname(__DIR__); - if (version_compare(app()->version(), '9.0.0', '>=')) { - $this->artisan('vendor:publish --tag=cloud-tasks --force') - ->expectsOutputToContain('Publishing [cloud-tasks] assets.') - ->expectsOutputToContain('Copying file [' . $expectedPublishBase . '/config/cloud-tasks.php] to [config/cloud-tasks.php]') - ->expectsOutputToContain('Copying directory [' . $expectedPublishBase . '/dashboard/dist] to [public/vendor/cloud-tasks]'); - } else { - $this->artisan('vendor:publish --tag=cloud-tasks --force') - ->expectsOutput('Copied File [' . $expectedPublishBase . '/config/cloud-tasks.php] To [/config/cloud-tasks.php]') - ->expectsOutput('Copied Directory [' . $expectedPublishBase . '/dashboard/dist] To [/public/vendor/cloud-tasks]') - ->expectsOutput('Publishing complete.'); - } + $this->artisan('vendor:publish --tag=cloud-tasks --force') + ->expectsOutputToContain('Publishing [cloud-tasks] assets.') + ->expectsOutputToContain('Copying file [' . $expectedPublishBase . '/config/cloud-tasks.php] to [config/cloud-tasks.php]') + ->expectsOutputToContain('Copying directory [' . $expectedPublishBase . '/dashboard/dist] to [public/vendor/cloud-tasks]'); + } /** diff --git a/tests/QueueTest.php b/tests/QueueTest.php index c68bac3..bd5b58a 100644 --- a/tests/QueueTest.php +++ b/tests/QueueTest.php @@ -10,6 +10,7 @@ use Illuminate\Queue\Events\JobProcessed; use Illuminate\Queue\Events\JobProcessing; use Illuminate\Queue\Events\JobQueued; +use Illuminate\Queue\Events\JobReleasedAfterException; use Illuminate\Support\Carbon; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Event; @@ -176,10 +177,6 @@ public function it_posts_the_task_the_correct_queue() */ public function it_can_dispatch_after_commit_inline() { - if (version_compare(app()->version(), '8.0.0', '<')) { - $this->markTestSkipped('Not supported by Laravel 7.x and below.'); - } - // Arrange CloudTasksApi::fake(); Event::fake(); @@ -202,10 +199,6 @@ public function it_can_dispatch_after_commit_inline() */ public function it_can_dispatch_after_commit_through_config() { - if (version_compare(app()->version(), '8.0.0', '<')) { - $this->markTestSkipped('Not supported by Laravel 7.x and below.'); - } - // Arrange CloudTasksApi::fake(); Event::fake(); @@ -233,7 +226,7 @@ public function jobs_can_be_released() CloudTasksApi::fake(); OpenIdVerificator::fake(); Event::fake([ - $this->getJobReleasedAfterExceptionEvent(), + JobReleasedAfterException::class, JobReleased::class, ]); @@ -241,7 +234,7 @@ public function jobs_can_be_released() $this->dispatch(new JobThatWillBeReleased())->run(); // Assert - Event::assertNotDispatched($this->getJobReleasedAfterExceptionEvent()); + Event::assertNotDispatched(JobReleasedAfterException::class); CloudTasksApi::assertDeletedTaskCount(0); // it returned 200 OK so we dont delete it, but Google does $releasedJob = null; Event::assertDispatched(JobReleased::class, function (JobReleased $event) use (&$releasedJob) { @@ -275,7 +268,7 @@ public function jobs_can_be_released_with_a_delay() CloudTasksApi::fake(); OpenIdVerificator::fake(); Event::fake([ - $this->getJobReleasedAfterExceptionEvent(), + JobReleasedAfterException::class, JobReleased::class, ]); Carbon::setTestNow(now()->addDay()); @@ -302,7 +295,7 @@ public function test_default_backoff() // Arrange CloudTasksApi::fake(); OpenIdVerificator::fake(); - Event::fake($this->getJobReleasedAfterExceptionEvent()); + Event::fake(JobReleasedAfterException::class); // Act $this->dispatch(new FailingJob())->run(); @@ -321,7 +314,7 @@ public function test_backoff_from_queue_config() $this->setConfigValue('backoff', 123); CloudTasksApi::fake(); OpenIdVerificator::fake(); - Event::fake($this->getJobReleasedAfterExceptionEvent()); + Event::fake(JobReleasedAfterException::class); // Act $this->dispatch(new FailingJob())->run(); @@ -340,12 +333,11 @@ public function test_backoff_from_job() Carbon::setTestNow(now()->addDay()); CloudTasksApi::fake(); OpenIdVerificator::fake(); - Event::fake($this->getJobReleasedAfterExceptionEvent()); + Event::fake(JobReleasedAfterException::class); // Act $failingJob = new FailingJob(); - $prop = version_compare(app()->version(), '8.0.0', '<') ? 'delay' : 'backoff'; - $failingJob->$prop = 123; + $failingJob->backoff = 123; $this->dispatch($failingJob)->run(); // Assert @@ -358,10 +350,6 @@ public function test_backoff_from_job() /** @test */ public function test_exponential_backoff_from_job_method() { - if (version_compare(app()->version(), '8.0.0', '<')) { - $this->markTestSkipped('Not supported by Laravel 7.x and below.'); - } - // Arrange Carbon::setTestNow(now()->addDay()); CloudTasksApi::fake(); diff --git a/tests/TaskHandlerTest.php b/tests/TaskHandlerTest.php index da2114c..e03e666 100644 --- a/tests/TaskHandlerTest.php +++ b/tests/TaskHandlerTest.php @@ -8,6 +8,7 @@ use Google\Protobuf\Duration; use Illuminate\Queue\Events\JobProcessed; use Illuminate\Queue\Events\JobProcessing; +use Illuminate\Queue\Events\JobReleasedAfterException; use Illuminate\Support\Carbon; use Illuminate\Support\Facades\Event; use Illuminate\Support\Facades\Log; @@ -336,9 +337,6 @@ public function test_unlimited_max_attempts() */ public function test_max_attempts_in_combination_with_retry_until() { - // Laravel 5, 6, 7: check both max_attempts and retry_until before failing a job. - // Laravel 8+: if retry_until, only check that - // Arrange OpenIdVerificator::fake(); CloudTasksApi::partialMock()->shouldReceive('getRetryConfig')->andReturn( @@ -362,15 +360,7 @@ public function test_max_attempts_in_combination_with_retry_until() $releasedJob->run(); # Max attempts was reached - # Laravel 5, 6, 7: fail because max attempts was reached - # Laravel 8+: don't fail because retryUntil has not yet passed. - - if (version_compare(app()->version(), '8.0.0', '<')) { - $this->assertEquals('failed', $task->fresh()->status); - return; - } else { - $this->assertEquals('error', $task->fresh()->status); - } + $this->assertEquals('error', $task->fresh()->status); CloudTasksApi::shouldReceive('getRetryUntilTimestamp')->andReturn(time() - 1); $releasedJob->run(); @@ -383,10 +373,6 @@ public function test_max_attempts_in_combination_with_retry_until() */ public function it_can_handle_encrypted_jobs() { - if (version_compare(app()->version(), '8.0.0', '<')) { - $this->markTestSkipped('Not supported by Laravel 7.x and below.'); - } - // Arrange OpenIdVerificator::fake(); Log::swap(new LogFake()); @@ -414,7 +400,7 @@ public function failing_jobs_are_released() CloudTasksApi::partialMock()->shouldReceive('getRetryConfig')->andReturn( (new RetryConfig())->setMaxAttempts(3) ); - Event::fake($this->getJobReleasedAfterExceptionEvent()); + Event::fake(JobReleasedAfterException::class); // Act $job = $this->dispatch(new FailingJob()); @@ -428,7 +414,7 @@ public function failing_jobs_are_released() CloudTasksApi::assertDeletedTaskCount(1); CloudTasksApi::assertCreatedTaskCount(2); CloudTasksApi::assertTaskDeleted($job->task->getName()); - Event::assertDispatched($this->getJobReleasedAfterExceptionEvent(), function ($event) { + Event::assertDispatched(JobReleasedAfterException::class, function ($event) { return $event->job->attempts() === 1; }); } @@ -440,21 +426,21 @@ public function attempts_are_tracked_internally() { // Arrange OpenIdVerificator::fake(); - Event::fake($this->getJobReleasedAfterExceptionEvent()); + Event::fake(JobReleasedAfterException::class); // Act & Assert $job = $this->dispatch(new FailingJob()); $job->run(); $releasedJob = null; - Event::assertDispatched($this->getJobReleasedAfterExceptionEvent(), function ($event) use (&$releasedJob) { + Event::assertDispatched(JobReleasedAfterException::class, function ($event) use (&$releasedJob) { $releasedJob = $event->job->getRawBody(); return $event->job->attempts() === 1; }); $this->runFromPayload($releasedJob); - Event::assertDispatched($this->getJobReleasedAfterExceptionEvent(), function ($event) { + Event::assertDispatched(JobReleasedAfterException::class, function ($event) { return $event->job->attempts() === 2; }); } @@ -466,14 +452,14 @@ public function attempts_are_copied_from_x_header() { // Arrange OpenIdVerificator::fake(); - Event::fake($this->getJobReleasedAfterExceptionEvent()); + Event::fake(JobReleasedAfterException::class); // Act & Assert $job = $this->dispatch(new FailingJob()); request()->headers->set('X-CloudTasks-TaskRetryCount', 6); $job->run(); - Event::assertDispatched($this->getJobReleasedAfterExceptionEvent(), function ($event) { + Event::assertDispatched(JobReleasedAfterException::class, function ($event) { return $event->job->attempts() === 7; }); } @@ -485,7 +471,7 @@ public function retried_jobs_get_a_new_name() { // Arrange OpenIdVerificator::fake(); - Event::fake($this->getJobReleasedAfterExceptionEvent()); + Event::fake(JobReleasedAfterException::class); CloudTasksApi::fake(); // Act & Assert diff --git a/tests/TestCase.php b/tests/TestCase.php index bbdd1e1..b2c709e 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -11,7 +11,6 @@ use Illuminate\Queue\Events\JobReleasedAfterException; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Event; -use Stackkit\LaravelGoogleCloudTasksQueue\Events\JobReleasedAfterException as PackageJobReleasedAfterException; use Stackkit\LaravelGoogleCloudTasksQueue\Events\TaskCreated; use Stackkit\LaravelGoogleCloudTasksQueue\TaskHandler; @@ -35,7 +34,7 @@ protected function setUp(): void $this->defaultHeaders['Authorization'] = 'Bearer ' . encrypt(time() + 10); Event::listen( - $this->getJobReleasedAfterExceptionEvent(), + JobReleasedAfterException::class, function ($event) { $this->releasedJobPayload = $event->job->getRawBody(); } @@ -239,13 +238,4 @@ protected function assertDatabaseCount($table, int $count, $connection = null) $this->assertEquals($count, DB::connection($connection)->table($table)->count()); } - public function getJobReleasedAfterExceptionEvent(): string - { - // The JobReleasedAfterException event is not available in Laravel versions - // below 9.x so instead for those versions we throw our own event which - // is identical to the Laravel one. - return version_compare(app()->version(), '9.0.0', '<') - ? PackageJobReleasedAfterException::class - : JobReleasedAfterException::class; - } }