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.
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.
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.