Skip to content

Commit

Permalink
Initial release
Browse files Browse the repository at this point in the history
  • Loading branch information
allantatter committed Sep 20, 2022
0 parents commit cad1624
Show file tree
Hide file tree
Showing 9 changed files with 354 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 Outl1ne <info@outl1ne.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Laravel Google Cloud Queues

[![Latest Version on Packagist](https://img.shields.io/packagist/v/outl1ne/laravel-google-cloud-queues.svg?style=flat-square)](https://packagist.org/packages/outl1ne/laravel-google-cloud-queues)
[![Total Downloads](https://img.shields.io/packagist/dt/outl1ne/laravel-google-cloud-queues.svg?style=flat-square)](https://packagist.org/packages/outl1ne/laravel-google-cloud-queues)

Manage Google Cloud queues from Laravel app. Updates queues on Google Cloud depending on the configuration file in a Laravel app.

## Installation

Install the package in a Laravel project via Composer:

```
composer require outl1ne/laravel-google-cloud-queues
```

## Credits

- [Allan Tatter](https://github.com/allantatter)

## License

Laravel Google Cloud Queues is open-sourced software licensed under the [MIT license](LICENSE.md).
31 changes: 31 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "outl1ne/laravel-google-cloud-queues",
"description": "Manage Google Cloud queues from Laravel app.",
"keywords": [
"laravel",
"outl1ne"
],
"license": "MIT",
"require": {
"laravel/framework": "^6.0 || ^7.0 || ^8.0 || ^9.0",
"phpseclib/phpseclib": "~2.0",
"google/cloud-tasks": "^1.10"
},
"autoload": {
"psr-4": {
"Outl1ne\\LaravelGoogleCloudQueues\\": "src/"
}
},
"extra": {
"laravel": {
"providers": [
"Outl1ne\\LaravelGoogleCloudQueues\\ServiceProvider"
]
}
},
"config": {
"sort-packages": true
},
"minimum-stability": "dev",
"prefer-stable": true
}
42 changes: 42 additions & 0 deletions config/google-cloud-queues.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

return [
'project_id' => env('GOOGLE_CLOUD_PROJECT_ID', 'your-project-id'),
'key_file_path' => env('GOOGLE_CLOUD_KEY_FILE', null), // optional: /path/to/service-account.json
'location' => env('GOOGLE_CLOUD_QUEUES_LOCATION', 'europe-west6'),
'queue_prefix' => env('GOOGLE_CLOUD_QUEUES_PREFIX', 'google-cloud-queue--'),

'rate_limits' => [
'max_burst_size' => 10,
'max_concurrent_dispatches' => 10,
'max_displatches_per_second' => 10,
],
'retry_config' => [
'max_attempts' => 5,
'max_retry_duration' => 0,
'min_backoff' => 1,
'max_backoff' => 900,
'max_doublings' => 16,
],

'queues' => [
[
'name' => 'queue1',
// 'rate_limits' => [
// 'max_burst_size' => 10,
// 'max_concurrent_dispatches' => 75,
// 'max_displatches_per_second' => 20,
// ],
// 'retry_config' => [
// 'max_attempts' => 5,
// 'max_retry_duration' => 0,
// 'min_backoff' => 1,
// 'max_backoff' => 3700,
// 'max_doublings' => 16,
// ],
],
[
'name' => 'queue2',
],
],
];
71 changes: 71 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace Outl1ne\LaravelGoogleCloudQueues;

use Google\Cloud\Tasks\V2\CloudTasksClient;
use Google\Cloud\Tasks\V2\Queue;
use Google\Cloud\Tasks\V2\RateLimits;
use Google\Cloud\Tasks\V2\RetryConfig;
use Google\Protobuf\Duration;

class Client
{
/**
* @var CloudTasksClient
*/
protected $client;

protected array $config;
protected string $locationName;

public function __construct(array $config, CloudTasksClient $client)
{
$this->client = $client;
$this->config = $config;
$this->locationName = $this->client::locationName($this->config['project_id'], $this->config['location']);
}

public function getQueues()
{
$pagedListResponse = $this->client->listQueues($this->locationName);
$queues = [];

foreach ($pagedListResponse->iteratePages() as $page) {
foreach ($page as $queue) {
$queues[] = $queue;
}
}

return $queues;
}

public function createQueue($queueName)
{
$queue = new Queue([
'name' => $this->client::queueName($this->config['project_id'], $this->config['location'], $queueName),
]);
$this->client->createQueue($this->locationName, $queue);
}

public function updateQueue($queueName, $options)
{
$queue = new Queue;
$queue->setName($this->client::queueName($this->config['project_id'], $this->config['location'], $queueName));

$rateLimits = new RateLimits;
$rateLimits->setMaxBurstSize($options['rate_limits']['max_burst_size']);
$rateLimits->setMaxConcurrentDispatches($options['rate_limits']['max_concurrent_dispatches']);
$rateLimits->setMaxDispatchesPerSecond($options['rate_limits']['max_displatches_per_second']);
$queue->setRateLimits($rateLimits);

$retryConfig = new RetryConfig;
$retryConfig->setMaxAttempts($options['retry_config']['max_attempts']);
$retryConfig->setMaxRetryDuration((new Duration())->setSeconds($options['retry_config']['max_retry_duration']));
$retryConfig->setMinBackoff((new Duration())->setSeconds($options['retry_config']['min_backoff']));
$retryConfig->setMaxBackoff((new Duration())->setSeconds($options['retry_config']['max_backoff']));
$retryConfig->setMaxDoublings($options['retry_config']['max_doublings']);
$queue->setRetryConfig($retryConfig);

$this->client->updateQueue($queue);
}
}
19 changes: 19 additions & 0 deletions src/Commands/QueueSync.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Outl1ne\LaravelGoogleCloudQueues\Commands;

use Illuminate\Console\Command;
use Outl1ne\LaravelGoogleCloudQueues\Queues;

class QueueSync extends Command
{
protected $signature = 'gcloud-queues:sync';
protected $description = 'Update queues in Google Cloud.';

public function handle()
{
$this->info('Starting to update queues in Google Cloud.');
(new Queues)->sync();
$this->info('Queue sync done.');
}
}
35 changes: 35 additions & 0 deletions src/Queue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Outl1ne\LaravelGoogleCloudQueues;

use Google\Cloud\Tasks\V2\CloudTasksClient;

class Queue
{
protected array $queue;
protected array $config;

public function __construct($queueConfig, $config)
{
$this->queue = $this->parse($queueConfig);
$this->config = $config;
}

protected function parse($queueConfig)
{
return $queueConfig;
}

public function getFinalName()
{
return $this->config['queue_prefix']. $this->queue['name'];
}

public function getQueueConfig()
{
return [
'rate_limits' => array_merge($this->config['rate_limits'] ?? [], $this->queue['rate_limits'] ?? []),
'retry_config' => array_merge($this->config['retry_config'] ?? [], $this->queue['retry_config'] ?? []),
];
}
}
76 changes: 76 additions & 0 deletions src/Queues.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace Outl1ne\LaravelGoogleCloudQueues;

use Google\Cloud\Tasks\V2\CloudTasksClient;

class Queues
{
/**
* @var Client
*/
protected $client;

protected $queues;
protected $config;

public function __construct()
{
$this->client = new Client(config('google-cloud-queues'), new CloudTasksClient());
$this->queues = $this->parseQueues(config('google-cloud-queues'), config('google-cloud-queues.queues'));
}

protected function parseQueues($config, $queuesConfig)
{
return collect($queuesConfig)->map(fn($queueConfig) => new Queue($queueConfig, $config));
}

public function sync()
{
$googleCloudQueues = $this->client->getQueues();

$this->createMissingQueues($googleCloudQueues);
$this->updateQueues();
}

protected function createMissingQueues($googleCloudQueues)
{
$missingQueues = $this->getMissingQueueNames(
$this->queues,
$this->getExistingQueueNames($googleCloudQueues),
);

foreach ($missingQueues as $missingQueue) {
$this->client->createQueue($missingQueue->getFinalName());
}
}

protected function updateQueues()
{
foreach ($this->queues as $queue) {
$this->updateQueue($queue);
}
}

protected function updateQueue($queue)
{
$this->client->updateQueue($queue->getFinalName(), $queue->getQueueConfig());
}

protected function getExistingQueueNames($queues)
{
$googleCloudQueueNames = [];

foreach ($queues as $queue) {
$googleCloudQueueNames[] = end(explode('/', $queue->getName()));
}

return $googleCloudQueueNames;
}

protected function getMissingQueueNames($configuredQueues, $existingQueueNames)
{
return $configuredQueues
->filter(fn($configuredQueue) => !in_array($configuredQueue->getFinalName(), $existingQueueNames));
}
}
37 changes: 37 additions & 0 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Outl1ne\LaravelGoogleCloudQueues;

use Illuminate\Support\Facades\Route;

class ServiceProvider extends \Illuminate\Support\ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
$this->mergeConfigFrom(__DIR__ . '/../config/google-cloud-queues.php', 'google-cloud-queues');

$this->commands([
Commands\QueueSync::class
]);
}

/**
* Perform post-registration booting of services.
*
* @return void
*/
public function boot()
{
if ($this->app->runningInConsole()) {
// Publish config
$this->publishes([
__DIR__ . '/../config/' => config_path(),
], 'config');
}
}
}

0 comments on commit cad1624

Please sign in to comment.