spatie/laravel-horizon-watcher
Run Horizon locally with auto-restarts on code changes. Adds an artisan command horizon:watch that starts Horizon and restarts it whenever PHP (or configured) files are created, updated, or deleted—great for avoiding stale workers during development.
Installation:
composer require spatie/laravel-horizon-watcher
Publish the config file (if needed):
php artisan vendor:publish --provider="Spatie\HorizonWatcher\HorizonWatcherServiceProvider"
First Use Case:
Run the horizon:watch command in your terminal:
php artisan horizon:watch
Where to Look First:
config/horizon-watcher.php (if published) to customize watched paths or behavior.php artisan horizon:watch --help to explore options like --ignore or --path.Daily Workflow:
php artisan horizon:watch during development.php artisan horizon:watch --ignore="tests/*" --path="app/Jobs,app/Workers"
app/Jobs and app/Workers directories, ignoring tests/.Integration with Laravel Forge/Valet:
package.json or Makefile).Makefile:
watch-horizon:
php artisan horizon:watch --ignore="storage/*"
CI/CD Exclusion:
local-only design)..env has APP_ENV=local when using the command.nodemon or entr:
For advanced users, combine with other tools for broader file watching:
# Watch PHP files and trigger horizon:watch on changes
entr -r 'php artisan horizon:watch' app/
config/horizon-watcher.php:
'paths' => [
app_path('Jobs'),
app_path('Workers'),
base_path('routes/channels.php'), // Include Horizon channel routes
],
Performance Overhead:
vendor/) can slow down file system operations.--ignore to exclude unnecessary directories.Stale Processes:
pkill -f "php artisan horizon"
Makefile:
clean-horizon:
pkill -f "php artisan horizon" || true
Permissions Issues:
www-data on Linux).Ignored Files Not Working:
--ignore flag uses glob patterns. Test patterns in isolation:
php artisan horizon:watch --ignore="tests/*,storage/*"
-v for debug output:
php artisan horizon:watch -v
storage/logs/laravel.log for watcher errors (e.g., permission denied).Custom Watcher Logic:
Extend the Spatie\HorizonWatcher\Commands\WatchHorizon command to add pre/post-restart hooks:
// app/Console/Commands/ExtendedWatchHorizon.php
namespace App\Console\Commands;
use Spatie\HorizonWatcher\Commands\WatchHorizon;
class ExtendedWatchHorizon extends WatchHorizon {
protected function beforeRestart() {
// Custom logic (e.g., log restart)
info('Restarting Horizon due to file changes...');
}
}
Register the command in app/Console/Kernel.php:
protected $commands = [
\App\Console\Commands\ExtendedWatchHorizon::class,
];
Environment-Specific Config: Use Laravel’s environment-based config to disable the watcher in non-local environments:
'enabled' => env('APP_ENV') === 'local',
Docker/Containerized Environments:
# docker-compose.yml
services:
app:
volumes:
- ./:/var/www/html:cached
docker exec -it app php artisan horizon:watch
How can I help you explore Laravel packages today?