Skip to content

Horizon ​

Complete Example: Authorized Queue Dashboard ​

Install DotBlaze.Horizon in the Blog application, run dotblaze migrate, and add the complete documentation job. Create:

csharp
// 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:

bash
dotnet add package DotBlaze.Horizon
bash
dotblaze migrate

The 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:

csharp
public class HorizonConfig : ConfigFile
{
    public override object Values() => new
    {
        enabled = true,
        path = "horizon",
        trim = new
        {
            recent = 60,
            completed = 60,
            failed = 10080,
        },
    };
}
KeyDefaultDescription
enabledtrueRecord new job activity. Setting false stops recording; previously stored jobs and metrics remain visible until pruned.
pathhorizonWhere the dashboard is.
trim.recent60Minutes to keep pending jobs in the list.
trim.completed60Minutes to keep completed jobs. The metrics cover the last hour, so keep at least 60.
trim.failed10080Minutes 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:

csharp
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:

csharp
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:

bash
dotblaze queue:manager

The 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:

bash
dotblaze queue:manager:terminate

The Dashboard ​

PageShows
DashboardWhether 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.
MetricsCompleted 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.
BatchesEvery batch with its progress and failures.
Pending JobsJobs that wait or are running.
Completed JobsJobs that finished. Click a job for its data, attempts, and timings.
Failed JobsThe 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:

bash
dotblaze horizon:clear

This does not touch the queues or the failed_jobs table.