This guide demonstrates how to set up and manage queues with Laravel Horizon, including installing the required package, configuring Redis, running the service, managing processes with PM2, and monitoring performance through Horizon and Telescope.
Steps1. Install thelaravel/horizon
package composer require laravel/horizon
php artisan horizon:install
INFO Installing Horizon resources.
Service Provider ......................................................... 0.66ms DONE
Configuration ............................................................ 0.35ms DONE
INFO Horizon scaffolding installed successfully.
REDIS_HOST
to point to the correct host (To avoid error Predis\Connection\ConnectionException
) Error Predis\Connection\ConnectionException
php_network_getaddresses: getaddrinfo for redis failed: Temporary failure in name resolution [tcp://redis:6379].
REDIS_HOST=127.0.0.1
sudo apt install redis-server
systemd
as the supervision
method ...
# If you run Redis from upstart or systemd, Redis can interact with your
# supervision tree. Options:
# supervised no - no supervision interaction
# supervised upstart - signal upstart by putting Redis into SIGSTOP mode
# supervised systemd - signal systemd by writing READY=1 to $NOTIFY_SOCKET
# supervised auto - detect upstart or systemd method based on
# UPSTART_JOB or NOTIFY_SOCKET environment variables
# Note: these supervision methods only signal "process is ready."
# They do not enable continuous liveness pings back to your supervisor.
supervised systemd
...
sudo systemctl restart redis.service
sudo systemctl status redis
● redis-server.service - Advanced key-value store
Loaded: loaded (/lib/systemd/system/redis-server.service; enabled; vendor preset: enabled)
Active: active (running) since Wed 2024-11-13 09:11:49 UTC; 13s ago
Docs: http://redis.io/documentation,
man:redis-server(1)
Process: 1796508 ExecStart=/usr/bin/redis-server /etc/redis/redis.conf (code=exited, status=0/SUCCESS)
Main PID: 1796517 (redis-server)
Tasks: 4 (limit: 1131)
Memory: 2.0M
CGroup: /system.slice/redis-server.service
└─1796517 /usr/bin/redis-server 127.0.0.1:6379
Nov 13 09:11:49 ubuntu-s-1vcpu-1gb-sgp1-01 systemd[1]: redis-server.service: Succeeded.
Nov 13 09:11:49 ubuntu-s-1vcpu-1gb-sgp1-01 systemd[1]: Stopped Advanced key-value store.
Nov 13 09:11:49 ubuntu-s-1vcpu-1gb-sgp1-01 systemd[1]: Starting Advanced key-value store...
Nov 13 09:11:49 ubuntu-s-1vcpu-1gb-sgp1-01 systemd[1]: redis-server.service: Can't open PID file /run/redis/redis-serve>
Nov 13 09:11:49 ubuntu-s-1vcpu-1gb-sgp1-01 systemd[1]: Started Advanced key-value store.
ping
command redis-cli
127.0.0.1:6379> ping
PONG
npm install pm2@latest -g
module.exports = {
apps : [{
name: 'Laravel Horizon',
script: 'php',
args: 'artisan horizon',
}],
}
pm2 start ecosystem.config.cjs
[PM2][WARN] Applications Laravel Horizon not running, starting...
[PM2] App [Laravel Horizon] launched (1 instances)
┌────┬────────────────────┬─────────────┬─────────┬─────────┬──────────┬────────┬─────────┬───────────┬──────────┬──────────┬──────────┬──────────┐
│ id │ name │ namespace │ version │ mode │ pid │ uptime │ restart │ status │ cpu │ mem │ user │ watching │
├────┼────────────────────┼─────────────┼─────────┼─────────┼──────────┼────────┼─────────┼───────────┼──────────┼──────────┼──────────┼──────────┤
│ 0 │ Laravel Horizon │ default │ N/A │ fork │ 1808674 │ 0s │ 0 │ online │ 0% │ 20.5mb │ root │ disabled │
└────┴────────────────────┴─────────────┴─────────┴─────────┴──────────┴────────┴─────────┴───────────┴──────────┴──────────┴──────────┴──────────┘
https://laravel.get-go.dev/tinker
to send batch of queueable emails https://laravel.get-go.dev/horizon
and you should see the Horizon dashboard page https://laravel.get-go.dev/telescope
to have more insight of the log entries, queued jobs, mail, and more