Skip to content

Commit e54e2c8

Browse files
author
palPalani
committed
Improved queue name
1 parent 6dd9e7d commit e54e2c8

File tree

6 files changed

+99
-17
lines changed

6 files changed

+99
-17
lines changed

README.md

Lines changed: 93 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,30 +15,112 @@ You can install the package via composer:
1515
composer require palpalani/laravel-sqs-queue-json-reader
1616
```
1717

18-
You can publish and run the migrations with:
19-
20-
```bash
21-
php artisan vendor:publish --provider="Palpalani\SqsQueueReader\SqsQueueReaderServiceProvider" --tag="migrations"
22-
php artisan migrate
23-
```
24-
2518
You can publish the config file with:
2619
```bash
27-
php artisan vendor:publish --provider="Palpalani\SqsQueueReader\SqsQueueReaderServiceProvider" --tag="config"
20+
php artisan vendor:publish --provider="palPalani\SqsQueueReader\SqsQueueReaderServiceProvider" --tag="config"
2821
```
2922

3023
This is the contents of the published config file:
3124

3225
```php
3326
return [
27+
28+
/**
29+
* Separate queue handle with corresponding queue name as key.
30+
*/
31+
'handlers' => [
32+
//'stripe-webhooks' => App\Jobs\SqsHandler::class,
33+
],
34+
35+
'default-handler' => App\Jobs\SqsHandler::class
3436
];
3537
```
3638

37-
## Usage
39+
If queue is not found in 'handlers' array, SQS payload is passed to default handler.
40+
41+
Add sqs-json connection to your config/queue.php, eg:
42+
43+
```php
44+
[
45+
// Add connection
46+
'sqs-json' => [
47+
'driver' => 'sqs-json',
48+
'key' => env('AWS_KEY', ''),
49+
'secret' => env('AWS_SECRET', ''),
50+
'prefix' => 'https://sqs.us-west-2.amazonaws.com/3242342351/',
51+
'queue' => 'stripe-webhooks',
52+
'region' => 'ap-southeast-2',
53+
],
54+
]
55+
```
56+
57+
In your .env file, choose sqs-plain as your new default queue driver:
58+
59+
```
60+
QUEUE_DRIVER=sqs-json
61+
```
62+
63+
Dispatching to SQS
64+
65+
If you plan to push plain messages from Laravel or Lumen, you can rely on DispatcherJob:
66+
67+
```php
68+
use palPalani\SqsQueueReader\Jobs\DispatcherJob;
69+
70+
class ExampleController extends Controller
71+
{
72+
public function index()
73+
{
74+
// Create a PHP object
75+
$object = [
76+
'music' => 'Sample message',
77+
'time' => time()
78+
];
79+
80+
// Pass it to dispatcher job
81+
$job = new DispatcherJob($object);
82+
83+
// Dispatch the job as you normally would
84+
// By default, your data will be encapsulated in 'data' and 'job' field will be added
85+
$this->dispatch($job);
86+
87+
// If you wish to submit a true plain JSON, add setPlain()
88+
$this->dispatch($job->setPlain());
89+
}
90+
}
91+
```
92+
This will push the following JSON object to SQS:
93+
94+
```json
95+
{"job":"App\\Jobs\\SqsHandler@handle","data":{"music":"Sample message","time":1464411642}}
96+
```
97+
98+
'job' field is not used, actually. It's just kept for compatibility.
99+
100+
Receiving from SQS
101+
If a third-party application is creating custom-format JSON messages, just add a
102+
handler in the config file and implement a handler class as follows:
38103

39104
```php
40-
$laravel-sqs-queue-json-reader = new Palpalani\SqsQueueReader();
41-
echo $laravel-sqs-queue-json-reader->echoPhrase('Hello, Palpalani!');
105+
use Illuminate\Contracts\Queue\Job as LaravelJob;
106+
107+
class HandlerJob extends Job
108+
{
109+
protected $data;
110+
111+
/**
112+
* @param LaravelJob $job
113+
* @param array $data
114+
*/
115+
public function handle(LaravelJob $job, array $data)
116+
{
117+
// This is incoming JSON payload, already decoded to an array
118+
var_dump($data);
119+
120+
// Raw JSON payload from SQS, if necessary
121+
var_dump($job->getRawBody());
122+
}
123+
}
42124
```
43125

44126
## Testing

phpunit.xml.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
verbose="true"
1212
>
1313
<testsuites>
14-
<testsuite name="Palpalani Test Suite">
14+
<testsuite name="palPalani Test Suite">
1515
<directory>tests</directory>
1616
</testsuite>
1717
</testsuites>

src/Jobs/DispatcherJob.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function getPayload()
3939
{
4040
if (! $this->isPlain()) {
4141
return [
42-
'job' => app('config')->get('sqs-plain.default-handler'),
42+
'job' => app('config')->get('sqs-queue-reader.default-handler'),
4343
'data' => $this->data,
4444
];
4545
}

src/SqsQueueReaderServiceProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function register()
2727
$this->mergeConfigFrom(__DIR__ . '/../config/sqs-queue-reader.php', 'laravel-sqs-queue-json-reader');
2828

2929
$this->app->booted(function () {
30-
$this->app['queue']->extend('sqs-plain', static function () {
30+
$this->app['queue']->extend('sqs-json', static function () {
3131
return new Connector();
3232
});
3333
});

tests/ExampleTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Palpalani\SqsQueueReader\Tests;
3+
namespace palPalani\SqsQueueReader\Tests;
44

55
class ExampleTest extends TestCase
66
{

tests/TestCase.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?php
22

3-
namespace Palpalani\SqsQueueReader\Tests;
3+
namespace palPalani\SqsQueueReader\Tests;
44

55
use Orchestra\Testbench\TestCase as Orchestra;
6-
use Palpalani\SqsQueueReader\SqsQueueReaderServiceProvider;
6+
use palPalani\SqsQueueReader\SqsQueueReaderServiceProvider;
77

88
class TestCase extends Orchestra
99
{

0 commit comments

Comments
 (0)