Skip to content

Commit a5f5843

Browse files
committed
Add fallback JobReleasedAfterException event
1 parent 251b9d8 commit a5f5843

File tree

6 files changed

+91
-16
lines changed

6 files changed

+91
-16
lines changed

composer.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,23 @@
3636
"Stackkit\\LaravelGoogleCloudTasksQueue\\CloudTasksServiceProvider"
3737
]
3838
}
39+
},
40+
"scripts": {
41+
"l9": [
42+
"composer require laravel/framework:9.* orchestra/testbench:7.* --no-interaction --no-update",
43+
"composer update --prefer-stable --prefer-dist --no-interaction --no-suggest"
44+
],
45+
"l8": [
46+
"composer require laravel/framework:8.* orchestra/testbench:6.* --no-interaction --no-update",
47+
"composer update --prefer-stable --prefer-dist --no-interaction --no-suggest"
48+
],
49+
"l7": [
50+
"composer require laravel/framework:7.* orchestra/testbench:5.* --no-interaction --no-update",
51+
"composer update --prefer-stable --prefer-dist --no-interaction --no-suggest"
52+
],
53+
"l6": [
54+
"composer require laravel/framework:6.* orchestra/testbench:4.* --no-interaction --no-update",
55+
"composer update --prefer-stable --prefer-dist --no-interaction --no-suggest"
56+
]
3957
}
4058
}

src/CloudTasksJob.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,5 +111,17 @@ public function release($delay = 0)
111111
parent::release();
112112

113113
$this->cloudTasksQueue->release($this, $delay);
114+
115+
// The package uses the JobReleasedAfterException provided by Laravel to grab
116+
// the payload of the released job in tests to easily run and test a released
117+
// job. Because the event is only accessible in Laravel 9.x, we create an
118+
// identical event to hook into for Laravel versions older than 9.x
119+
if (version_compare(app()->version(), '9.0.0', '<')) {
120+
$properties = TaskHandler::getCommandProperties($this->job['data']['command']);
121+
122+
$connection = $properties['connection'] ?? config('queue.default');
123+
124+
app('events')->dispatch(new JobReleasedAfterException($connection, $this));
125+
}
114126
}
115127
}

src/JobReleasedAfterException.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Stackkit\LaravelGoogleCloudTasksQueue;
6+
7+
use Illuminate\Contracts\Queue\Job;
8+
9+
class JobReleasedAfterException
10+
{
11+
/**
12+
* The connection name.
13+
*
14+
* @var string
15+
*/
16+
public string $connectionName;
17+
18+
/**
19+
* The job instance.
20+
*
21+
* @var Job
22+
*/
23+
public Job $job;
24+
25+
/**
26+
* Create a new event instance.
27+
*
28+
* @param string $connectionName
29+
* @param Job $job
30+
* @return void
31+
*/
32+
public function __construct(string $connectionName, Job $job)
33+
{
34+
$this->job = $job;
35+
$this->connectionName = $connectionName;
36+
}
37+
}

tests/QueueTest.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,9 @@ public function it_can_dispatch_after_commit_inline()
176176
DB::beginTransaction();
177177
SimpleJob::dispatch()->afterCommit();
178178
Event::assertNotDispatched(JobQueued::class);
179-
DB::commit();
179+
while (DB::transactionLevel() !== 0) {
180+
DB::commit();
181+
}
180182
Event::assertDispatched(JobQueued::class, function (JobQueued $event) {
181183
return $event->job instanceof SimpleJob;
182184
});
@@ -201,7 +203,9 @@ public function it_can_dispatch_after_commit_through_config()
201203
DB::beginTransaction();
202204
SimpleJob::dispatch();
203205
Event::assertNotDispatched(JobQueued::class);
204-
DB::commit();
206+
while (DB::transactionLevel() !== 0) {
207+
DB::commit();
208+
}
205209
Event::assertDispatched(JobQueued::class, function (JobQueued $event) {
206210
return $event->job instanceof SimpleJob;
207211
});

tests/TaskHandlerTest.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,14 @@
77
use Google\Protobuf\Duration;
88
use Illuminate\Queue\Events\JobProcessed;
99
use Illuminate\Queue\Events\JobProcessing;
10-
use Illuminate\Queue\Events\JobReleasedAfterException;
1110
use Illuminate\Support\Facades\Event;
1211
use Illuminate\Support\Facades\Log;
13-
use Illuminate\Validation\ValidationException;
1412
use Stackkit\LaravelGoogleCloudTasksQueue\CloudTasksApi;
1513
use Stackkit\LaravelGoogleCloudTasksQueue\CloudTasksException;
1614
use Stackkit\LaravelGoogleCloudTasksQueue\LogFake;
1715
use Stackkit\LaravelGoogleCloudTasksQueue\OpenIdVerificator;
1816
use Stackkit\LaravelGoogleCloudTasksQueue\StackkitCloudTask;
1917
use Stackkit\LaravelGoogleCloudTasksQueue\TaskHandler;
20-
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
2118
use Tests\Support\EncryptedJob;
2219
use Tests\Support\FailingJob;
2320
use Tests\Support\SimpleJob;
@@ -417,7 +414,7 @@ public function failing_jobs_are_released()
417414
CloudTasksApi::partialMock()->shouldReceive('getRetryConfig')->andReturn(
418415
(new RetryConfig())->setMaxAttempts(3)
419416
);
420-
Event::fake([JobReleasedAfterException::class]);
417+
Event::fake($this->getJobReleasedAfterExceptionEvent());
421418

