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 829f720

Browse files
[4.x-dev] Added "setTaskHeadersUsing()" method to allow a closure to be passed for the task headers. This is the same as the "configureHandlerUrlUsing()" method. Also added some forget methods so these callbacks can be easily cleared if necessary.
1 parent 4cf5598 commit 829f720

File tree

3 files changed

+66
-17
lines changed

3 files changed

+66
-17
lines changed

‎README.md

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -102,26 +102,44 @@ Please check the table below on what the values mean and what their value should
102102

103103
#### Passing headers to a task
104104

105-
You can pass headers to a task by using the `withHeaders` method on the queue connection.
105+
You can pass headers to a task by using the `setTaskHeadersUsing` method on the `CloudTasksQueue` class.
106106

107107
```php
108-
use Illuminate\Queue\Queue;
108+
use Stackkit\LaravelGoogleCloudTasksQueue\CloudTasksQueue;
109109

110-
Queue::connection()->setTaskHeaders([
110+
CloudTasksQueue::setTaskHeadersUsing(static fn() => [
111111
'X-My-Header' => 'My-Value',
112112
]);
113113
```
114114

115-
If necessary, the current job being dispatched is also available:
115+
If necessary, the current payload being dispatched is also available:
116116

117117
```php
118-
use Illuminate\Queue\Queue;
118+
use Stackkit\LaravelGoogleCloudTasksQueue\CloudTasksQueue;
119119

120-
Queue::connection()->setTaskHeaders(fn (array $job) => [
121-
'X-My-Header' => $job['displayName']
120+
CloudTasksQueue::setTaskHeadersUsing(static fn(array $payload) => [
121+
'X-My-Header' => $payload['displayName'],
122122
]);
123123
```
124124

125+
#### Configure task handler url
126+
127+
You can set the handler url for a task by using the `configureHandlerUrlUsing` method on the `CloudTasksQueue` class.
128+
129+
```php
130+
use Stackkit\LaravelGoogleCloudTasksQueue\CloudTasksQueue;
131+
132+
CloudTasksQueue::configureHandlerUrlUsing(static fn() => 'https://example.com/my-url');
133+
```
134+
135+
If necessary, the current job being dispatched is also available:
136+
137+
```php
138+
use Stackkit\LaravelGoogleCloudTasksQueue\CloudTasksQueue;
139+
140+
CloudTasksQueue::configureHandlerUrlUsing(static fn(MyJob $job) => 'https://example.com/my-url/' . $job->something());
141+
```
142+
125143
<details>
126144
<summary>
127145
How it works & Differences

‎src/CloudTasksQueue.php

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424

2525
class CloudTasksQueue extends LaravelQueue implements QueueContract
2626
{
27-
private Closure|array $headers = [];
2827
private static ?Closure $handlerUrlCallback = null;
28+
private static ?Closure $taskHeadersCallback = null;
2929

3030
public function __construct(public array $config, public CloudTasksClient $client, public $dispatchAfterCommit = false)
3131
{
@@ -37,6 +37,21 @@ public static function configureHandlerUrlUsing(Closure $callback): void
3737
static::$handlerUrlCallback = $callback;
3838
}
3939

40+
public static function forgetHandlerUrlCallback(): void
41+
{
42+
self::$handlerUrlCallback = null;
43+
}
44+
45+
public static function setTaskHeadersUsing(Closure $callback): void
46+
{
47+
static::$taskHeadersCallback = $callback;
48+
}
49+
50+
public static function forgetTaskHeadersCallback(): void
51+
{
52+
self::$taskHeadersCallback = null;
53+
}
54+
4055
/**
4156
* Get the size of the queue.
4257
*
@@ -194,7 +209,7 @@ private function enrichPayloadWithInternalData(
194209
/** @param string|object $job */
195210
public function addPayloadToTask(array $payload, Task $task, mixed $job): Task
196211
{
197-
$headers = value($this->headers, $payload) ?: [];
212+
$headers = $this->headers($payload);
198213

199214
if (! empty($this->config['app_engine'])) {
200215
$path = \Safe\parse_url(route('cloud-tasks.handle-task'), PHP_URL_PATH);
@@ -270,8 +285,16 @@ public function getHandler(mixed $job): string
270285
return $handler.'/'.config('cloud-tasks.uri');
271286
}
272287

273-
public function setTaskHeaders(Closure|array $headers): void
288+
/**
289+
* @param array<string, mixed> $payload
290+
* @return array<string, mixed>
291+
*/
292+
private function headers(mixed $payload): array
274293
{
275-
$this->headers = $headers;
294+
if (!static::$taskHeadersCallback) {
295+
return [];
296+
}
297+
298+
return (static::$taskHeadersCallback)($payload);
276299
}
277300
}

‎tests/QueueTest.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Illuminate\Support\Facades\DB;
1515
use Illuminate\Support\Facades\Event;
1616
use Illuminate\Support\Facades\Queue;
17+
use Override;
1718
use PHPUnit\Framework\Attributes\Test;
1819
use Stackkit\LaravelGoogleCloudTasksQueue\CloudTasksApi;
1920
use Stackkit\LaravelGoogleCloudTasksQueue\CloudTasksQueue;
@@ -28,6 +29,15 @@
2829

2930
class QueueTest extends TestCase
3031
{
32+
#[Override]
33+
protected function tearDown(): void
34+
{
35+
parent::tearDown();
36+
37+
CloudTasksQueue::forgetHandlerUrlCallback();
38+
CloudTasksQueue::forgetTaskHeadersCallback();
39+
}
40+
3141
#[Test]
3242
public function a_http_request_with_the_handler_url_is_made()
3343
{
@@ -480,7 +490,7 @@ public function headers_can_be_added_to_the_task()
480490
CloudTasksApi::fake();
481491

482492
// Act
483-
Queue::connection()->setTaskHeaders([
493+
CloudTasksQueue::setTaskHeadersUsing(staticfn() => [
484494
'X-MyHeader' => 'MyValue',
485495
]);
486496

@@ -499,11 +509,9 @@ public function headers_can_be_added_to_the_task_with_job_context()
499509
CloudTasksApi::fake();
500510

501511
// Act
502-
Queue::connection()->setTaskHeaders(function (array $payload) {
503-
return [
504-
'X-MyHeader' => $payload['displayName'],
505-
];
506-
});
512+
CloudTasksQueue::setTaskHeadersUsing(static fn(array $payload) => [
513+
'X-MyHeader' => $payload['displayName'],
514+
]);
507515

508516
$this->dispatch((new SimpleJob()));
509517

0 commit comments

Comments
(0)

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