Skip to content

Commit bf98321

Browse files
miccehedinmarickvantuil
authored andcommitted
Add support for json credential file
1 parent bf14e38 commit bf98321

File tree

3 files changed

+53
-19
lines changed

3 files changed

+53
-19
lines changed

README.md

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,21 @@ Please check the [Laravel support policy](https://laravel.com/docs/master/releas
5151
// is marked as a DEADLINE_EXCEEDED failure.
5252
'dispatch_deadline' => null,
5353
'backoff' => 0,
54+
55+
'queue' => env('STACKKIT_CLOUD_TASKS_QUEUE', 'default'),
56+
'credential_file' => env('STACKKIT_CLOUD_TASKS_CREDENTIAL_FILE', ''),
57+
// Either you have an AppEngine task, and use these
58+
'app_engine' => env('STACKKIT_APP_ENGINE_TASK', false),
59+
'app_engine_service' => env('STACKKIT_APP_ENGINE_SERVICE', ''),
60+
// Else you get a HttpTask, and use these
61+
'handler' => env('STACKKIT_CLOUD_TASKS_HANDLER', ''),
62+
'service_account_email' => env('STACKKIT_CLOUD_TASKS_SERVICE_EMAIL', ''),
63+
'signed_audience' => env('STACKKIT_CLOUD_TASKS_SIGNED_AUDIENCE', true),
64+
// Optional: The deadline in seconds for requests sent to the worker. If the worker
65+
// does not respond by this deadline then the request is cancelled and the attempt
66+
// is marked as a DEADLINE_EXCEEDED failure.
67+
'dispatch_deadline' => null,
68+
'backoff' => 0,
5469
],
5570
```
5671

@@ -64,13 +79,14 @@ Now that the package is installed, the final step is to set the correct environm
6479

6580
Please check the table below on what the values mean and what their value should be.
6681

67-
| Environment variable | Description |Example
68-
--------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---
69-
| `STACKKIT_CLOUD_TASKS_PROJECT` | The project your queue belongs to. |`my-project`
70-
| `STACKKIT_CLOUD_TASKS_LOCATION` | The region where the project is hosted |`europe-west6`
71-
| `STACKKIT_CLOUD_TASKS_QUEUE` | The default queue a job will be added to |`emails`
72-
| `STACKKIT_CLOUD_TASKS_SERVICE_EMAIL` | The email address of the service account. Important, it should have the correct roles. See the section below which roles. |`[email protected]`
73-
| `STACKKIT_CLOUD_TASKS_HANDLER` (optional) | The URL that Cloud Tasks will call to process a job. This should be the URL to your Laravel app. By default we will use the URL that dispatched the job. |`https://<your website>.com`
82+
| Environment variable | Description |Example
83+
---------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------|---
84+
| `STACKKIT_CLOUD_TASKS_PROJECT` | The project your queue belongs to. |`my-project`
85+
| `STACKKIT_CLOUD_TASKS_LOCATION` | The region where the project is hosted. |`europe-west6`
86+
| `STACKKIT_CLOUD_TASKS_QUEUE` | The default queue a job will be added to. |`emails`
87+
| `STACKKIT_CLOUD_TASKS_CREDENTIAL_FILE` (optional) | A json credential file to authenticate the connection (from outside AppEngine) |`project-123.json`
88+
| `STACKKIT_CLOUD_TASKS_SERVICE_EMAIL` | The email address of the service account. Important, it should have the correct roles. See the section below which roles. |`[email protected]`
89+
| `STACKKIT_CLOUD_TASKS_HANDLER` (optional) | The URL that Cloud Tasks will call to process a job. This should be the URL to your Laravel app. By default we will use the URL that dispatched the job. |`https://<your website>.com`
7490
| `STACKKIT_CLOUD_TASKS_SIGNED_AUDIENCE` (optional) | True or false depending if you want extra security by signing the audience of your tasks. May misbehave in certain Cloud Run setups. Defaults to true. | `true`
7591
</details>
7692
<details>

src/CloudTasksServiceProvider.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,14 @@ public function boot(): void
2929

