Skip to content
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

Improve SQS driver and fix tests #497

Merged
merged 51 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
7bc5511
First implementation
s1lver Sep 22, 2023
8efd635
Migrate phpunit config
s1lver Sep 22, 2023
c571f81
Added return type hints and fixed unsupported phpunit methods
s1lver Sep 22, 2023
d8659d5
opis/closure replaced to laravel/serializable-closure
s1lver Sep 22, 2023
08d53fd
opis/closure replaced to laravel/serializable-closure
s1lver Sep 28, 2023
33f7793
Added IDE helpers and more types hinting
s1lver Sep 28, 2023
a967c8f
Updated CI
s1lver Sep 28, 2023
ec08b06
Updated CI
s1lver Sep 28, 2023
8a7b9c8
Updated CI
s1lver Sep 28, 2023
891f54b
Added more strict types
s1lver Sep 28, 2023
8edfc1b
Added more strict types
s1lver Oct 2, 2023
140c53f
Merge remote-tracking branch 'upstream/master' into php_81
s1lver Oct 2, 2023
244d004
Added more strict types
s1lver Oct 2, 2023
b97c89c
Removed deprecated AMQP driver
s1lver Oct 2, 2023
729d057
Added more strict types
s1lver Oct 2, 2023
055a2c2
Added more strict types
s1lver Oct 2, 2023
1626b8a
Added more strict types
s1lver Oct 2, 2023
17f21be
Removed deprecated Job interface
s1lver Oct 2, 2023
f994f0f
Added more strict types
s1lver Oct 2, 2023
0a702e3
Removed deprecated Serializer interface
s1lver Oct 2, 2023
7882b59
Added more strict types
s1lver Oct 2, 2023
9df1db4
Removed deprecated RetryableJob interface
s1lver Oct 2, 2023
f93677c
Removed deprecated Signal class
s1lver Oct 2, 2023
105d79c
Removed deprecated Verbose class
s1lver Oct 2, 2023
1e8cd64
Added more strict types
s1lver Oct 2, 2023
c06aba8
Removed deprecated Dockerfile
s1lver Oct 2, 2023
bedfa60
Cleaning
s1lver Oct 2, 2023
711f008
Added more strict types
s1lver Oct 2, 2023
b2034eb
Added code coverage driver
s1lver Oct 4, 2023
495c32d
Added more strict types
s1lver Oct 4, 2023
721b7a9
Merge branch 'master' into php_81
s1lver Oct 4, 2023
29068a7
Added more strict types
s1lver Oct 4, 2023
4355264
Added codecov
s1lver Oct 4, 2023
d6da3f4
Added more strict types
s1lver Oct 4, 2023
7ceb8fc
Cleaning
s1lver Oct 4, 2023
62f4959
Cleaning
s1lver Oct 4, 2023
516f6e1
Fixed static analysis
s1lver Oct 4, 2023
5fe7c13
Fixed static analysis
s1lver Oct 4, 2023
a81acb9
Fixed static analysis
s1lver Oct 4, 2023
0340813
Improve amqp driver
s1lver Oct 5, 2023
8f35468
Improve static analyzes
s1lver Oct 5, 2023
20a10c9
Fixed CI
s1lver Oct 5, 2023
868273e
Fixed CI
s1lver Oct 5, 2023
82142da
Fixed CI
s1lver Oct 5, 2023
178dec2
Improved test for AMQP driver
s1lver Oct 10, 2023
3754194
Change testsuite name
s1lver Oct 12, 2023
d40f767
Using constructor property promotion
s1lver Oct 12, 2023
66d8518
Improve SQS driver and fix tests
s1lver Oct 12, 2023
5d600e7
Updated documentation for SQS driver
s1lver Oct 12, 2023
d20dbc3
Fix CI
s1lver Oct 12, 2023
15226bc
Merge remote-tracking branch 'upstream/3.0' into sqs
s1lver Oct 12, 2023
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
9 changes: 0 additions & 9 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,11 +1,2 @@
COMPOSE_PROJECT_NAME=yii2-queue
COMPOSE_FILE=tests/docker-compose.yml

AWS_SQS_ENABLED=
AWS_SQS_URL=
AWS_KEY=
AWS_SECRET=
AWS_REGION=
AWS_SQS_FIFO_ENABLED=
AWS_SQS_FIFO_URL=
AWS_SQS_FIFO_MESSAGE_GROUP_ID=
8 changes: 0 additions & 8 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,6 @@ on:
env:
COMPOSE_PROJECT_NAME: yii2-queue
COMPOSE_FILE: tests/docker-compose.yml
AWS_SQS_ENABLED: ${{ secrets.AWS_SQS_ENABLED }}
AWS_SQS_URL: ${{ secrets.AWS_SQS_URL }}
AWS_KEY: ${{ secrets.AWS_KEY }}
AWS_SECRET: ${{ secrets.AWS_SECRET }}
AWS_REGION: ${{ secrets.AWS_REGION }}
AWS_SQS_FIFO_ENABLED: ${{ secrets.AWS_SQS_FIFO_ENABLED }}
AWS_SQS_FIFO_URL: ${{ secrets.AWS_SQS_FIFO_URL }}
AWS_SQS_FIFO_MESSAGE_GROUP_ID: ${{ secrets.AWS_SQS_FIFO_MESSAGE_GROUP_ID }}
jobs:
phpunit:
name: PHP ${{ matrix.php }}
Expand Down
8 changes: 0 additions & 8 deletions .github/workflows/static.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,6 @@ name: static analysis
env:
COMPOSE_PROJECT_NAME: yii2-queue
COMPOSE_FILE: tests/docker-compose.yml
AWS_SQS_ENABLED: ${{ secrets.AWS_SQS_ENABLED }}
AWS_SQS_URL: ${{ secrets.AWS_SQS_URL }}
AWS_KEY: ${{ secrets.AWS_KEY }}
AWS_SECRET: ${{ secrets.AWS_SECRET }}
AWS_REGION: ${{ secrets.AWS_REGION }}
AWS_SQS_FIFO_ENABLED: ${{ secrets.AWS_SQS_FIFO_ENABLED }}
AWS_SQS_FIFO_URL: ${{ secrets.AWS_SQS_FIFO_URL }}
AWS_SQS_FIFO_MESSAGE_GROUP_ID: ${{ secrets.AWS_SQS_FIFO_MESSAGE_GROUP_ID }}
jobs:
psalm:
name: PHP ${{ matrix.php }}
Expand Down
10 changes: 9 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ help: ## Display help information
build: ## Build an image from a docker-compose file. Params: {{ v=8.1 }}. Default latest PHP 8.1
@cp -n .env.example .env
PHP_VERSION=$(filter-out $@,$(v)) docker-compose up -d --build
make create-sqs-queue
make create-sqs-fifo-queue

test: ## Run tests. Params: {{ v=8.1 }}. Default latest PHP 8.1
PHP_VERSION=$(filter-out $@,$(v)) docker-compose build --pull yii2-queue-php
make build
PHP_VERSION=$(filter-out $@,$(v)) docker-compose run yii2-queue-php vendor/bin/phpunit --coverage-clover coverage.xml
make down

Expand Down Expand Up @@ -35,3 +37,9 @@ clean:

clean-all: clean
sudo rm -rf tests/runtime/.composer*

create-sqs-queue: ## Create SQS queue
docker exec yii2-queue-localstack sh -c "awslocal sqs create-queue --queue-name yii2-queue"

create-sqs-fifo-queue: ## Create SQS FIFO queue
docker exec yii2-queue-localstack sh -c 'awslocal sqs create-queue --queue-name yii2-queue.fifo --attributes "FifoQueue=true"'
26 changes: 25 additions & 1 deletion docs/guide-ru/driver-sqs.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
```php
return [
'bootstrap' => [
'queue', // The component registers own console commands
'queue', // Компонент регистрирует собственные консольные команды
],
'components' => [
'queue' => [
Expand All @@ -24,6 +24,30 @@ return [
];
```

Пример настройки для FIFO очередей:

```php
return [
'bootstrap' => [
'queue', // Компонент регистрирует собственные консольные команды
],
'components' => [
'queue' => [
'class' => \yii\queue\sqs\Queue::class,
'url' => '<sqs url>',
'key' => '<key>',
'secret' => '<secret>',
'region' => '<region>',
'messageGroupId' => '<Group ID>',
],
],
];
```

Идентификатор группы сообщений требуется SQS для очередей FIFO. Вы можете настроить свои собственные или использовать значение "default".

Идентификатор дедупликации генерируется автоматически, поэтому независимо от того, активировали ли вы дедупликацию на основе содержимого в очереди SQS или нет, этот идентификатор будет использоваться.

Консоль
-------

Expand Down
86 changes: 86 additions & 0 deletions docs/guide-zh-CN/driver-sqs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
AWS SQS Driver
============

The driver uses AWS SQS to store queue data.

You have to add `aws/aws-sdk-php` extension to your application in order to use it.

Configuration example for standard queues:

```php
return [
'bootstrap' => [
'queue', // The component registers own console commands
],
'components' => [
'queue' => [
'class' => \yii\queue\sqs\Queue::class,
'url' => '<sqs url>',
'key' => '<key>',
'secret' => '<secret>',
'region' => '<region>',
],
],
];
```

Configuration example for FIFO queues:

```php
return [
'bootstrap' => [
'queue', // The component registers own console commands
],
'components' => [
'queue' => [
'class' => \yii\queue\sqs\Queue::class,
'url' => '<sqs url>',
'key' => '<key>',
'secret' => '<secret>',
'region' => '<region>',
'messageGroupId' => '<Group ID>',
],
],
];
```

The message group ID is required by SQS for FIFO queues. You can configure your own or use the "default" value.

The deduplication ID is generated automatically, so no matter if you have activated content-based deduplication in the SQS queue or not, this ID will be used.

Console
-------

Console command is used to execute tasks.

```sh
yii queue/listen [timeout]
```

`listen` command launches a daemon which infinitely queries the queue. If there are new tasks
they're immediately obtained and executed. `timeout` parameter is number of seconds to wait a job.
It uses SQS "Long Polling" feature, that holds a connection between client and a queue.

**Important:** `timeout` parameter for SQS driver must be in range between 0 and 20 seconds.

