Oh Dear is the all-in-one monitoring tool for your entire website. We monitor uptime, SSL certificates, broken links, scheduled tasks and more. You'll get a notifications for us when something's wrong. All that paired with a developer friendly API and kick-ass documentation. O, and you'll also be able to create a public status page under a minute. Start monitoring using our free trial now.

Automatically restart Horizon when local PHP files change

Original – by Freek Van der Herten – 3 minute read

We've released a new package called spatie/laravel-horizon-watcher. This one can automatically restart Horizon after any code change.

In this post, I'd like to share why we've built and how you can use it.

Why you need to restart Horizon locally

Imagine you're debugging a queued job locally. You've started Horizon to mimic production and are tinkering with the code of the job. After each code change, you are dispatching the job to verify if the problem is fixed.

Nothing seems to help, and the job stays buggy. After a while, you notice that your changes in the job seemingly aren't picked up. And then... it hits you... You forgot to restart Horizon, so none of your code changes were picked up. Your fix from 30 minutes ago was the right one.

I don't know about you, but I have experienced this scenario several times in the past few years and lost quite some time.

In case you didn't know: if Horizon runs locally, it won't pick up any changes you make in queued jobs, as the PHP classes are already loaded.

After having lost more time because of it, it decided to spend some time fixing this problem.

Using spatie/laravel-horizon-watcher

Our new package spatie/laravel-horizon-watcher can automatically restart Horizon after any code change. It is meant to be used locally, so installing it using the --dev flag is best.

composer require spatie/laravel-horizon-watcher --dev

After the package is installed, you can start Horizon using this command.

php artisan horizon:watch

If you change any PHP file in your project, Horizon will automatically restart, so your code changes get picked up. Here's what it looks like in action.

Under the hood, this package is quite simple. The WatchHorizonCommand class is less than 100 lines long. It uses our spatie/file-system-watcher package to detect code changes.

In closing

I'm pretty sure that spatie/laravel-horizon-watcher will save much of my time in the future, and I hope it can be helpful for you too.

In an ideal world, Horizon itself would have a watch command or flag. I created a separate package instead of a PR to Horizon because I could make and use this package in under an hour. If you want to send a PR with this functionality to Horizon, feel free to do so.

This isn't the first package that our team has built. On our company website, check out all our open-source packages in this long list. If you want to support us, consider picking up any of our paid products.

Stay up to date with all things Laravel, PHP, and JavaScript.

You can follow me on these platforms:

On all these platforms, regularly share programming tips, and what I myself have learned in ongoing projects.

Every month I send out a newsletter containing lots of interesting stuff for the modern PHP developer.

Expect quick tips & tricks, interesting tutorials, opinions and packages. Because I work with Laravel every day there is an emphasis on that framework.

Rest assured that I will only use your email address to send you the newsletter and will not use it for any other purposes.

Comments

What are your thoughts on "Automatically restart Horizon when local PHP files change"?

Comments powered by Laravel Comments
Want to join the conversation? Log in or create an account to post a comment.