Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7a0f7df

Browse files
[4.x-dev] Can set the HandlerUrl using a Closure callback. If given, the handler url is used from that closure.
1 parent b461e1e commit 7a0f7df

File tree

6 files changed

+15
-110
lines changed

6 files changed

+15
-110
lines changed

‎src/CloudTasksQueue.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,17 @@
2525
class CloudTasksQueue extends LaravelQueue implements QueueContract
2626
{
2727
private Closure | array $headers = [];
28+
private static ?Closure $handlerUrlCallback = null;
2829

2930
public function __construct(public array $config, public CloudTasksClient $client, public $dispatchAfterCommit = false)
3031
{
3132
//
3233
}
34+
35+
public static function configureHandlerUrlUsing(Closure $callback): void
36+
{
37+
static::$handlerUrlCallback = $callback;
38+
}
3339

3440
/**
3541
* Get the size of the queue.
@@ -190,12 +196,6 @@ private function enrichPayloadWithInternalData(
190196
public function addPayloadToTask(array $payload, Task $task, mixed $job): Task
191197
{
192198
$headers = value($this->headers, $payload) ?: [];
193-
if ($job instanceof HasTaskHeaders) {
194-
$headers = [
195-
...$headers,
196-
...$job->taskHeaders(),
197-
];
198-
}
199199

200200
if (!empty($this->config['app_engine'])) {
201201
$path = \Safe\parse_url(route('cloud-tasks.handle-task'), PHP_URL_PATH);
@@ -254,8 +254,8 @@ public function release(CloudTasksJob $job, int $delay = 0): void
254254
/** @param string|object $job */
255255
public function getHandler(mixed $job): string
256256
{
257-
if ($jobinstanceof HasTaskHandlerUrl) {
258-
return $job->taskHandlerUrl();
257+
if (static::$handlerUrlCallback) {
258+
return (static::$handlerUrlCallback)($job);
259259
}
260260

261261
if (empty($this->config['handler'])) {

‎src/HasTaskHandlerUrl.php

Lines changed: 0 additions & 10 deletions
This file was deleted.

‎src/HasTaskHeaders.php

Lines changed: 0 additions & 11 deletions
This file was deleted.

‎tests/QueueTest.php

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@
1616
use Illuminate\Support\Facades\Queue;
1717
use PHPUnit\Framework\Attributes\Test;
1818
use Stackkit\LaravelGoogleCloudTasksQueue\CloudTasksApi;
19+
use Stackkit\LaravelGoogleCloudTasksQueue\CloudTasksQueue;
1920
use Stackkit\LaravelGoogleCloudTasksQueue\Events\JobReleased;
20-
use Tests\Support\CustomHandlerUrlJob;
21-
use Tests\Support\CustomHeadersJob;
2221
use Tests\Support\FailingJob;
2322
use Tests\Support\FailingJobWithExponentialBackoff;
2423
use Tests\Support\JobOutput;
@@ -76,18 +75,21 @@ public function it_posts_to_the_configured_handler_url()
7675
}
7776

7877
#[Test]
79-
public function it_posts_to_the_job_handler_url()
78+
public function it_posts_to_the_callback_handler_url()
8079
{
8180
// Arrange
8281
$this->setConfigValue('handler', 'https://docker.for.mac.localhost:8081');
8382
CloudTasksApi::fake();
83+
CloudTasksQueue::configureHandlerUrlUsing(static fn(SimpleJob $job) => 'https://example.com/api/my-custom-route?job=' . $job->id);
8484

8585
// Act
86-
$this->dispatch(new CustomHandlerUrlJob());
86+
$job = new SimpleJob();
87+
$job->id = 1;
88+
$this->dispatch($job);
8789

8890
// Assert
8991
CloudTasksApi::assertTaskCreated(function (Task $task): bool {
90-
return $task->getHttpRequest()->getUrl() === 'https://example.com/api/my-custom-route';
92+
return $task->getHttpRequest()->getUrl() === 'https://example.com/api/my-custom-route?job=1';
9193
});
9294
}
9395

@@ -510,23 +512,4 @@ public function headers_can_be_added_to_the_task_with_job_context()
510512
return $task->getHttpRequest()->getHeaders()['X-MyHeader'] === SimpleJob::class;
511513
});
512514
}
513-
514-
#[Test]
515-
public function job_headers_can_be_added_to_the_task()
516-
{
517-
// Arrange
518-
CloudTasksApi::fake();
519-
520-
// Act
521-
Queue::connection()->setTaskHeaders([
522-
'X-MyHeader' => 'MyValue',
523-
]);
524-
$this->dispatch((new CustomHeadersJob()));
525-
526-
// Assert
527-
CloudTasksApi::assertTaskCreated(function (Task $task): bool {
528-
$headers = $task->getHttpRequest()->getHeaders();
529-
return $headers['X-MyHeader'] === 'MyValue' && $headers['X-MyJobHeader'] === 'MyJobValue';
530-
});
531-
}
532515
}

‎tests/Support/CustomHandlerUrlJob.php

Lines changed: 0 additions & 27 deletions
This file was deleted.

‎tests/Support/CustomHeadersJob.php

Lines changed: 0 additions & 30 deletions
This file was deleted.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /