Skip to content

Commit 4962623

Browse files
authored
feat: add default queue delay support on messages (#3)
* feat: add default queue delay support on messages
1 parent 19baf03 commit 4962623

File tree

10 files changed

+137
-77
lines changed

10 files changed

+137
-77
lines changed

.github/workflows/test.yml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
name: test
2+
3+
on:
4+
pull_request:
5+
6+
jobs:
7+
test:
8+
runs-on: ubuntu-latest
9+
steps:
10+
- name: Checkout to GitHub workspace
11+
uses: actions/checkout@v4
12+
with:
13+
fetch-depth: 0
14+
15+
- name: Test
16+
continue-on-error: false
17+
env:
18+
CI_PULL_REQUEST_TITLE: ${{ github.event.pull_request.title }}
19+
run: |
20+
# Commitlint
21+
npx --version
22+
npm install --location=global --save-dev @commitlint/config-conventional
23+
echo "module.exports = {
24+
extends: ['@commitlint/config-conventional'],
25+
rules: {
26+
'header-max-length': [2, 'always', 100]
27+
}
28+
}" >commitlint.config.js
29+
npx commitlint --to HEAD --verbose
30+
echo "$CI_PULL_REQUEST_TITLE" | npx commitlint
31+
32+
# Functional Tests
33+
composer install --prefer-dist
34+
./vendor/bin/phpunit

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/vendor
2-
.phpunit.result.cache
2+
.phpunit.*cache
3+
composer.lock

composer.json

Lines changed: 52 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,57 @@
11
{
2-
"name": "culturalinfusion/laravel-sqs-fifo",
3-
"type": "library",
4-
"description": "Laravel SQS FIFO",
5-
"keywords": [
6-
"laravel",
7-
"sqs",
8-
"fifo"
9-
],
10-
"homepage": "https://culturalinfusion.org.au",
11-
"license": "MIT",
12-
"authors": [
13-
{
14-
"name": "Mohsen Sadeghzade",
15-
"email": "[email protected]"
2+
"name": "culturalinfusion/laravel-sqs-fifo",
3+
"type": "library",
4+
"description": "Laravel SQS FIFO",
5+
"keywords": [
6+
"laravel",
7+
"sqs",
8+
"fifo"
9+
],
10+
"homepage": "https://culturalinfusion.org.au",
11+
"license": "MIT",
12+
"authors": [
13+
{
14+
"name": "Mohsen Sadeghzade",
15+
"email": "[email protected]"
16+
},
17+
{
18+
"name": "Ali Bayat",
19+
"email": "[email protected]"
20+
}
21+
],
22+
"support": {
23+
"issues": "https://github.com/culturalinfusion/laravel-sqs-fifo/issues",
24+
"source": "https://github.com/culturalinfusion/laravel-sqs-fifo"
1625
},
17-
{
18-
"name": "Ali Bayat",
19-
"email": "[email protected]"
20-
}
21-
],
22-
"support": {
23-
"issues": "https://github.com/culturalinfusion/laravel-sqs-fifo/issues",
24-
"source": "https://github.com/culturalinfusion/laravel-sqs-fifo"
25-
},
26-
"minimum-stability": "dev",
27-
"prefer-stable": true,
28-
"require": {
29-
"php": "^7.2 || ^8.0",
30-
"aws/aws-sdk-php": "^3.239",
31-
"illuminate/queue": ">=4.1"
32-
},
33-
"require-dev": {
34-
"phpunit/phpunit": "^9.5",
35-
"orchestra/testbench": "^7.7"
36-
},
37-
"autoload": {
38-
"psr-4": {
39-
"CulturalInfusion\\LaravelSqsFifo\\": "src/"
40-
}
41-
},
42-
"autoload-dev": {
43-
"psr-4": {
44-
"CulturalInfusion\\LaravelSqsFifo\\Tests\\": "tests/"
45-
}
46-
},
47-
"extra": {
48-
"laravel": {
49-
"providers": [
50-
"CulturalInfusion\\LaravelSqsFifo\\ServiceProvider"
51-
]
26+
"minimum-stability": "dev",
27+
"prefer-stable": true,
28+
"require": {
29+
"php": "^8.0",
30+
"aws/aws-sdk-php": "^3.239.9",
31+
"illuminate/queue": "^10.44.0"
32+
},
33+
"require-dev": {
34+
"phpunit/phpunit": "^10.5",
35+
"orchestra/testbench": "^8.21"
36+
},
37+
"autoload": {
38+
"psr-4": {
39+
"CulturalInfusion\\LaravelSqsFifo\\": "src/"
40+
}
41+
},
42+
"autoload-dev": {
43+
"psr-4": {
44+
"CulturalInfusion\\LaravelSqsFifo\\Tests\\": "tests/"
45+
}
5246
},
53-
"branch-alias": {
54-
"dev-master": "1.x-dev"
47+
"extra": {
48+
"laravel": {
49+
"providers": [
50+
"CulturalInfusion\\LaravelSqsFifo\\ServiceProvider"
51+
]
52+
},
53+
"branch-alias": {
54+
"dev-master": "1.x-dev"
55+
}
5556
}
56-
}
5757
}

config/queue.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@
2020
'suffix' => env('SQS_SUFFIX'),
2121
'queue' => env('SQS_QUEUE'),
2222
'queue_name_prefix' => env('SQS_QUEUE_NAME_PREFIX'),
23-
'message_group_id' => env('SQS_MESSAGE_GROUP_ID', env('SQS_PREFIX') . "/" . env('SQS_QUEUE')),
23+
'message_group_id' => env('SQS_MESSAGE_GROUP_ID', env('SQS_ENDPOINT') . "/" . env('SQS_QUEUE')),
2424
'region' => env('AWS_DEFAULT_REGION', 'ap-southeast-2'),
25+
'submit_delay' => env('SQS_SUBMIT_DELAY', true) // Whether to use default delay of a Queue on Messages
2526
]
2627
]
2728
];

