Skip to content

Commit b461e1e

Browse files
committed
[4.x-dev] A job can also have task headers, which are merged with any other headers.
1 parent 8bb520a commit b461e1e

File tree

4 files changed

+67
-0
lines changed

4 files changed

+67
-0
lines changed

src/CloudTasksQueue.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,12 @@ private function enrichPayloadWithInternalData(
190190
public function addPayloadToTask(array $payload, Task $task, mixed $job): Task
191191
{
192192
$headers = value($this->headers, $payload) ?: [];
193+
if ($job instanceof HasTaskHeaders) {
194+
$headers = [
195+
...$headers,
196+
...$job->taskHeaders(),
197+
];
198+
}
193199

194200
if (!empty($this->config['app_engine'])) {
195201
$path = \Safe\parse_url(route('cloud-tasks.handle-task'), PHP_URL_PATH);

src/HasTaskHeaders.php

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

tests/QueueTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Stackkit\LaravelGoogleCloudTasksQueue\CloudTasksApi;
1919
use Stackkit\LaravelGoogleCloudTasksQueue\Events\JobReleased;
2020
use Tests\Support\CustomHandlerUrlJob;
21+
use Tests\Support\CustomHeadersJob;
2122
use Tests\Support\FailingJob;
2223
use Tests\Support\FailingJobWithExponentialBackoff;
2324
use Tests\Support\JobOutput;
@@ -509,4 +510,23 @@ public function headers_can_be_added_to_the_task_with_job_context()
509510
return $task->getHttpRequest()->getHeaders()['X-MyHeader'] === SimpleJob::class;
510511
});
511512
}
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+
}
512532
}

tests/Support/CustomHeadersJob.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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\HasTaskHeaders;
13+
14+
class CustomHeadersJob implements ShouldQueue, HasTaskHeaders
15+
{
16+
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
17+
18+
public function handle(): void
19+
{
20+
event(new JobOutput('CustomHandlerUrlJob:success'));
21+
}
22+
23+
/** @inheritdoc */
24+
public function taskHeaders(): array
25+
{
26+
return [
27+
'X-MyJobHeader' => 'MyJobValue',
28+
];
29+
}
30+
}

0 commit comments

Comments
 (0)