Skip to content

Commit 81bf6e5

Browse files
Merge pull request #152 from stackkit/feature/custom-cloud-tasks-client-options
Allow passing custom cloud tasks client options
2 parents 367c8fc + 14a57cd commit 81bf6e5

File tree

5 files changed

+50
-1
lines changed

5 files changed

+50
-1
lines changed

README.md

+21
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,27 @@ CloudTasksQueue::configureWorkerOptionsUsing(function (IncomingTask $task) {
145145
});
146146
```
147147

148+
#### Use a custom credentials file
149+
150+
Modify (or add) the `client_options` key in the `config/cloud-tasks.php` file:
151+
152+
```php
153+
'client_options' => [
154+
'credentials' => '/path/to/credentials.json',
155+
]
156+
```
157+
158+
159+
#### Modify CloudTasksClient options
160+
161+
Modify (or add) the `client_options` key in the `config/cloud-tasks.php` file:
162+
163+
```php
164+
'client_options' => [
165+
// custom options here
166+
]
167+
```
168+
148169
### How it works and differences
149170

150171
Using Cloud Tasks as a Laravel queue driver is fundamentally different than other Laravel queue drivers, like Redis.

config/cloud-tasks.php

+5
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,9 @@
88

99
// If the application only dispatches jobs
1010
'disable_task_handler' => env('CLOUD_TASKS_DISABLE_TASK_HANDLER', false),
11+
12+
// Optionally, pass custom options to the Cloud Tasks API client
13+
'client_options' => [
14+
// 'credentials' => '/path/to/custom/credentials.json',
15+
],
1116
];

src/CloudTasksServiceProvider.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function boot(): void
2626
private function registerClient(): void
2727
{
2828
$this->app->singleton(CloudTasksClient::class, function () {
29-
return new CloudTasksClient();
29+
return new CloudTasksClient(config('cloud-tasks.client_options', []));
3030
});
3131

3232
$this->app->singleton('cloud-tasks.worker', function (Application $app) {

tests/CloudTasksApiTest.php

+18
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,24 @@ protected function setUp(): void
4242

4343
}
4444

45+
#[Test]
46+
public function custom_client_options_can_be_added()
47+
{
48+
// Arrange
49+
config()->set('cloud-tasks.client_options', [
50+
'credentials' => __DIR__.'/Support/gcloud-key-dummy.json',
51+
]);
52+
53+
// Act
54+
$export = var_export(app(CloudTasksClient::class), true);
55+
56+
// Assert
57+
58+
// CloudTasksClient makes it a bit difficult to read its properties, so this will have to do...
59+
$this->assertStringContainsString('[email protected]', $export);
60+
$this->assertStringContainsString('PRIVATE KEY', $export);
61+
}
62+
4563
#[Test]
4664
public function test_create_task()
4765
{

tests/Support/gcloud-key-dummy.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"type": "service_account",
3+
"client_email": "[email protected]",
4+
"private_key": "PRIVATE KEY"
5+
}

0 commit comments

Comments
 (0)