phpunit.xml.dist

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" backupStaticAttributes="false" bootstrap="vendor/autoload.php" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" verbose="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
3-
<coverage processUncoveredFiles="true">
4-
<include>
5-
<directory suffix=".php">./src</directory>
6-
</include>
7-
</coverage>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" bootstrap="vendor/autoload.php" colors="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
83
<testsuites>
94
<testsuite name="Service">
105
<directory suffix="Test.php">./tests</directory>
@@ -14,4 +9,9 @@
149
<env name="API_DEBUG" value="false"/>
1510
<ini name="memory_limit" value="512M"/>
1611
</php>
12+
<source>
13+
<include>
14+
<directory suffix=".php">./src</directory>
15+
</include>
16+
</source>
1717
</phpunit>

src/Services/SqsFifoConnector.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ public function connect(array $config): SqsFifoQueue
2828
$config['prefix'] ?? '',
2929
$config['suffix'] ?? '',
3030
$config['message_group_id'],
31-
$config['queue_name_prefix']
31+
$config['queue_name_prefix'],
32+
$config['submit_delay'] ?? true,
3233
);
3334
}
3435
}

src/Services/SqsFifoQueue.php

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace CulturalInfusion\LaravelSqsFifo\Services;
44

55
use Aws\Sqs\SqsClient;
6-
use Exception;
6+
use BadMethodCallException;
77
use Illuminate\Queue\SqsQueue;
88

99
class SqsFifoQueue extends SqsQueue
@@ -23,7 +23,14 @@ class SqsFifoQueue extends SqsQueue
2323
protected string $queue_name_prefix;
2424

2525
/**
26-
* The queue name suffix.
26+
* The flag to check whether to use default delay of the Queue on Messages.
27+
*
28+
* @var string
29+
*/
30+
protected string $submit_delay;
31+
32+
/**
33+
* The queue name.
2734
*
2835
* @var string
2936
*/
@@ -44,6 +51,8 @@ class SqsFifoQueue extends SqsQueue
4451
* @param string $prefix
4552
* @param string $suffix
4653
* @param string $message_group_id
54+
* @param string $queue_name_prefix
55+
* @param bool $submit_delay
4756
* @return void
4857
*/
4958
public function __construct(
@@ -52,11 +61,13 @@ public function __construct(
5261
string $prefix = '',
5362
string $suffix = '',
5463
string $message_group_id = null,
55-
string $queue_name_prefix = ''
64+
string $queue_name_prefix = '',
65+
bool $submit_delay = true
5666
) {
5767
parent::__construct($sqs, $default, $prefix);
5868
$this->message_group_id = $message_group_id;
5969
$this->queue_name_prefix = $queue_name_prefix;
70+
$this->submit_delay = $submit_delay;
6071
$this->suffix = $suffix;
6172
}
6273

@@ -94,12 +105,25 @@ public function pushRaw($payload, $queue = null, $options = []): string|null
94105
}
95106

