Horizon
- Introduction
- Complete Example: Authorized Queue Dashboard
- Installation
- Running Horizon
- The Dashboard
- Failed Jobs
- Clearing Recorded Jobs
Complete Example: Authorized Queue Dashboard
Install DotBlaze.Horizon in the Blog application, run dotblaze migrate, and add the complete documentation job. Create:
// app/Providers/HorizonAccessProvider.cs
using Blog.Models;
using DotBlaze;
using DotBlaze.Horizon;
using Microsoft.AspNetCore.Builder;
namespace Blog.Providers;
public class HorizonAccessProvider : Provider
{
public override void Boot(WebApplication app)
{
Gate.Define<User>("viewHorizon", user => user.Email == "operator@example.test");
Horizon.Auth(context => Gate.Allows("viewHorizon"));
}
}This requires the gate even in Development. Create the local operator account through registration, sign in, and open /horizon; other users and guests are denied. Replace the demo email condition with your application's operator policy.
Run dotblaze demo:queue, then dotblaze queue:work --connection=database --queue=documentation --stop-when-empty. The job should move from pending to completed; its receipt file proves execution. The dashboard's manager panel requires queue:manager and the supervisor configuration, whereas a plain worker is enough to process this example.
Records are buffered. In a test use await Horizon.Flush() before asserting, as DashboardsTest.cs does, rather than adding arbitrary delays.
Introduction
DotBlaze.Horizon is a dashboard for your queues, like Laravel Horizon. It shows:
- the running queue managers and their supervisors and workers,
- how many jobs wait in each queue, and how long it will take to work through them,
- recent pending, completed, and failed jobs, with their data and exceptions,
- batches,
- metrics: throughput per minute, and run times per job and per queue.
The workers themselves come with DotBlaze: queue:manager runs and balances them, as php artisan horizon does. Horizon adds the dashboard and records the jobs it shows. It works with the database and redis queue drivers.
Installation
Add the package and run its migration, which creates the horizon_jobs table:
dotnet add package DotBlaze.Horizondotblaze migrateThe dashboard is at /horizon. DotBlaze finds the package by itself; there is nothing to register.
Configuration
Horizon works without configuration. To change its defaults, add config/Horizon.cs:
public class HorizonConfig : ConfigFile
{
public override object Values() => new
{
enabled = true,
path = "horizon",
trim = new
{
recent = 60,
completed = 60,
failed = 10080,
},
};
}| Key | Default | Description |
|---|---|---|
enabled | true | Record new job activity. Setting false stops recording; previously stored jobs and metrics remain visible until pruned. |
path | horizon | Where the dashboard is. |
trim.recent | 60 | Minutes to keep pending jobs in the list. |
trim.completed | 60 | Minutes to keep completed jobs. The metrics cover the last hour, so keep at least 60. |
trim.failed | 10080 | Minutes to keep failed jobs in the list (one week). The failed_jobs table is not trimmed. |
Jobs are recorded when they are queued, picked up, and finished, in any process: the web app, queue:work, or queue:manager's workers. Writes happen in the background, about once a second, so recording does not slow down your jobs. Jobs on the sync connection run right away and are not recorded.
Dashboard Authorization
In the Development environment, everyone can open the dashboard. In every other environment, only users that the viewHorizon gate allows can. Define the gate in a service provider:
public override void Boot(WebApplication app)
{
Gate.Define<User>("viewHorizon", user => user.Email is "taylor@example.com" or "ada@example.com");
}Guests are always denied by a gate. To decide in another way, for example by IP address, use Horizon.Auth. It replaces both the environment check and the gate:
Horizon.Auth(context => context.Connection.RemoteIpAddress?.ToString() == "10.0.0.5");The dashboard's routes use the web middleware group, so its buttons (retry, delete) are protected against CSRF.
Running Horizon
Start the workers with the queue manager. Configure its supervisors in config/Queue.cs, as described in The Queue Manager:
dotblaze queue:managerThe dashboard shows each running manager with its supervisors, worker counts, and waiting jobs. A manager writes its status every second to storage/framework/queue-manager, and every few seconds to the default cache store. With a shared cache store (redis or database), the dashboard sees the managers of every server, not only those on the web server's machine.
After a deploy, stop the managers so their process supervisor starts them again with the new code:
dotblaze queue:manager:terminateThe Dashboard
| Page | Shows |
|---|---|
| Dashboard | Whether the managers run, jobs per minute, jobs and failed jobs, processes, the queue with the longest wait, the slowest queue, and the busiest queue. Below that, the current workload per queue and each manager's supervisors. |
| Metrics | Completed jobs per minute over the last hour, and per job class and per queue: the number of jobs, their average and longest run time, and failures. |
| Batches | Every batch with its progress and failures. |
| Pending Jobs | Jobs that wait or are running. |
| Completed Jobs | Jobs that finished. Click a job for its data, attempts, and timings. |
| Failed Jobs | The failed_jobs table, with the exception of each job. |
The dashboard refreshes every three seconds.
The wait of a queue is an estimate: the waiting jobs, times the average run time of its jobs in the last hour, divided by the workers on that queue.
Tags
Like Horizon, the dashboard tags a job with the models in its data. A job with a Post property holding post 1 is tagged Post:1, because models in a job's data are stored as their id.
Failed Jobs
The Failed Jobs page lists the failed_jobs table. Open a failed job to see its exception and data. Retry pushes it back onto the queue it failed on, like queue:retry <id>, and Retry all retries every failed job. Delete removes it from failed_jobs.
Clearing Recorded Jobs
Recorded jobs are trimmed automatically (see Configuration). To delete them all at once:
dotblaze horizon:clearThis does not touch the queues or the failed_jobs table.