Skip to content

Commit fb0179f

Browse files
authored
Adding Illuminate/Queue integration (#549)
* Adding Illuminate/Queue integration * PHPStan fixes * Psalm fixes * Support illumiate 6.18 * composer normalize * composer fixes * FIxes * cs
0 parents  commit fb0179f

15 files changed

+524
-0
lines changed

.gitattributes

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/.github export-ignore
2+
/tests export-ignore
3+
/.gitignore export-ignore
4+
/phpunit.xml.dist export-ignore

.github/FUNDING.yml

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# These are supported funding model platforms
2+
3+
github: [nyholm, jderusse]

.github/workflows/.editorconfig

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[*.yml]
2+
indent_size = 2

.github/workflows/checks.yml

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
name: BC Check
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
8+
jobs:
9+
roave-bc-check:
10+
name: Roave BC Check
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
- name: Checkout code
15+
uses: actions/checkout@v2
16+
17+
- name: Roave BC Check
18+
uses: docker://nyholm/roave-bc-check-ga

.github/workflows/ci.yml

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
name: Tests
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
8+
jobs:
9+
10+
build:
11+
name: Build
12+
runs-on: ubuntu-latest
13+
strategy:
14+
max-parallel: 10
15+
matrix:
16+
php: ['7.2', '7.3', '7.4']
17+
18+
steps:
19+
- name: Set up PHP
20+
uses: shivammathur/[email protected]
21+
with:
22+
php-version: ${{ matrix.php }}
23+
coverage: xdebug
24+
ini-values: xdebug.overload_var_dump=1
25+
tools: prestissimo
26+
27+
- name: Checkout code
28+
uses: actions/checkout@v2
29+
30+
- name: Initialize tests
31+
run: make initialize
32+
33+
- name: Download dependencies
34+
run: |
35+
composer config minimum-stability dev
36+
composer req symfony/phpunit-bridge --no-update
37+
composer update --no-interaction --prefer-dist --optimize-autoloader --prefer-stable
38+
39+
- name: Run tests
40+
run: ./vendor/bin/simple-phpunit

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/vendor/
2+
.php_cs.cache
3+
.phpunit.result.cache
4+
composer.lock

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Change Log
2+
3+
## 0.1.0
4+
5+
First version

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2020 Jérémy Derussé, Tobias Nyholm
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# AsyncAws Illuminate Queue integration
2+
3+
![](https://github.com/async-aws/illuminate-queue/workflows/Tests/badge.svg?branch=master)
4+
![](https://github.com/async-aws/illuminate-queue/workflows/BC%20Check/badge.svg?branch=master)
5+
6+
## Install
7+
8+
```cli
9+
composer require async-aws/illuminate-queue
10+
```
11+
12+
## Documentation
13+
14+
See https://async-aws.com/ for documentation.
15+
16+
## Contribute
17+
18+
Contributions are welcome and appreciated. Please read https://async-aws.com/contribute/

composer.json

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"name": "async-aws/illuminate-queue",
3+
"type": "library",
4+
"description": "AsyncAws integration for Illuminate Queue.",
5+
"keywords": [
6+
"aws",
7+
"async-aws",
8+
"laravel",
9+
"illuminate",
10+
"sqs",
11+
"queue"
12+
],
13+
"license": "MIT",
14+
"require": {
15+
"php": "^7.2.5",
16+
"async-aws/sqs": "^1.0",
17+
"illuminate/queue": "^6.18.11 || ^7.10"
18+
},
19+
"extra": {
20+
"branch-alias": {
21+
"dev-master": "0.1-dev"
22+
}
23+
},
24+
"autoload": {
25+
"psr-4": {
26+
"AsyncAws\\Illuminate\\Queue\\": "src"
27+
}
28+
},
29+
"autoload-dev": {
30+
"psr-4": {
31+
"AsyncAws\\Illuminate\\Queue\\Tests\\": "tests/"
32+
}
33+
}
34+
}

phpunit.xml.dist

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/5.2/phpunit.xsd"
5+
backupGlobals="false"
6+
colors="true"
7+
bootstrap="vendor/autoload.php"
8+
failOnRisky="true"
9+
failOnWarning="true"
10+
>
11+
<php>
12+
<ini name="error_reporting" value="-1" />
13+
</php>
14+
15+
<testsuites>
16+
<testsuite name="Test Suite">
17+
<directory>./tests/</directory>
18+
</testsuite>
19+
</testsuites>
20+
21+
<filter>
22+
<whitelist>
23+
<directory>./src</directory>
24+
</whitelist>
25+
</filter>
26+
</phpunit>

src/AsyncAwsSqsQueue.php

+182
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
<?php
2+
3+
namespace AsyncAws\Illuminate\Queue;
4+
5+
use AsyncAws\Illuminate\Queue\Job\AsyncAwsSqsJob;
6+
use AsyncAws\Sqs\Enum\QueueAttributeName;
7+
use AsyncAws\Sqs\SqsClient;
8+
use Illuminate\Contracts\Queue\Job;
9+
use Illuminate\Contracts\Queue\Queue as QueueContract;
10+
use Illuminate\Queue\Queue;
11+
use Illuminate\Support\Str;
12+
13+
/**
14+
* This class is a port from Illuminate\Queue\SqsQueue.
15+
*/
16+
class AsyncAwsSqsQueue extends Queue implements QueueContract
17+
{
18+
/**
19+
* The Amazon SQS instance.
20+
*
21+
* @var SqsClient
22+
*/
23+
protected $sqs;
24+
25+
/**
26+
* The name of the default queue.
27+
*
28+
* @var string
29+
*/
30+
protected $default;
31+
32+
/**
33+
* The queue URL prefix.
34+
*
35+
* @var string
36+
*/
37+
protected $prefix;
38+
39+
/**
40+
* The queue name suffix.
41+
*
42+
* @var string
43+
*/
44+
private $suffix;
45+
46+
/**
47+
* Create a new Amazon SQS queue instance.
48+
*
49+
* @param string $default
50+
* @param string $prefix
51+
* @param string $suffix
52+
*
53+
* @return void
54+
*/
55+
public function __construct(SqsClient $sqs, $default, $prefix = '', $suffix = '')
56+
{
57+
$this->sqs = $sqs;
58+
$this->prefix = $prefix;
59+
$this->default = $default;
60+
$this->suffix = $suffix;
61+
}
62+
63+
/**
64+
* Get the size of the queue.
65+
*
66+
* @param string|null $queue
67+
*
68+
* @return int
69+
*/
70+
public function size($queue = null)
71+
{
72+
$response = $this->sqs->getQueueAttributes([
73+
'QueueUrl' => $this->getQueue($queue),
74+
'AttributeNames' => [QueueAttributeName::APPROXIMATE_NUMBER_OF_MESSAGES],
75+
]);
76+
77+
$attributes = $response->getAttributes();
78+
79+
return (int) $attributes[QueueAttributeName::APPROXIMATE_NUMBER_OF_MESSAGES];
80+
}
81+
82+
/**
83+
* Push a new job onto the queue.
84+
*
85+
* @param \Closure|string|object $job
86+
* @param mixed $data
87+
* @param string|null $queue
88+
*
89+
* @return mixed
90+
*/
91+
public function push($job, $data = '', $queue = null)
92+
{
93+
return $this->pushRaw($this->createPayload($job, $queue ?: $this->default, $data), $queue);
94+
}
95+
96+
/**
97+
* Push a raw payload onto the queue.
98+
*
99+
* @param string $payload
100+
* @param string|null $queue
101+
*
102+
* @return mixed
103+
*/
104+
public function pushRaw($payload, $queue = null, array $options = [])
105+
{
106+
return $this->sqs->sendMessage([
107+
'QueueUrl' => $this->getQueue($queue), 'MessageBody' => $payload,
108+
])->getMessageId();
109+
}
110+
111+
/**
112+
* Push a new job onto the queue after a delay.
113+
*
114+
* @param \DateTimeInterface|\DateInterval|int $delay
115+
* @param \Closure|string|object $job
116+
* @param mixed $data
117+
* @param string|null $queue
118+
*
119+
* @return mixed
120+
*/
121+
public function later($delay, $job, $data = '', $queue = null)
122+
{
123+
return $this->sqs->sendMessage([
124+
'QueueUrl' => $this->getQueue($queue),
125+
'MessageBody' => $this->createPayload($job, $queue ?: $this->default, $data),
126+
'DelaySeconds' => $this->secondsUntil($delay),
127+
])->getMessageId();
128+
}
129+
130+
/**
131+
* Pop the next job off of the queue.
132+
*
133+
* @param string|null $queue
134+
*
135+
* @return Job|null
136+
*/
137+
public function pop($queue = null)
138+
{
139+
$response = $this->sqs->receiveMessage([
140+
'QueueUrl' => $queue = $this->getQueue($queue),
141+
'AttributeNames' => ['ApproximateReceiveCount'],
142+
]);
143+
144+
foreach ($response->getMessages() as $message) {
145+
return new AsyncAwsSqsJob(
146+
$this->container,
147+
$this->sqs,
148+
$message,
149+
$this->connectionName,
150+
$queue
151+
);
152+
}
153+
154+
return null;
155+
}
156+
157+
/**
158+
* Get the queue or return the default.
159+
*
160+
* @param string|null $queue
161+
*
162+
* @return string
163+
*/
164+
public function getQueue($queue)
165+
{
166+
$queue = $queue ?: $this->default;
167+
168+
return false === filter_var($queue, \FILTER_VALIDATE_URL)
169+
? rtrim($this->prefix, '/') . '/' . Str::finish($queue, $this->suffix)
170+
: $queue;
171+
}
172+
173+
/**
174+
* Get the underlying SQS instance.
175+
*
176+
* @return SqsClient
177+
*/
178+
public function getSqs()
179+
{
180+
return $this->sqs;
181+
}
182+
}

0 commit comments

Comments
 (0)