coverzen/configurable-sqs
Composer 安装命令:
composer require coverzen/configurable-sqs
包简介
Package to utilize laravel queue:work command to listen to Amazon SQS message with configurable schemas
README 文档
README
This package provides a simple way to subscribe to an AWS SQS queue in Laravel.
The package is designed to be as simple as possible to use, with a simple configuration for redirect any message to a specific listener.
On Classic Laravel Listeners is possible add a enqueueFilter method to filter the message before the job is enqueued to SQS.
Installation
composer require coverzen/configurable-sqs
Configuration
First, you need to add the following environment variables to your .env file:
AWS_ACCESS_KEY_ID=your-key AWS_SECRET_ACCESS_KEY=your-secret AWS_DEFAULT_REGION=your-region AWS_ACCOUNT_ID=your-account-id
Then, you need to add the following configuration to your config/queue.php file:
'connections' => [ ... 'configurable-sqs' => [ 'driver' => 'configurable-sqs', 'key' => env('AWS_ACCESS_KEY_ID'), 'secret' => env('AWS_SECRET_ACCESS_KEY'), 'prefix' => 'https://sqs.'.env('AWS_DEFAULT_REGION').'.amazonaws.com/'.env('AWS_ACCOUNT_ID').'/', 'queue' => 'your-queue-name', 'suffix' => '', 'region' => env('AWS_DEFAULT_REGION'), 'has_consumer' => true, ], ]
Usage
To subscribe custom listener to a queue, you need to create a new listener class and configure it in the configurable-sqs.php file.
'your-queue-name' => [ [ 'type' => ConfigurableJob::TYPE_SQS_SIMPLE_PAYLOAD, 'search' => [ 'key' => 'message', 'value' => 'test', ], 'listener' => YourListener::class, ], [ 'type' => ConfigurableJob::TYPE_SQS_REGEX_PAYLOAD, 'search' => [ 'key' => 'message', 'value' => '/^test$/i', ], 'listener' => YourListener::class, ], [ 'type' => ConfigurableJob::TYPE_SNS_FROM, 'arn' => 'arn:aws:sns:eu-south-1:0000000000:HelloWorld', 'listener' => YourListener::class, ], ],
The type key can be one of the following values:
ConfigurableJob::TYPE_SQS_SIMPLE_PAYLOAD: This type will search for a specific key and value in the SQS message.ConfigurableJob::TYPE_SQS_REGEX_PAYLOAD: This type will search for a specific key and value in the SQS message using a regex pattern.ConfigurableJob::TYPE_SNS_FROM: This type will search for a specific SNS ARN in the SQS message.
The listener class can implement the Coverzen\ConfigurableSqs\Interfaces\ListenerInterface interface.
namespace App\Listeners; YourListener implements ListenerInterface { public function handle(array $message): void { // Your logic here } }
Simple SQS message job
ExampleSimpleSQSJob.php
use Coverzen\ConfigurableSqs\Job\SimpleSQSJob; class ExampleSimpleSQSJob extends SimpleSQSJob { protected string $event = 'example'; public function __construct($data) { parent::__construct($data); } }
EventTrigger.php
use App\Jobs\ExampleSimpleSQSJob; class EventTrigger { public function trigger() { ExampleSimpleSQSJob::dispatch([ 'message' => 'test', ]); } }
When the EventTrigger::trigger() class is executed the ExampleSimpleSQSJob will be dispatched to queue trough sqs-configurable driver using a simple message payload as:
{
"event": "example",
"data": {
"message": "test"
}
}
Filter message before job is enqueued
You can add a enqueueFilter method to your listener class to filter the message before the job is enqueued to SQS.
namespace App\Listeners; use App\Events\MyEvent; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; class TestListener implements ShouldQueue { use Queueable; public static function enqueueFilter(MyEvent $event): bool { // Your logic here // If you return false the job will not be enqueued return true; } public function handle(MyEvent $event): bool { return true; } }
Sending only queue configuration
If you want to send only the queue configuration, you need turn to false has_consumer configuration option.
'connections' => [ ... 'configurable-sqs-send-only' => [ 'driver' => 'configurable-sqs', 'key' => env('AWS_ACCESS_KEY_ID'), 'secret' => env('AWS_SECRET_ACCESS_KEY'), 'prefix' => 'https://sqs.'.env('AWS_DEFAULT_REGION').'.amazonaws.com/'.env('AWS_ACCOUNT_ID').'/', 'queue' => 'your-queue-name', 'suffix' => '', 'region' => env('AWS_DEFAULT_REGION'), 'has_consumer' => false, ], ]
License
The MIT License (MIT). Please see License File for more information.
Credits
coverzen/configurable-sqs 适用场景与选型建议
coverzen/configurable-sqs 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 7.28k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2024 年 06 月 03 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 coverzen/configurable-sqs 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 coverzen/configurable-sqs 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 7.28k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 7
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-06-03