-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
548 additions
and
0 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
## Purpose and approach | ||
|
||
- Describe the problem or feature in addition to a link to the issues. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
name: Tests | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
pull_request: ~ | ||
|
||
jobs: | ||
build: | ||
name: Test Suite | ||
runs-on: ubuntu-20.04 | ||
|
||
steps: | ||
- uses: actions/checkout@v2 | ||
|
||
- name: Setup PHP, with composer and extensions | ||
uses: shivammathur/setup-php@v2 | ||
with: | ||
php-version: 8.0 | ||
extensions: mbstring, xml, hash, ctype, iconv, curl | ||
|
||
- name: Get composer cache directory | ||
id: composer-cache | ||
run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT | ||
|
||
- name: Cache composer dependencies | ||
uses: actions/cache@v1 | ||
with: | ||
path: ${{ steps.composer-cache.outputs.dir }} | ||
key: composer-${{ hashFiles('composer.lock') }} | ||
restore-keys: composer- | ||
|
||
- name: Install Composer dependencies | ||
run: composer install -n | ||
|
||
- uses: symfonycorp/security-checker-action@v4 | ||
|
||
- name: Validate composer.lock | ||
run: composer validate --strict | ||
|
||
- name: Run pint | ||
run: vendor/bin/pint --test | ||
|
||
- name: Run pest | ||
run: vendor/bin/pest | ||
|
||
- name: Run Phpstan | ||
run: vendor/bin/phpstan --no-progress --debug --memory-limit=1G |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/.phpunit.cache | ||
/vendor | ||
.phpunit.result.cache | ||
/.fleet | ||
/.idea | ||
/.vscode | ||
/.phpstorm.meta.php | ||
clover.xml | ||
/build | ||
composer.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# Laravel SNS-SQS Queue | ||
[![Latest Stable Version](https://poser.pugx.org/acdphp/laravel-sns-sqs-queue/v)](https://packagist.org/packages/acdphp/laravel-sns-sqs-queue) | ||
|
||
Fanout Laravel queue jobs with AWS SNS and SQS for microservices communication. | ||
|
||
![workflow](./.docs/workflow.jpg) | ||
|
||
## Installation | ||
1. Install the package | ||
```shell | ||
composer require acdphp/laravel-sns-sqs-queue | ||
``` | ||
|
||
2. Add new queue connection in your `config/queue.php`. *(Similar to default sqs connection but with added `endpoint` and `sns_topic_arn`)* | ||
```php | ||
'connections' => [ | ||
... | ||
'sns-sqs' => [ | ||
'driver' => 'sns-sqs', | ||
'key' => env('AWS_ACCESS_KEY_ID'), | ||
'secret' => env('AWS_SECRET_ACCESS_KEY'), | ||
'region' => env('AWS_DEFAULT_REGION', 'us-east-1'), | ||
'prefix' => env('SQS_PREFIX', 'https://sqs.us-east-1.amazonaws.com/your-account-id'), | ||
'queue' => env('SQS_QUEUE', 'default'), | ||
'suffix' => env('SQS_SUFFIX'), | ||
'after_commit' => false, | ||
'endpoint' => env('AWS_ENDPOINT'), | ||
'sns_topic_arn' => env('SNS_TOPIC_ARN', 'arn:aws:sns:us-east-1:your-account-id:topic'), | ||
], | ||
] | ||
``` | ||
|
||
3. Usage | ||
1. Globally set your `QUEUE_CONNECTION` to `sns-sqs` | ||
2. OR Job specific (recommended to only use this for microservice message jobs): | ||
```php | ||
class MicroserviceMessageJob implements ShouldQueue | ||
{ | ||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; | ||
public function __construct($yourData, $anotherData) | ||
{ | ||
$this->onConnection('sns-sqs'); | ||
} | ||
``` | ||
## Notes | ||
- Fanout Jobs must exist in all applications that should handle the message with the same namespace and constructor arguments for it work properly. | ||
- Publishing to SNS uses HTTP/S protocol which is synchronous. This means that your job dispatch will hold off the process until the SNS responds back. Consider using a better technology, for instance, using AMQP (like this [library](https://github.com/vyuldashev/laravel-queue-rabbitmq)) for better performance. | ||
## License | ||
The MIT License (MIT). Please see [License File](LICENSE) for more information. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
{ | ||
"name": "acdphp/laravel-sns-sqs-queue", | ||
"description": "Fanout Laravel queue jobs with AWS SNS and SQS for microservices communication.", | ||
"type": "library", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Carlo Dinopol", | ||
"email": "carlo.dinopol@gmail.com" | ||
} | ||
], | ||
"minimum-stability": "stable", | ||
"require": { | ||
"php": "^8.0", | ||
"aws/aws-sdk-php": "^3.295" | ||
}, | ||
"require-dev": { | ||
"laravel/pint": "^1.5", | ||
"phpstan/phpstan": "^1.10", | ||
"larastan/larastan": "^2.7", | ||
"ekino/phpstan-banned-code": "^1.0", | ||
"orchestra/testbench": "^7.37", | ||
"pestphp/pest": "^1.23", | ||
"pestphp/pest-plugin-laravel": "^1.4", | ||
"mockery/mockery": "^1.6" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"Acdphp\\SnsSqsQueue\\": "src" | ||
} | ||
}, | ||
"autoload-dev": { | ||
"psr-4": { | ||
"Acdphp\\SnsSqsQueue\\Tests\\": "tests", | ||
"Workbench\\App\\": "workbench/app/" | ||
} | ||
}, | ||
"extra": { | ||
"laravel": { | ||
"providers": [ | ||
"Acdphp\\SnsSqsQueue\\SnsSqsQueueServiceProvider" | ||
] | ||
} | ||
}, | ||
"config": { | ||
"allow-plugins": { | ||
"pestphp/pest-plugin": true | ||
} | ||
}, | ||
"scripts": { | ||
"post-autoload-dump": [ | ||
"@clear", | ||
"@prepare" | ||
], | ||
"clear": "@php vendor/bin/testbench package:purge-skeleton --ansi", | ||
"prepare": "@php vendor/bin/testbench package:discover --ansi", | ||
"build": "@php vendor/bin/testbench workbench:build --ansi", | ||
"serve": [ | ||
"Composer\\Config::disableProcessTimeout", | ||
"@build", | ||
"@php vendor/bin/testbench serve" | ||
], | ||
"lint": [ | ||
"@php vendor/bin/pint", | ||
"@php vendor/bin/phpstan analyse" | ||
], | ||
"test": [ | ||
"@php vendor/bin/pest" | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
includes: | ||
- ./vendor/larastan/larastan/extension.neon | ||
- ./vendor/ekino/phpstan-banned-code/extension.neon | ||
|
||
parameters: | ||
checkModelProperties: true | ||
paths: | ||
- src | ||
level: 6 | ||
checkMissingIterableValueType: false | ||
reportUnmatchedIgnoredErrors: false | ||
checkGenericClassInNonGenericObjectType: false | ||
banned_code: | ||
nodes: | ||
- { type: Stmt_Echo, functions: null } | ||
- { type: Expr_Eval, functions: null } | ||
- { type: Expr_Exit, functions: null } | ||
- { type: Expr_Print, functions: null } | ||
- | ||
type: Expr_FuncCall | ||
functions: | ||
- dd | ||
- ddd | ||
- debug_backtrace | ||
- dump | ||
- exec | ||
- passthru | ||
- phpinfo | ||
- print_r | ||
- proc_open | ||
- shell_exec | ||
- system | ||
- var_dump | ||
use_from_tests: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" | ||
backupGlobals="false" | ||
bootstrap="vendor/autoload.php" | ||
colors="true" | ||
executionOrder="random" | ||
failOnWarning="true" | ||
failOnRisky="true" | ||
failOnEmptyTestSuite="true" | ||
cacheDirectory=".phpunit.cache" | ||
backupStaticProperties="false" | ||
> | ||
<testsuites> | ||
<testsuite name="Feature"> | ||
<directory>tests/Feature</directory> | ||
</testsuite> | ||
</testsuites> | ||
<source> | ||
<include> | ||
<directory suffix=".php">./src</directory> | ||
</include> | ||
</source> | ||
<php> | ||
<env name="APP_ENV" value="testing"/> | ||
<env name="APP_KEY" value="base64:2fl+Ktvkfl+Fuz4Qp/A75G2RTiWVA/ZoKZvp6fiiM10="/> | ||
</php> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
namespace Acdphp\SnsSqsQueue\Queue\Connectors; | ||
|
||
use Acdphp\SnsSqsQueue\Queue\SnsSqsQueue; | ||
use Aws\Sns\SnsClient; | ||
use Aws\Sqs\SqsClient; | ||
use Illuminate\Queue\Connectors\SqsConnector; | ||
use Illuminate\Support\Arr; | ||
|
||
class SnsSqsConnector extends SqsConnector | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function connect(array $config) | ||
{ | ||
$config = $this->getDefaultConfiguration($config); | ||
|
||
if ($config['key'] && $config['secret']) { | ||
$config['credentials'] = Arr::only($config, ['key', 'secret', 'token']); | ||
} | ||
|
||
return new SnsSqsQueue( | ||
new SnsClient($config), | ||
$config['sns_topic_arn'], | ||
new SqsClient($config), | ||
$config['queue'], | ||
$config['prefix'] ?? '', | ||
$config['suffix'] ?? '', | ||
$config['after_commit'] ?? null | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
namespace Acdphp\SnsSqsQueue\Queue; | ||
|
||
use Aws\Sns\SnsClient; | ||
use Aws\Sqs\SqsClient; | ||
use Illuminate\Queue\SqsQueue; | ||
|
||
class SnsSqsQueue extends SqsQueue | ||
{ | ||
protected SnsClient $sns; | ||
|
||
protected string $topicArn; | ||
|
||
public function __construct( | ||
SnsClient $sns, | ||
string $topicArn, | ||
SqsClient $sqs, | ||
$default, | ||
$prefix = '', | ||
$suffix = '', | ||
$dispatchAfterCommit = false | ||
) { | ||
$this->sns = $sns; | ||
$this->topicArn = $topicArn; | ||
|
||
parent::__construct($sqs, $default, $prefix, $suffix, $dispatchAfterCommit); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function pushRaw($payload, $queue = null, array $options = []) | ||
{ | ||
return $this->sns->publish([ | ||
'TopicArn' => $this->topicArn, | ||
'Message' => $payload, | ||
])->get('MessageId'); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function later($delay, $job, $data = '', $queue = null) | ||
{ | ||
return $this->enqueueUsing( | ||
$job, | ||
$this->createPayload($job, $queue ?: $this->default, $data), | ||
$queue, | ||
$delay, | ||
function ($payload, $queue, $delay) { | ||
return $this->sns->publish([ | ||
'TopicArn' => $this->topicArn, | ||
'MessageBody' => $payload, | ||
'DelaySeconds' => $this->secondsUntil($delay), | ||
])->get('MessageId'); | ||
} | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace Acdphp\SnsSqsQueue; | ||
|
||
use Acdphp\SnsSqsQueue\Queue\Connectors\SnsSqsConnector; | ||
use Illuminate\Queue\QueueManager; | ||
use Illuminate\Support\ServiceProvider; | ||
|
||
class SnsSqsQueueServiceProvider extends ServiceProvider | ||
{ | ||
public function register(): void | ||
{ | ||
$this->app->afterResolving(QueueManager::class, function (QueueManager $manager) { | ||
$manager->addConnector('sns-sqs', function () { | ||
return new SnsSqsConnector(); | ||
}); | ||
}); | ||
} | ||
|
||
public function boot(): void | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
providers: | ||
- Workbench\App\Providers\WorkbenchServiceProvider | ||
- Acdphp\SnsSqsQueue\SnsSqsQueueServiceProvider | ||
|
||
workbench: | ||
start: '/' | ||
install: true | ||
discovers: | ||
web: false | ||
api: false | ||
commands: false | ||
components: false | ||
views: false | ||
build: [] | ||
assets: [] | ||
sync: [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?php | ||
|
||
test('confirm environment is set to testing', function () { | ||
expect(config('app.env'))->toBe('testing'); | ||
}); |
Oops, something went wrong.