Skip to content

Commit

Permalink
Add support for Horizon
Browse files Browse the repository at this point in the history
Signed-off-by: Cy Rossignol <[email protected]>
  • Loading branch information
cyrossignol committed Mar 23, 2018
1 parent 5df5002 commit fe437ff
Show file tree
Hide file tree
Showing 24 changed files with 1,481 additions and 51 deletions.
18 changes: 15 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
language: php
dist: trusty
sudo: false

php:
- 5.6
Expand All @@ -11,7 +12,9 @@ env:
- LARAVEL_VERSION=">=5.4.0 <5.4.20"
- LARAVEL_VERSION=">=5.4.20 <5.5.0"
- LARAVEL_VERSION="5.5.*"
- LARAVEL_VERSION="5.5.*" HORIZON=1
- LARAVEL_VERSION="5.6.*"
- LARAVEL_VERSION="5.6.*" HORIZON=1
- LUMEN_VERSION="5.4.*"
- LUMEN_VERSION="5.5.*"
- LUMEN_VERSION="5.6.*"
Expand All @@ -20,14 +23,22 @@ matrix:
exclude:
- php: 5.6
env: LARAVEL_VERSION="5.5.*"
- php: 5.6
env: LARAVEL_VERSION="5.5.*" HORIZON=1
- php: 5.6
env: LUMEN_VERSION="5.5.*"
- php: 5.6
env: LARAVEL_VERSION="5.6.*"
- php: 5.6
env: LARAVEL_VERSION="5.6.*" HORIZON=1
- php: 5.6
env: LUMEN_VERSION="5.6.*"
- php: 7.0
env: LARAVEL_VERSION="5.5.*" HORIZON=1
- php: 7.0
env: LARAVEL_VERSION="5.6.*"
- php: 7.0
env: LARAVEL_VERSION="5.6.*" HORIZON=1
- php: 7.0
env: LUMEN_VERSION="5.6.*"

Expand All @@ -36,9 +47,10 @@ before_install:
- if [ -n "$LARAVEL_VERSION" ]; then composer require --no-update "laravel/framework:$LARAVEL_VERSION"; fi
- if [ -n "$LUMEN_VERSION" ]; then composer remove --dev --no-update "laravel/framework"; fi
- if [ -n "$LUMEN_VERSION" ]; then composer require --no-update "laravel/lumen-framework:$LUMEN_VERSION"; fi
- if [ -z "$HORIZON" ]; then composer remove --dev --no-update "laravel/horizon"; fi

install: composer install --prefer-dist --no-interaction --no-suggest
install: travis_retry composer install --prefer-dist --no-interaction --no-suggest

