Skip to content

Commit

Permalink
Merge pull request #152 from stackkit/feature/custom-cloud-tasks-clie…
Browse files Browse the repository at this point in the history
…nt-options

Allow passing custom cloud tasks client options
  • Loading branch information
marickvantuil authored Aug 16, 2024
2 parents 367c8fc + 14a57cd commit 81bf6e5
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 1 deletion.
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"
}

0 comments on commit 81bf6e5

Please sign in to comment.