96107
/**
97-
* SQS FIFO does not support DelaySeconds currently.
108+
* Since SQS FIFO does not support delay per message,
109+
* this method checks whether to apply delay value of
110+
* the queue on the job, or throw an exception otherwise.
111+
*
112+
* @param \DateTime|int $delay
113+
* @param string $job
114+
* @param mixed $data
115+
* @param string|null $queue
98116
*
99-
* @throw Exception
117+
* @return mixed
118+
*
119+
* @throws BadMethodCallException
100120
*/
101-
public function later($delay, $job, $data = '', $queue = null): Exception
121+
public function later($delay, $job, $data = '', $queue = null): mixed
102122
{
103-
throw new Exception("Cannot support DelaySeconds for FIFO queues.");
123+
if ($this->submit_delay) {
124+
return $this->push($job, $data, $queue);
125+
}
126+
127+
throw new BadMethodCallException('SQS FIFO does not support DelaySeconds per message.');
104128
}
105129
}

tests/ConnectorTest.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@
22

33
namespace CulturalInfusion\LaravelSqsFifo\Tests;
44

5-
use InvalidArgumentException;
6-
use CulturalInfusion\LaravelSqsFifo\{Services\SqsFifoConnector, SqsFifoQueue};
5+
use CulturalInfusion\LaravelSqsFifo\Services\{SqsFifoConnector, SqsFifoQueue};
76

87
class ConnectorTest extends TestCase
98
{
109
public function test_sqs_fifo_driver_returns_sqs_fifo_queue()
1110
{
12-
$config = $this->app['config']['queue.connections.sqsfifo'];
11+
$config = $this->container['config']['queue.connections.sqsfifo'];
1312
$connector = new SqsFifoConnector();
1413

1514
$connection = $connector->connect($config);

tests/QueueTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function test_queue_sends_message_group_id()
3434
$client->shouldReceive('sendMessage')->with(Mockery::on($closure))->andReturn($result);
3535

3636
$queue = new SqsFifoQueue($client, '', '', '', $group, '');
37-
$queue->setContainer($this->app);
37+
$queue->setContainer($this->container);
3838

3939
$this->assertEquals($queue->pushRaw($job), $messageId);
4040
}

tests/TestCase.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class TestCase extends BaseTestCase
1616
*
1717
* @var Container
1818
*/
19-
protected Container $app;
19+
protected Container $container;
2020

2121
/**
2222
* The Queue Capsule instance for the tests.
@@ -49,9 +49,9 @@ public function setUpCapsule(): void
4949
$queue->setAsGlobal();
5050

5151
$this->queue = $queue;
52-
$this->app = $queue->getContainer();
52+
$this->container = $queue->getContainer();
5353

54-
$this->app->instance('queue', $queue->getQueueManager());
54+
$this->container->instance('queue', $queue->getQueueManager());
5555
}
5656

5757
/**
@@ -61,7 +61,7 @@ public function setUpCapsule(): void
6161
*/
6262
public function registerServiceProvider(): void
6363
{
64-
$provider = new ServiceProvider($this->app);
64+
$provider = new ServiceProvider($this->container);
6565

6666
$provider->register();
6767
}
@@ -97,7 +97,7 @@ public function setUpQueueConnection(): void
9797
], $connection);
9898

9999
$queue->getQueueManager()->addConnector($connection, function () {
100-
return new SqsFifoConnector($this->app);
100+
return new SqsFifoConnector($this->container);
101101
});
102102
}
103103

0 commit comments

Comments
 (0)