Skip to content

Commit

Permalink
Merge pull request #2 from letsgoi/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
marcoocram authored Apr 27, 2020
2 parents 7146a71 + dd27fb1 commit 9733661
Show file tree
Hide file tree
Showing 26 changed files with 952 additions and 1 deletion.
8 changes: 8 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Path-based git attributes
# https://www.kernel.org/pub/software/scm/git/docs/gitattributes.html

# Ignore all test and documentation with "export-ignore".
/.gitattributes export-ignore
/.gitignore export-ignore
/phpunit.xml.dist export-ignore
/tests export-ignore
28 changes: 28 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Continuous Integration

on: [push]

jobs:
syntax:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Install dependencies
run: composer install -q --no-ansi --no-interaction --no-scripts --no-suggest --no-progress --prefer-dist

- name: Check syntax
run: composer syntax

test:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Install dependencies
run: composer install -q --no-ansi --no-interaction --no-scripts --no-suggest --no-progress --prefer-dist

- name: Execute tests
run: composer test
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/.idea
/vendor
composer.lock
/phpunit.xml
.phpunit.result.cache
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) GOI

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
128 changes: 127 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,127 @@
# Laravel Domain Events Messaging
# Laravel Domain Events Messaging

This package allows to send/consume domain message events from your laravel app. This can be used to communicate between services or in the same application via queues.

It's divided in two main elements:

- **Publisher**: To publish messages on queue.
- **Consumer**: Read messages from queue and execute jobs/events.

## Requirements

- PHP >= 7.2
- Laravel >= 6.0

## Instalation

- Require package with composer:

```bash
composer require letsgoi/laravel-domain-events-messaging
```

- Publish configuration:

```bash
php artisan vendor:publish --provider="Letsgoi\DomainEventsMessaging\DomainEventsMessagingServiceProvider" --tag="config"
```

Service Provider will be automatically registered, however if you want to add it manually, you must add this to your `config/app.php` file:

```php
'providers' => [
// ...
Letsgoi\DomainEventsMessaging\DomainEventsMessagingServiceProvider::class,
];
```

## Usage

### Publisher

Work in Progress

### Consumer

Domain Events Consumer will consume messages from the queue driver defined on config file (on `DOMAIN_EVENTS_CONSUMER_CONNECTION` env variable) and launch jobs/events setted on config file (`config/domain_events_messaging.php`) by message subject.

#### Defining events:

On config file you must set the messages to be consumed and the job/event to be dispatched when this message will be consumed:

```php
'consumer' => [
// ...

'events' => [
'message.subject' => Event::class,
'message.other' => Job::class,
// ...
]
]
```

When consumer read some message with subject defined on this config, the job/event will be launch with the `array $payload` setted on his constructor.

#### Jobs/Events

The class defined on config file to be dispatched on message, could be:

- Laravel Job: It must use the `Illuminate\Foundation\Bus\Dispatchable` trait.
- Laravel Event: It must use the `Illuminate\Foundation\Events\Dispatchable` trait.

Both will be receive on his constructor message as string:

```php
public function __construct(string $message)
```

You can queue the jobs as a normal job on Laravel.

#### Running consumer:

To run the consumer as a daemon you must execute:

```bash
php artisan domain-events:consume
```

This task should be monitored with some like supervisor to be running on fails.

## Drivers

### AWS SNS-SQS

To use AWS SNS or AWS SQS driver, you must follow this instructions:

- Add (and fill) this variables on your .env file:

```
# AWS IAM
AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=
# SNS
AWS_SNS_DOMAIN_EVENTS_TOPIC_ARN=
# SQS
AWS_SQS_DOMAIN_EVENTS_URL=
```

## Testing

Run tests:


```bash
composer test
```

## Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