This method is most efficient when command is properly daemonized via
[supervisor](worker.md#supervisor) or [systemd](worker.md#systemd).

```sh
yii queue/run
```

`run` command obtains and executes tasks in a loop until queue is empty. Works well with
[cron](worker.md#cron).

`run` and `listen` commands have options:

- `--verbose`, `-v`: print executing statuses into console.
- `--isolate`: each task is executed in a separate child process.
- `--color`: highlighting for verbose mode.

```sh
yii queue/clear
```

`clear` command clears a queue.
1 change: 1 addition & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
cacheDirectory=".phpunit.cache"
displayDetailsOnIncompleteTests="true"
displayDetailsOnSkippedTests="true"
executionOrder="random"
>
<testsuites>
<testsuite name="Yii2-Queue">
Expand Down
1 change: 0 additions & 1 deletion psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
<directory name="src/gii" />
<directory name="src/drivers/db/migrations" />
<directory name="src/drivers/beanstalk" />
<directory name="src/drivers/sqs" />
</ignoreFiles>
</projectFiles>
<extraFiles>
Expand Down
3 changes: 0 additions & 3 deletions src/drivers/sqs/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,6 @@ public function actionRun(): ?int
*/
public function actionListen(int $timeout = 3): ?int
{
if (!is_numeric($timeout)) {
throw new Exception('Timeout must be numeric.');
}
if ($timeout < 1 || $timeout > 20) {
throw new Exception('Timeout must be between 1 and 20');
}
Expand Down
17 changes: 9 additions & 8 deletions src/drivers/sqs/Queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use yii\base\NotSupportedException;
use yii\queue\cli\Queue as CliQueue;
use yii\queue\serializers\JsonSerializer;
use yii\queue\serializers\SerializerInterface;

/**
* SQS Queue.
Expand Down Expand Up @@ -64,12 +65,12 @@ class Queue extends CliQueue
* Json serializer by default.
* @inheritdoc
*/
public string|array|\yii\queue\serializers\SerializerInterface $serializer = JsonSerializer::class;
public string|array|SerializerInterface $serializer = JsonSerializer::class;

/**
* @var SqsClient
* @var SqsClient|null
*/
private SqsClient $_client;
private ?SqsClient $client = null;

/**
* Listens queue and runs each job.
Expand Down Expand Up @@ -173,7 +174,7 @@ public function status($id): int
* @return bool
* @since 2.2.1
*/
public function handle(string $id, string $message, int $ttr, int $attempt)
public function handle(string $id, string $message, int $ttr, int $attempt): bool
{
return $this->handleMessage($id, $message, $ttr, $attempt);
}
Expand Down Expand Up @@ -213,8 +214,8 @@ protected function pushMessage(string $payload, int $ttr, int $delay, mixed $pri
*/
protected function getClient(): SqsClient
{
if ($this->_client) {
return $this->_client;
if (null !== $this->client) {
return $this->client;
}

if ($this->key !== null && $this->secret !== null) {
Expand All @@ -228,11 +229,11 @@ protected function getClient(): SqsClient
$credentials = CredentialProvider::defaultProvider();
}

$this->_client = new SqsClient([
$this->client = new SqsClient([
'credentials' => $credentials,
'region' => $this->region,
'version' => $this->version,
]);
return $this->_client;
return $this->client;
}
}
40 changes: 17 additions & 23 deletions tests/app/config/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
'amqpInteropQueue',
'beanstalkQueue',
'stompQueue',
'sqsQueue',
'sqsFifoQueue',
],
'components' => [
'syncQueue' => [
Expand Down Expand Up @@ -116,6 +118,21 @@
'class' => StompQueue::class,
'host' => getenv('ACTIVEMQ_HOST') ?: 'localhost',
],
'sqsQueue' => [
'class' => SqsQueue::class,
'url' => getenv('AWS_SQS_URL'),
'key' => getenv('AWS_KEY'),
'secret' => getenv('AWS_SECRET'),
'region' => getenv('AWS_REGION'),
],
'sqsFifoQueue' => [
'class' => SqsQueue::class,
'url' => getenv('AWS_SQS_FIFO_URL'),
'key' => getenv('AWS_KEY'),
'secret' => getenv('AWS_SECRET'),
'region' => getenv('AWS_REGION'),
'messageGroupId' => getenv('AWS_SQS_FIFO_MESSAGE_GROUP_ID'),
],
],
];

Expand All @@ -127,27 +144,4 @@
];
}

if (getenv('AWS_SQS_ENABLED')) {
$config['bootstrap'][] = 'sqsQueue';
$config['components']['sqsQueue'] = [
'class' => SqsQueue::class,
'url' => getenv('AWS_SQS_URL'),
'key' => getenv('AWS_KEY'),
'secret' => getenv('AWS_SECRET'),
'region' => getenv('AWS_REGION'),
];
}

if (getenv('AWS_SQS_FIFO_ENABLED')) {
$config['bootstrap'][] = 'sqsFifoQueue';
$config['components']['sqsFifoQueue'] = [
'class' => SqsQueue::class,
'url' => getenv('AWS_SQS_FIFO_URL'),
'key' => getenv('AWS_KEY'),
'secret' => getenv('AWS_SECRET'),
'region' => getenv('AWS_REGION'),
'messageGroupId' => getenv('AWS_SQS_FIFO_MESSAGE_GROUP_ID'),
];
}

return $config;
32 changes: 23 additions & 9 deletions tests/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,13 @@ services:
BEANSTALK_HOST: beanstalk
GEARMAN_HOST: gearmand
COMPOSER_ALLOW_SUPERUSER: 1
AWS_SQS_ENABLED: ${AWS_SQS_ENABLED}
AWS_KEY: ${AWS_KEY}
AWS_SECRET: ${AWS_SECRET}
AWS_REGION: ${AWS_REGION}
AWS_SQS_URL: ${AWS_SQS_URL}
ACTIVEMQ_HOST: activemq
AWS_SQS_FIFO_ENABLED: ${AWS_SQS_FIFO_ENABLED}
AWS_SQS_FIFO_URL: ${AWS_SQS_FIFO_URL}
AWS_SQS_FIFO_MESSAGE_GROUP_ID: ${AWS_SQS_FIFO_MESSAGE_GROUP_ID}
AWS_KEY: ${AWS_KEY:-admin}
AWS_SECRET: ${AWS_SECRET:-admin}
AWS_REGION: ${AWS_REGION:-us-east-1}
AWS_SQS_URL: ${AWS_SQS_URL:-http://localstack:4566/000000000000/yii2-queue}
AWS_SQS_FIFO_URL: ${AWS_SQS_FIFO_URL:-http://localstack:4566/000000000000/yii2-queue.fifo}
AWS_SQS_FIFO_MESSAGE_GROUP_ID: ${AWS_SQS_FIFO_MESSAGE_GROUP_ID:-default}
depends_on:
- mysql
- postgres
Expand All @@ -49,6 +47,7 @@ services:
- beanstalk
- gearmand
- activemq
- localstack
networks:
net: {}

Expand Down Expand Up @@ -118,7 +117,22 @@ services:
networks:
net: {}

# https://hub.docker.com/r/localstack/localstack
localstack:
container_name: yii2-queue-localstack
image: localstack/localstack
restart: always
ports:
- "4566:4566"
environment:
AWS_DEFAULT_REGION: us-east-1
AWS_ACCESS_KEY_ID: admin
AWS_SECRET_ACCESS_KEY: admin
SERVICES: sqs
DISABLE_CORS_CHECKS: 1
networks:
net: {}

networks:
net:
name: yii2_queue_net

Loading
Loading