Skip to content
This repository was archived by the owner on Sep 23, 2024. It is now read-only.

Commit c14bbe9

Browse files
author
Steve Porter
committed
feat: initial commit
0 parents  commit c14bbe9

File tree

11 files changed

+489
-0
lines changed

11 files changed

+489
-0
lines changed

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.DS_Store
2+
.phpunit.result.cache
3+
/.idea
4+
/.vscode
5+
/vendor
6+
composer.lock
7+
composer.phar
8+
Thumbs.db

.styleci.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
preset: laravel
2+
3+
enabled:
4+
- alpha_ordered_imports
5+
6+
disabled:
7+
- length_ordered_imports

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Changelog
2+
3+
All notable changes to `laravel-horizon-rabbitmq` will be documented in this file
4+
5+
## 0.1.0 - 2018-09-14
6+
- BETA release

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2018 DesignMyNight
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 all
13+
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 THE
21+
SOFTWARE.

composer.json

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"name": "designmynight/laravel-horizon-rabbitmq",
3+
"description": "A package to support horizon with RabbitMQ",
4+
"homepage": "https://github.com/designmynight/laravel-horizon-rabbitmq",
5+
"license": "MIT",
6+
"keywords": [
7+
"laravel",
8+
"laravel-horizon",
9+
"laravel-horizon-rabbitmq",
10+
"rabbitmq",
11+
"designmynight"
12+
],
13+
"require": {
14+
"illuminate/support": "^5.6",
15+
"laravel/horizon": "^1.4",
16+
"vladimir-yuldashev/laravel-queue-rabbitmq": "7.1.*"
17+
},
18+
"autoload": {
19+
"psr-4": {
20+
"DesignMyNight\\Laravel\\Horizon\\": "src"
21+
}
22+
},
23+
"authors": [
24+
{
25+
"name": "Steve Porter",
26+
"email": "[email protected]",
27+
"role": "Developer"
28+
}
29+
],
30+
"extra": {
31+
"laravel": {
32+
"providers": [
33+
"DesignMyNight\\Laravel\\Horizon\\HorizonServiceProvider"
34+
]
35+
}
36+
}
37+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
namespace DesignMyNight\Laravel\Horizon\Connectors;
4+
5+
use DesignMyNight\Laravel\Horizon\RabbitMQQueue;
6+
use Enqueue\AmqpTools\DelayStrategyAware;
7+
use Enqueue\AmqpTools\RabbitMqDlxDelayStrategy;
8+
use Illuminate\Contracts\Events\Dispatcher;
9+
use Illuminate\Contracts\Queue\Queue;
10+
use Illuminate\Queue\Events\WorkerStopping;
11+
use Interop\Amqp\AmqpConnectionFactory as InteropAmqpConnectionFactory;
12+
use Interop\Amqp\AmqpConnectionFactory;
13+
use Interop\Amqp\AmqpContext;
14+
use VladimirYuldashev\LaravelQueueRabbitMQ\Queue\Connectors\RabbitMQConnector as BaseConnector;
15+
16+
class RabbitMQConnector extends BaseConnector
17+
{
18+
protected $dispatcher;
19+
20+
public function __construct(Dispatcher $dispatcher)
21+
{
22+
$this->dispatcher = $dispatcher;
23+
}
24+
25+
/**
26+
* @inheritDoc
27+
*/
28+
public function connect(array $config): Queue
29+
{
30+
if (false === array_key_exists('factory_class', $config)) {
31+
throw new \LogicException('The factory_class option is missing though it is required.');
32+
}
33+
34+
$factoryClass = $config['factory_class'];
35+
if (false === class_exists($factoryClass) || false === (new \ReflectionClass($factoryClass))->implementsInterface(InteropAmqpConnectionFactory::class)) {
36+
throw new \LogicException(sprintf('The factory_class option has to be valid class that implements "%s"', InteropAmqpConnectionFactory::class));
37+
}
38+
39+
/** @var AmqpConnectionFactory $factory */
40+
$factory = new $factoryClass([
41+
'dsn' => $config['dsn'],
42+
'host' => $config['host'],
43+
'port' => $config['port'],
44+
'user' => $config['login'],
45+
'pass' => $config['password'],
46+
'vhost' => $config['vhost'],
47+
'ssl_on' => $config['ssl_params']['ssl_on'],
48+
'ssl_verify' => $config['ssl_params']['verify_peer'],
49+
'ssl_cacert' => $config['ssl_params']['cafile'],
50+
'ssl_cert' => $config['ssl_params']['local_cert'],
51+
'ssl_key' => $config['ssl_params']['local_key'],
52+
'ssl_passphrase' => $config['ssl_params']['passphrase'],
53+
]);
54+
55+
if ($factory instanceof DelayStrategyAware) {
56+
$factory->setDelayStrategy(new RabbitMqDlxDelayStrategy());
57+
}
58+
59+
/** @var AmqpContext $context */
60+
$context = $factory->createContext();
61+
62+
$this->dispatcher->listen(WorkerStopping::class, function () use ($context) {
63+
$context->close();
64+
});
65+
66+
return new RabbitMQQueue($context, $config);
67+
}
68+
}