422419
// Act
423420
$job = $this->dispatch(new FailingJob());
@@ -431,7 +428,7 @@ public function failing_jobs_are_released()
431428
CloudTasksApi::assertDeletedTaskCount(1);
432429
CloudTasksApi::assertCreatedTaskCount(2);
433430
CloudTasksApi::assertTaskDeleted($job->task->getName());
434-
Event::assertDispatched(JobReleasedAfterException::class, function ($event) {
431+
Event::assertDispatched($this->getJobReleasedAfterExceptionEvent(), function ($event) {
435432
return $event->job->attempts() === 1;
436433
});
437434
}
@@ -444,21 +441,21 @@ public function attempts_are_tracked_internally()
444441
// Arrange
445442
CloudTasksApi::fake();
446443
OpenIdVerificator::fake();
447-
Event::fake([JobReleasedAfterException::class]);
444+
Event::fake($this->getJobReleasedAfterExceptionEvent());
448445

449446
// Act & Assert
450447
$job = $this->dispatch(new FailingJob());
451448
$job->run();
452449
$releasedJob = null;
453450

454-
Event::assertDispatched(JobReleasedAfterException::class, function ($event) use (&$releasedJob) {
451+
Event::assertDispatched($this->getJobReleasedAfterExceptionEvent(), function ($event) use (&$releasedJob) {
455452
$releasedJob = $event->job->getRawBody();
456453
return $event->job->attempts() === 1;
457454
});
458455

459456
$this->runFromPayload($releasedJob);
460457

461-
Event::assertDispatched(JobReleasedAfterException::class, function ($event) {
458+
Event::assertDispatched($this->getJobReleasedAfterExceptionEvent(), function ($event) {
462459
return $event->job->attempts() === 2;
463460
});
464461
}

tests/TestCase.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,13 @@
55
use Closure;
66
use Firebase\JWT\JWT;
77
use Google\ApiCore\ApiException;
8-
use Google\Cloud\Tasks\V2\Queue;
9-
use Google\Cloud\Tasks\V2\RetryConfig;
108
use Google\Cloud\Tasks\V2\Task;
119
use Illuminate\Foundation\Testing\DatabaseTransactions;
1210
use Illuminate\Queue\Events\JobReleasedAfterException;
1311
use Illuminate\Support\Facades\DB;
1412
use Google\Cloud\Tasks\V2\CloudTasksClient;
1513
use Illuminate\Support\Facades\Event;
16-
use Mockery;
17-
use Stackkit\LaravelGoogleCloudTasksQueue\CloudTasksJob;
14+
use Stackkit\LaravelGoogleCloudTasksQueue\JobReleasedAfterException as PackageJobReleasedAfterException;
1815
use Stackkit\LaravelGoogleCloudTasksQueue\TaskCreated;
1916
use Stackkit\LaravelGoogleCloudTasksQueue\TaskHandler;
2017

@@ -38,8 +35,8 @@ protected function setUp(): void
3835
$this->defaultHeaders['Authorization'] = 'Bearer ' . encrypt(time() + 10);
3936

4037
Event::listen(
41-
JobReleasedAfterException::class,
42-
function (JobReleasedAfterException $event) {
38+
$this->getJobReleasedAfterExceptionEvent(),
39+
function ($event) {
4340
$this->releasedJobPayload = $event->job->getRawBody();
4441
}
4542
);
@@ -238,4 +235,14 @@ protected function assertDatabaseCount($table, int $count, $connection = null)
238235
{
239236
$this->assertEquals($count, DB::connection($connection)->table($table)->count());
240237
}
238+
239+
public function getJobReleasedAfterExceptionEvent(): string
240+
{
241+
// The JobReleasedAfterException event is not available in Laravel versions
242+
// below 9.x so instead for those versions we throw our own event which
243+
// is identical to the Laravel one.
244+
return version_compare(app()->version(), '9.0.0', '<')
245+
? PackageJobReleasedAfterException::class
246+
: JobReleasedAfterException::class;
247+
}
241248
}

0 commit comments

Comments
 (0)