3030
private function registerClient(): void
3131
{
32-
$this->app->singleton(CloudTasksClient::class, function () {
33-
return new CloudTasksClient();
32+
$this->app->singleton(CloudTasksClient::class, function ($app) {
33+
$config = config('queue.connections.cloudtasks');
34+
$options = [];
35+
$x = $config['credential_file'];
36+
if (!empty($x)) {
37+
$options['credentials'] = $x;
38+
}
39+
return new CloudTasksClient($options);
3440
});
3541

3642
$this->app->bind('open-id-verificator', OpenIdVerificatorConcrete::class);

src/TaskHandler.php

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010
use Illuminate\Queue\Jobs\Job;
1111
use Illuminate\Queue\QueueManager;
1212
use Illuminate\Queue\WorkerOptions;
13+
use Illuminate\Support\Facades\Log;
1314
use Illuminate\Support\Str;
1415
use Illuminate\Validation\ValidationException;
1516
use Safe\Exceptions\JsonException;
1617
use UnexpectedValueException;
18+
1719
use function Safe\json_decode;
1820

1921
class TaskHandler
@@ -45,11 +47,18 @@ public function __construct(CloudTasksClient $client)
4547

4648
public function handle(?string $task = null): void
4749
{
50+
Log::debug(json_encode(request()->headers->all()));
51+
Log::debug(json_encode(request()->bearerToken()));
52+
Log::debug(json_encode(request()->getContent()));
53+
4854
$task = $this->captureTask($task);
55+
Log::debug(json_encode($task));
4956

5057
$this->loadQueueConnectionConfiguration($task);
58+
Log::debug(json_encode($this->config));
5159

5260
$this->setQueue();
61+
Log::debug(json_encode($this->queue));
5362

5463
OpenIdVerificator::verify(request()->bearerToken(), $this->config);
5564

@@ -63,7 +72,7 @@ public function handle(?string $task = null): void
6372
*/
6473
private function captureTask($task): array
6574
{
66-
$task = $task ?: (string) (request()->getContent());
75+
$task = $task ?: (string)(request()->getContent());
6776

6877
try {
6978
$array = json_decode($task, true);
@@ -72,13 +81,13 @@ private function captureTask($task): array
7281
}
7382

7483
$validator = validator([
75-
'json' => $task,
76-
'task' => $array,
84+
'json' => $task,
85+
'task' => $array,
7786
'name_header' => request()->header('X-CloudTasks-Taskname'),
7887
], [
79-
'json' => 'required|json',
80-
'task' => 'required|array',
81-
'task.data' => 'required|array',
88+
'json' => 'required|json',
89+
'task' => 'required|array',
90+
'task.data' => 'required|array',
8291
'name_header' => 'required|string',
8392
]);
8493

@@ -141,8 +150,8 @@ private function handleTask(array $task): void
141150
// we know the job was created using an earlier version of the package. This
142151
// job does not have the attempts tracked internally yet.
143152
$taskRetryCountHeader = request()->header('X-CloudTasks-TaskRetryCount');
144-
if ($taskRetryCountHeader && (int) $taskRetryCountHeader > 0) {
145-
$job->setAttempts((int) $taskRetryCountHeader);
153+
if ($taskRetryCountHeader && (int)$taskRetryCountHeader > 0) {
154+
$job->setAttempts((int)$taskRetryCountHeader);
146155
} else {
147156
$job->setAttempts($task['internal']['attempts']);
148157
}
@@ -173,11 +182,14 @@ private function loadQueueRetryConfig(CloudTasksJob $job): void
173182
public static function getCommandProperties(string $command): array
174183
{
175184
if (Str::startsWith($command, 'O:')) {
176-
return (array) unserialize($command, ['allowed_classes' => false]);
185+
return (array)unserialize($command, ['allowed_classes' => false]);
177186
}
178187

179188
if (app()->bound(Encrypter::class)) {
180-
return (array) unserialize(app(Encrypter::class)->decrypt($command), ['allowed_classes' => ['Illuminate\Support\Carbon']]);
189+
return (array)unserialize(
190+
app(Encrypter::class)->decrypt($command),
191+
['allowed_classes' => ['Illuminate\Support\Carbon']]
192+
);
181193
}
182194

183195
return [];

0 commit comments

Comments
 (0)