src/HorizonServiceProvider.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
namespace DesignMyNight\Laravel\Horizon;
4+
5+
use DesignMyNight\Laravel\Horizon\Connectors\RabbitMQConnector;
6+
use Illuminate\Contracts\Events\Dispatcher;
7+
use Illuminate\Support\ServiceProvider;
8+
9+
class HorizonServiceProvider extends ServiceProvider
10+
{
11+
/**
12+
* All of the Horizon event / listener mappings.
13+
*
14+
* @var array
15+
*/
16+
protected $events = [
17+
\Illuminate\Queue\Events\JobFailed::class => [
18+
Listeners\MarshalFailedEvent::class,
19+
],
20+
];
21+
22+
/**
23+
* Register the Horizon job events.
24+
*
25+
* @return void
26+
*/
27+
protected function registerEvents()
28+
{
29+
$events = $this->app->make(Dispatcher::class);
30+
31+
foreach ($this->events as $event => $listeners) {
32+
foreach ($listeners as $listener) {
33+
$events->listen($event, $listener);
34+
}
35+
}
36+
}
37+
38+
/**
39+
* Register the custom queue connectors for Horizon.
40+
*
41+
* @return void
42+
*/
43+
protected function registerQueueConnectors()
44+
{
45+
$queue = $this->app['queue'];
46+
47+
$queue->addConnector('rabbitmq', function () {
48+
return new RabbitMQConnector($this->app['events']);
49+
});
50+
}
51+
52+
/**
53+
* Register any application services.
54+
*
55+
* @return void
56+
*/
57+
public function boot()
58+
{
59+
$this->registerEvents();
60+
$this->registerQueueConnectors();
61+
}
62+
}

src/Jobs/RabbitMQJob.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace DesignMyNight\Laravel\Horizon\Jobs;
4+
5+
use VladimirYuldashev\LaravelQueueRabbitMQ\Queue\Jobs\RabbitMQJob as BaseJob;
6+
7+
class RabbitMQJob extends BaseJob
8+
{
9+
/**
10+
* @inheritDoc
11+
*/
12+
public function delete()
13+
{
14+
parent::delete();
15+
16+
$this->connection->deleteReserved($this->queue, $this);
17+
}
18+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace DesignMyNight\Laravel\Horizon\Listeners;
4+
5+
use Illuminate\Contracts\Events\Dispatcher;
6+
use Illuminate\Queue\Events\JobFailed as LaravelJobFailed;
7+
use Laravel\Horizon\Events\JobFailed;
8+
9+
class MarshalFailedEvent
10+
{
11+
/**
12+
* The event dispatcher implementation.
13+
*
14+
* @var \Illuminate\Contracts\Events\Dispatcher
15+
*/
16+
public $events;
17+
18+
/**
19+
* Create a new listener instance.
20+
*
21+
* @param \Illuminate\Contracts\Events\Dispatcher $events
22+
* @return void
23+
*/
24+
public function __construct(Dispatcher $events)
25+
{
26+
$this->events = $events;
27+
}
28+
29+
/**
30+
* Handle the event.
31+
*
32+
* @param \Illuminate\Queue\Events\JobFailed $event
33+
* @return void
34+
*/
35+
public function handle(LaravelJobFailed $event)
36+
{
37+
$this->events->dispatch((new JobFailed(
38+
$event->exception, $event->job, $event->job->getRawBody()
39+
))->connection($event->connectionName)->queue($event->job->getQueue()));
40+
}
41+
}

src/README.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
Laravel Horizon RabbitMQ
2+
===============
3+
4+
[![Latest Stable Version](http://img.shields.io/github/release/designmynight/laravel-horizon-rabbitmq.svg)](https://packagist.org/packages/designmynight/laravel-horizon-rabbitmq) [![Total Downloads](http://img.shields.io/packagist/dm/designmynight/laravel-horizon-rabbitmq.svg)](https://packagist.org/packages/designmynight/laravel-horizon-rabbitmq)
5+
[![StyleCI](https://github.styleci.io/repos/147424037/shield?branch=master)](https://github.styleci.io/repos/147424037)
6+
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
7+
8+
A service provider to add support for a [RabbitMQ](https://github.com/vyuldashev/laravel-queue-rabbitmq) queue driver to [Laravel Horizon](https://github.com/laravel/horizon).
9+
10+
Table of contents
11+
-----------------
12+
* [Installation](#installation)
13+
* [Configuration](#configuration)
14+
15+
Installation
16+
------------
17+
18+
Installation using composer:
19+
20+
```sh
21+
composer require designmynight/laravel-horizon-rabbitmq
22+
```
23+
24+
### Laravel version Compatibility
25+
26+
Laravel | Package |
27+
:---------|:--------|
28+
5.6.x | 0.1.x |
29+
30+
And add the service provider in `config/app.php`:
31+
32+
```php
33+
DesignMyNight\Laravel\Horizon\HorizonServiceProvider::class,
34+
```
35+
36+
Configuration
37+
------------
38+
39+
Be sure to set the connection of your supervisor queue to `rabbitmq` in `config/horizon.php`:
40+
41+
```php
42+
'environments' => [
43+
'production' => [
44+
'supervisor-1' => [
45+
'connection' => 'rabbitmq',
46+
'queue' => ['default'],
47+
'balance' => 'simple',
48+
'processes' => 8,
49+
'tries' => 3,
50+
],
51+
],
52+
```
53+
54+
55+
The service provider will overide the default laravel horizon redis queue and redis job classes with a RabbitMQ implentation in order to trigger the necessary events for horizon to function correctly.

0 commit comments

Comments
 (0)