Apr 30, 2018

Setting up Horizon and Spark with Laravel Forge

I have recently started working on a new project that I expect to show in a couple of days. When I started, the technical stack was a no brainer for me: Laravel, Spark, Forge and Algolia.

Unfortunately, I found that it was not clear how to combine them all. I will share with you the few tips I have put together to make Laravel Horizon work smoothly with Kiosk (a feature of Laravel Spark), and how to setup the supervior with Laravel Forge.

Laravel Horizon

Horizon is the best way to manage your queues with Laravel. It comes with a nice dashboard to monitor your jobs but you have to setup something for authentication. By default, you can only access it in local env.

The documented way, is to use the Auth callback:

1Horizon::auth(function ($request) {
2 // return true / false;
3});

By I didn't want to implement any custom logic and duplicate it. Laravel Spark ships with Kiosk, a dedicated dashboard area for devs and admins. I thought it would make sense to just use that to authenticate to the Horizon dashboard.

It's undocumented and it's not in the default config file but you can pass a list of middleware to use for Horizon.

In your config/horizon.php:

1return [
2 // ...
3 // Existing config
4 // ...
5 
6 'middleware' => ['web', 'dev'],
7];

Now, Laravel will check if the logged in user is a developer (in the Spark sense of things) but it will only allow access in local env.

You need to override the Horizon::Auth callback. In your AppServiceProvider:

1public function boot()
2{
3 \Horizon::auth(function () { return true; });
4}

And you're good to go! :rocket:

Setting up supervisor for Horizon with Laravel Forge

I'm not a expert in server management so I chose to use Laravel Forge but I couldn't figure out how to make sure Laravel Horizon was always running.

Add a deamon

Under Server Details > Deamon, add a deamon to start Horizon. This will make sure that every time the process is terminated, the server start it again.

Laravel Forge configuration for Horizon deamon

Terminate Horizon after new deployment

Because the process will always use the version of the code it was booted with, it has to be terminated to use the latest version of your code.

In your deployment script, you only have to add the following line at the very end.

1php artisan horizon:terminate

Restart Horizon regularly

PHP usually isn't doing to good with long running processes so I prefer restarting Horizon regularly.

You can achieve that in the Server details > Scheduler section.

Laravel Forge configuration for Horizon deamon

That's it! I hope it will help some of you.