Skip to content

Allow passing custom cloud tasks client options #152

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,27 @@ CloudTasksQueue::configureWorkerOptionsUsing(function (IncomingTask $task) {
});
```

#### Use a custom credentials file

Modify (or add) the `client_options` key in the `config/cloud-tasks.php` file:

```php
'client_options' => [
'credentials' => '/path/to/credentials.json',
]
```


#### Modify CloudTasksClient options

Modify (or add) the `client_options` key in the `config/cloud-tasks.php` file:

```php
'client_options' => [
// custom options here
]
```

### How it works and differences

Using Cloud Tasks as a Laravel queue driver is fundamentally different than other Laravel queue drivers, like Redis.
Expand Down
5 changes: 5 additions & 0 deletions config/cloud-tasks.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,9 @@

// If the application only dispatches jobs
'disable_task_handler' => env('CLOUD_TASKS_DISABLE_TASK_HANDLER', false),

// Optionally, pass custom options to the Cloud Tasks API client
'client_options' => [
// 'credentials' => '/path/to/custom/credentials.json',
],
];
2 changes: 1 addition & 1 deletion src/CloudTasksServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function boot(): void
private function registerClient(): void
{
$this->app->singleton(CloudTasksClient::class, function () {
return new CloudTasksClient();
return new CloudTasksClient(config('cloud-tasks.client_options', []));
});

$this->app->singleton('cloud-tasks.worker', function (Application $app) {
Expand Down
18 changes: 18 additions & 0 deletions tests/CloudTasksApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,24 @@ protected function setUp(): void

}

#[Test]
public function custom_client_options_can_be_added()
{
// Arrange
config()->set('cloud-tasks.client_options', [
'credentials' => __DIR__.'/Support/gcloud-key-dummy.json',
]);

// Act
$export = var_export(app(CloudTasksClient::class), true);

// Assert

// CloudTasksClient makes it a bit difficult to read its properties, so this will have to do...
$this->assertStringContainsString('[email protected]', $export);
$this->assertStringContainsString('PRIVATE KEY', $export);
}

#[Test]
public function test_create_task()
{
Expand Down
5 changes: 5 additions & 0 deletions tests/Support/gcloud-key-dummy.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"type": "service_account",
"client_email": "[email protected]",
"private_key": "PRIVATE KEY"
}