Skip to content

Commit 8bb520a

Browse files
committed
[4.x-dev] Can use the task handler url from the job, if available. Otherwise it falls back to the configured handler url.
1 parent 0ba0b99 commit 8bb520a

File tree

4 files changed

+74
-13
lines changed

4 files changed

+74
-13
lines changed

src/CloudTasksQueue.php

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ public function push($job, $data = '', $queue = null)
5757
$this->createPayload($job, $queue, $data),
5858
$queue,
5959
null,
60-
function ($payload, $queue) {
61-
return $this->pushRaw($payload, $queue);
60+
function ($payload, $queue) use ($job) {
61+
return $this->pushRaw($payload, $queue, ['job' => $job]);
6262
}
6363
);
6464
}
@@ -73,8 +73,9 @@ function ($payload, $queue) {
7373
public function pushRaw($payload, $queue = null, array $options = [])
7474
{
7575
$delay = ! empty($options['delay']) ? $options['delay'] : 0;
76+
$job = $options['job'] ?? null;
7677

77-
return $this->pushToCloudTasks($queue, $payload, $delay);
78+
return $this->pushToCloudTasks($queue, $payload, $delay, $job);
7879
}
7980

8081
/**
@@ -93,8 +94,8 @@ public function later($delay, $job, $data = '', $queue = null)
9394
$this->createPayload($job, $queue, $data),
9495
$queue,
9596
$delay,
96-
function ($payload, $queue, $delay) {
97-
return $this->pushToCloudTasks($queue, $payload, $delay);
97+
function ($payload, $queue, $delay) use ($job) {
98+
return $this->pushToCloudTasks($queue, $payload, $delay, $job);
9899
}
99100
);
100101
}
@@ -105,9 +106,10 @@ function ($payload, $queue, $delay) {
105106
* @param string|null $queue
106107
* @param string $payload
107108
* @param \DateTimeInterface|\DateInterval|int $delay
109+
* @param string|object $job
108110
* @return string
109111
*/
110-
protected function pushToCloudTasks($queue, $payload, $delay = 0)
112+
protected function pushToCloudTasks($queue, $payload, $delay, mixed $job)
111113
{
112114
$queue = $queue ?: $this->config['queue'];
113115

@@ -122,7 +124,7 @@ protected function pushToCloudTasks($queue, $payload, $delay = 0)
122124
connectionName: $this->getConnectionName(),
123125
);
124126

125-
$this->addPayloadToTask($payload, $task);
127+
$this->addPayloadToTask($payload, $task, $job);
126128

127129
// The deadline for requests sent to the app. If the app does not respond by
128130
// this deadline then the request is cancelled and the attempt is marked as
@@ -184,7 +186,8 @@ private function enrichPayloadWithInternalData(
184186
return $payload;
185187
}
186188

187-
public function addPayloadToTask(array $payload, Task $task): Task
189+
/** @param string|object $job */
190+
public function addPayloadToTask(array $payload, Task $task, mixed $job): Task
188191
{
189192
$headers = value($this->headers, $payload) ?: [];
190193

@@ -206,7 +209,7 @@ public function addPayloadToTask(array $payload, Task $task): Task
206209
$task->setAppEngineHttpRequest($appEngineRequest);
207210
} else {
208211
$httpRequest = new HttpRequest();
209-
$httpRequest->setUrl($this->getHandler());
212+
$httpRequest->setUrl($this->getHandler($job));
210213
$httpRequest->setBody(json_encode($payload));
211214
$httpRequest->setHttpMethod(HttpMethod::POST);
212215
$httpRequest->setHeaders($headers);
@@ -237,13 +240,18 @@ public function release(CloudTasksJob $job, int $delay = 0): void
237240

238241
$payload = $job->getRawBody();
239242

240-
$options = ['delay' => $delay];
243+
$options = ['delay' => $delay, 'job' => $job];
241244

242245
$this->pushRaw($payload, $job->getQueue(), $options);
243246
}
244247

245-
public function getHandler(): string
248+
/** @param string|object $job */
249+
public function getHandler(mixed $job): string
246250
{
251+
if ($job instanceof HasTaskHandlerUrl) {
252+
return $job->taskHandlerUrl();
253+
}
254+
247255
if (empty($this->config['handler'])) {
248256
$this->config['handler'] = request()->getSchemeAndHttpHost();
249257
}

src/HasTaskHandlerUrl.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Stackkit\LaravelGoogleCloudTasksQueue;
6+
7+
interface HasTaskHandlerUrl
8+
{
9+
public function taskHandlerUrl(): string;
10+
}

tests/QueueTest.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
use Illuminate\Support\Facades\Queue;
1717
use PHPUnit\Framework\Attributes\Test;
1818
use Stackkit\LaravelGoogleCloudTasksQueue\CloudTasksApi;
19-
use Stackkit\LaravelGoogleCloudTasksQueue\CloudTasksQueue;
2019
use Stackkit\LaravelGoogleCloudTasksQueue\Events\JobReleased;
20+
use Tests\Support\CustomHandlerUrlJob;
2121
use Tests\Support\FailingJob;
2222
use Tests\Support\FailingJobWithExponentialBackoff;
2323
use Tests\Support\JobOutput;
@@ -59,7 +59,7 @@ public function it_posts_to_the_handler()
5959
}
6060

6161
#[Test]
62-
public function it_posts_to_the_correct_handler_url()
62+
public function it_posts_to_the_configured_handler_url()
6363
{
6464
// Arrange
6565
$this->setConfigValue('handler', 'https://docker.for.mac.localhost:8081');
@@ -74,6 +74,22 @@ public function it_posts_to_the_correct_handler_url()
7474
});
7575
}
7676

77+
#[Test]
78+
public function it_posts_to_the_job_handler_url()
79+
{
80+
// Arrange
81+
$this->setConfigValue('handler', 'https://docker.for.mac.localhost:8081');
82+
CloudTasksApi::fake();
83+
84+
// Act
85+
$this->dispatch(new CustomHandlerUrlJob());
86+
87+
// Assert
88+
CloudTasksApi::assertTaskCreated(function (Task $task): bool {
89+
return $task->getHttpRequest()->getUrl() === 'https://example.com/api/my-custom-route';
90+
});
91+
}
92+
7793
#[Test]
7894
public function it_posts_the_serialized_job_payload_to_the_handler()
7995
{
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Support;
6+
7+
use Illuminate\Bus\Queueable;
8+
use Illuminate\Contracts\Queue\ShouldQueue;
9+
use Illuminate\Foundation\Bus\Dispatchable;
10+
use Illuminate\Queue\InteractsWithQueue;
11+
use Illuminate\Queue\SerializesModels;
12+
use Stackkit\LaravelGoogleCloudTasksQueue\HasTaskHandlerUrl;
13+
14+
class CustomHandlerUrlJob implements ShouldQueue, HasTaskHandlerUrl
15+
{
16+
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
17+
18+
public function handle(): void
19+
{
20+
event(new JobOutput('CustomHandlerUrlJob:success'));
21+
}
22+
23+
public function taskHandlerUrl(): string
24+
{
25+
return 'https://example.com/api/my-custom-route';
26+
}
27+
}

0 commit comments

Comments
 (0)