## License
[MIT](./LICENSE.md)
60 changes: 60 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
{
"name": "letsgoi/laravel-domain-events",
"description": "Package to publish/consume domain events messages from Laravel apps",
"keywords": [
"goi",
"letsgoi",
"laravel",
"domain",
"events",
"messages",
"queue",
"communication"
],
"homepage": "https://github.com/letsgoi/laravel-domain-events",
"license": "MIT",
"authors": [
{
"name": "GOI",
"email": "tech@letsgoi.com"
}
],
"require": {
"php": ">=7.2",
"illuminate/console": "^6.0|^7.0",
"illuminate/support": "^6.0|^7.0",
"ext-json": "*",
"aws/aws-sdk-php": "^3.134"
},
"require-dev": {
"phpunit/phpunit": "^8.0",
"orchestra/testbench": "^5.1",
"letsgoi/php-code-style": "^0.1.1"
},
"autoload": {
"psr-4": {
"Letsgoi\\DomainEventsMessaging\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Letsgoi\\DomainEventsMessaging\\Tests\\": "tests"
}
},
"extra": {
"laravel": {
"providers": [
"Letsgoi\\DomainEventsMessaging\\DomainEventsMessagingServiceProvider"
],
"aliases": {
"DomainEventsMessagingConsumer": "Letsgoi\\DomainEventsMessaging\\Facades\\DomainEventsMessagingConsumer"
}
}
},
"scripts": {
"test": "phpunit",
"syntax": "phpcs"
},
"minimum-stability": "dev",
"prefer-stable": true
}
45 changes: 45 additions & 0 deletions config/domain_events_messaging.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

return [
'consumer' => [
/*
|--------------------------------------------------------------------------
| Default Domain Event Consumer Connection Name (sqs|null)
|--------------------------------------------------------------------------
|
*/

'default' => env('DOMAIN_EVENTS_CONSUMER_CONNECTION', 'null'),

/*
|--------------------------------------------------------------------------
| Domain Event Consumer Connections
|--------------------------------------------------------------------------
|
*/

'connections' => [
'sqs' => [
'queue_url' => env('AWS_DOMAIN_EVENTS_SQS_URL'),
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
'queue' => env('AWS_SQS_URL'),
'region' => env('AWS_DEFAULT_REGION', 'us-east-1'),
],
],


/*
|--------------------------------------------------------------------------
| Domain Event Consumer Events
|
| Set events/jobs to receive messages on events.
|--------------------------------------------------------------------------
|
*/

'events' => [
// 'application.event.subject' => EventJob::class,
],
],
];
7 changes: 7 additions & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0"?>
<ruleset name="GOI-CS">
<rule ref="./vendor/letsgoi/php-code-style/src/phpcs.xml" />

<!-- Folders included -->
<file>src/</file>
</ruleset>
22 changes: 22 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/autoload.php"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
verbose="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="Permissions Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">src/</directory>
</whitelist>
</filter>
</phpunit>
53 changes: 53 additions & 0 deletions src/Consumer/Console/ConsumeCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace Letsgoi\DomainEventsMessaging\Consumer\Console;

use Illuminate\Console\Command;
use Letsgoi\DomainEventsMessaging\Consumer\Consumer;

class ConsumeCommand extends Command
{
/** @var string */
protected $signature = 'domain-events-messaging:consume
{--once : Only consume the next message on the queue}
{--force : Force the consumer to run even in maintenance mode}
{--sleep=3 : Number of seconds to sleep when no message is available}';

/** @var string */
protected $description = 'Consume domain messages from queue';

/** @var Consumer */
private $domainEventConsumer;

public function __construct(Consumer $domainEventConsumer)
{
parent::__construct();

$this->domainEventConsumer = $domainEventConsumer;
}

public function handle(): void
{
if ($this->downForMaintenance()) {
$this->error('The application is in maintenance mode');

return;
}

if ($this->option('once')) {
$this->domainEventConsumer->consume();

$this->info('Event consumer runned!');

return;
}

$this->info('Running event consumer...');
$this->domainEventConsumer->daemon($this->option('sleep'));
}

private function downForMaintenance(): bool
{
return $this->option('force') ? false : $this->laravel->isDownForMaintenance();
}
}
Loading

0 comments on commit 9733661

Please sign in to comment.