Sunday, March 12, 2023
HomePHPRobotically restart Horizon when native PHP recordsdata change

Robotically restart Horizon when native PHP recordsdata change

We have launched a brand new package deal referred to as spatie/laravel-horizon-watcher. This one can robotically restart Horizon after any code change.

On this put up, I would wish to share why we have constructed and the way you should use it.

Why you might want to restart Horizon regionally

Think about you are debugging a queued job regionally. You have began Horizon to imitate manufacturing and are tinkering with the code of the job. After every code change, you’re dispatching the job to confirm if the issue is mounted.

Nothing appears to assist, and the job stays buggy. After some time, you discover that your adjustments within the job seemingly aren’t picked up. After which… it hits you… You forgot to restart Horizon, so none of your code adjustments have been picked up. Your repair from half-hour in the past was the precise one.

I do not find out about you, however I’ve skilled this situation a number of occasions previously few years and misplaced fairly a while.

In case you did not know: if Horizon runs regionally, it will not decide up any adjustments you make in queued jobs, because the PHP lessons are already loaded.

After having misplaced extra time due to it, it determined to spend a while fixing this downside.

Utilizing spatie/laravel-horizon-watcher

Our new package deal spatie/laravel-horizon-watcher can robotically restart Horizon after any code change. It’s meant for use regionally, so putting in it utilizing the --dev flag is finest.

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

After the package deal is put in, you can begin Horizon utilizing this command.

php artisan horizon:watch

When you change any PHP file in your venture, Horizon will robotically restart, so your code adjustments get picked up. Here is what it appears to be like like in motion.

Underneath the hood, this package deal is kind of easy. The WatchHorizonCommand class is lower than 100 traces lengthy. It makes use of our spatie/file-system-watcher package deal to detect code adjustments.

In closing

I am fairly positive that spatie/laravel-horizon-watcher will save a lot of my time sooner or later, and I hope it may be useful for you too.

In a really perfect world, Horizon itself would have a watch command or flag. I created a separate package deal as an alternative of a PR to Horizon as a result of I may make and use this package deal in below an hour. If you wish to ship a PR with this performance to Horizon, be at liberty to take action.

This is not the primary package deal that our group has constructed. On our firm web site, try all our open-source packages in this lengthy checklist. If you wish to assist us, contemplate selecting up any of our paid merchandise.



Please enter your comment!
Please enter your name here

Most Popular

Recent Comments