before_script: SUPERVISE=no ./start-cluster.sh || { cat ./cluster/*.log; false; }
before_script: SUPERVISE=no travis_retry ./start-cluster.sh || { cat ./cluster/*.log; false; }

script: vendor/bin/phpunit ${LUMEN_VERSION:+--exclude-group laravel-only}
script: vendor/bin/phpunit --exclude-group ${LUMEN_VERSION:+laravel-only},${HORIZON:-horizon}
81 changes: 76 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ erases stored session information as well.

This package wraps the configuration of Laravel's broadcasting, cache, session,
and queue APIs for Sentinel with the ability to set options for our Redis
services independently. We configure the package separately from Laravel's
standard Redis configuration so we can choose to use the Sentinel connections
as needed by the environment. A developer may use a standalone Redis server in
their local environment, while production environments operate a Redis Sentinel
set of servers.
services independently. It adds Sentinel support for [Laravel Horizon][s-horizon]
and fixes other compatibility issues.

We configure the package separately from Laravel's standard Redis configuration
so we can choose to use the Sentinel connections as needed by the environment.
A developer may use a standalone Redis server in their local environment, while
production environments operate a Redis Sentinel set of servers.


Contents
Expand All @@ -42,6 +44,7 @@ Contents
- [Executing Redis Commands (RedisSentinel Facade)][s-facade]
- [Dependency Injection][s-dependency-injection]
- [Other Sentinel Considerations][s-considerations]
- [Laravel Horizon][s-horizon]
- [Testing](#testing)
- [License](#license)
- [Appendix: Environment Variables][s-appx-env-vars]
Expand Down Expand Up @@ -598,6 +601,9 @@ service (`app('redis')`, etc) will operate using the Sentinel connections.
This makes it easier for developers to use a standalone Redis server in their
local environments and switch to a full Sentinel set of servers in production.

**Note:** When using the package with [Laravel Horizon][s-horizon], this change
will cause Horizon to run over a Sentinel connection as well.


Executing Redis Commands (RedisSentinel Facade)
-------------------------------------------------
Expand Down Expand Up @@ -726,6 +732,67 @@ connection failure handling. For high-availability, use the Laravel API by
passing a closure to `subscribe()` or `psubscribe()`.


Laravel Horizon
---------------

Versions 2.4.0 and greater of this package provide for the use of Sentinel
connections with the [Laravel Horizon][laravel-horizon] queue management tool
in compatible applications (Laravel 5.5+).

After [installing Horizon][laravel-horizon-install-docs], we need to update
some configuration settings in *config/horizon.php*:

If needed, change `'use' => 'default'` to the name of the Sentinel connection
to use for the Horizon backend as configured in *config/database.php*.

**Important:** The standard `'redis'` connections array in *config/database.php*
must contain an element with the same key as the Sentinel connection specified
for the `'use'` directive or Horizon throws an exception. Currently, Horizon
does not provide a way for this package to handle this behavior, but an
(in-progress) pull request may eliminate this requirement in the future. This
element can contain any value (but a matching Redis connection config seems
most appropriate).

Change the backend driver for Horizon's internal metadata to `'redis-sentinel'`
by adding the following element to the top-level array:

```php
'driver' => env('HORIZON_DRIVER', 'redis');
```

...and assign the value of `redis-sentinel` to `HORIZON_DRIVER` in *.env*.


Then, add an entry to the `'waits'` array for any Sentinel queues:

```php
'waits' => [
...
'redis-sentinel:default' => 60,
],
```

Next, change the connection driver to `redis-sentinel` for each of the queue
workers that should use Sentinel connections in the `'environments'` block:

```php
...
'supervisor-1' => [
'connection' => 'redis-sentinel',
...
],
...
```

Horizon will now use the application's Redis Sentinel connections to monitor
and process our queues.

**Note:** If we already configured the package to [override Laravel's standard
Redis API][s-override-redis-api] (by setting `REDIS_DRIVER` to `redis-sentinel`,
for example), we don't need to change `HORIZON_DRIVER` to `'redis-sentinel'`.
The package already routes all Redis operations through Sentinel connections.


Testing
-------

Expand Down Expand Up @@ -1117,6 +1184,7 @@ Sentinel Documentation][sentinel].
[s-env-config]: #environment-based-configuration
[s-env-config-examples]: #environment-based-configuration-examples
[s-facade]: #executing-redis-commands-redissentinel-facade
[s-horizon]: #laravel-horizon
[s-hybrid-config]: #hybrid-configuration
[s-integration-tests]: #integration-tests
[s-multi-sentinel-example]: #multi-sentinel-connection-configuration
Expand All @@ -1129,6 +1197,9 @@ Sentinel Documentation][sentinel].
[s-standard-config-examples]: #configuration-file-examples

[laravel-env-docs]: https://laravel.com/docs/configuration#environment-configuration
[laravel-horizon]: https://horizon.laravel.com
[laravel-horizon-docs]: https://laravel.com/docs/horizon
[laravel-horizon-install-docs]: https://laravel.com/docs/horizon#installation
[laravel-package-discovery-docs]: https://laravel.com/docs/packages#package-discovery
[laravel-redis-api-docs]: https://laravel.com/docs/redis#interacting-with-redis
[laravel-redis-docs]: https://laravel.com/docs/redis
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
},
"require-dev": {
"laravel/framework": "^5.4",
"laravel/horizon": "^1.0",
"laravel/lumen-framework": "^5.4",
"mockery/mockery": "0.9.*",
"phpunit/phpunit": "^4.8"
Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit bootstrap="vendor/autoload.php"
<phpunit bootstrap="tests/bootstrap.php"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
Expand Down
Loading

0 comments on commit fe437ff

Please sign in to comment.