Hangfire

Hangfire is one of the most widely used background job processing libraries in the .NET community, featuring a built-in monitoring dashboard. Brighter provides first-class integration with Hangfire for scheduler functionality, making it an excellent choice for production systems requiring easy setup, visual monitoring, and persistent scheduling.

Production Recommendation

Hangfire is highly recommended for production use alongside Quartz.NET as one of the two primary production schedulers for Brighter.

Why Choose Hangfire?

  • Built-in Dashboard: Web UI for monitoring jobs, queues, and servers

  • Easy Setup: Simple configuration and minimal boilerplate

  • Persistent: Jobs stored in databases (SQL Server, PostgreSQL, Redis, etc.)

  • Reliable: Jobs survive application restarts and crashes

  • Cancellation: Full support for cancelling scheduled jobs

  • Automatic Retries: Configurable retry policies for failed jobs

  • Multiple Storage: SQL Server, PostgreSQL, MySQL, Redis, MongoDB

  • Background Processing: Beyond scheduling - fire-and-forget, delayed, recurring jobs

⚠️ Important: Strong Naming Limitation

The Paramore.Brighter.MessageScheduler.Hangfire assembly is NOT strong-named.

This is due to Hangfire itself not using strong naming. If your application requires all assemblies to be strong-named (for example, for enterprise policy compliance), you will need to use an alternative scheduler (Quartz.NET or AWS/Azure schedulers).

Why this matters:

  • Cannot mix strong-named and non-strong-named assemblies in some enterprise environments

  • May cause issues with certain security policies

  • Use Quartz.NET if strong naming is a hard requirement

Hangfire Overview

Hangfire is a comprehensive background job processing library that provides:

  • Persistent Job Storage: Store jobs in various backends

  • Automatic Retry: Retry failed jobs with exponential backoff

  • Dashboard: Web-based monitoring UI

  • Multiple Queue Support: Organize jobs into different queues

  • Job Continuations: Chain jobs together

  • Recurring Jobs: Cron-based scheduling

  • Job Filters: Intercept job execution

  • Distributed Processing: Multiple servers can process jobs

For more information, visit the Hangfire documentation.

How Brighter Integrates with Hangfire

Brighter integrates with Hangfire through:

  1. BrighterHangfireSchedulerJob: A Hangfire job that executes scheduled Brighter messages

  2. HangfireMessageSchedulerFactory: Factory that creates Brighter's message scheduler backed by Hangfire

  3. JobActivator Integration: Uses Brighter's DI container to resolve jobs

When you schedule a message with Brighter:

  1. Brighter creates a Hangfire background job with the message payload

  2. Hangfire persists the job to its storage backend

  3. At the scheduled time, Hangfire fires the job

  4. BrighterHangfireSchedulerJob receives the job execution

  5. BrighterHangfireSchedulerJob dispatches the message via Brighter's Command Processor

  6. Your handler executes

  7. Job appears in Hangfire dashboard with status

NuGet Packages

Install the required NuGet packages:

For persistence, add a Hangfire storage package:

Configuration

Basic Configuration

Configure Brighter with Hangfire scheduler:

Configuration with PostgreSQL

Configuration with Redis

Configuration with Custom Queue

Organize Brighter jobs into a specific queue:

Benefits:

  • Isolate Brighter jobs from other Hangfire jobs

  • Configure different server instances for different queues

  • Monitor Brighter jobs separately in dashboard

Storage Options

Hangfire supports multiple persistent storage backends:

SQL Server

PostgreSQL

MySQL

Redis

In-Memory (Development Only)

⚠️ Warning: In-memory storage loses all jobs on restart. Use persistent storage for production.

Dashboard

Hangfire includes a powerful web-based dashboard for monitoring jobs.

Basic Dashboard Setup

Dashboard with Authentication

Secure the dashboard with authorization:

Dashboard with Custom Path and Options

Dashboard Features

The Hangfire dashboard provides:

  • Jobs: View all jobs (scheduled, processing, succeeded, failed)

  • Retries: Monitor automatic retry attempts

  • Recurring Jobs: Manage recurring jobs

  • Servers: View active Hangfire servers

  • Succeeded/Failed: Historical job execution data

  • Real-time Updates: Live job status updates

  • Job Details: Inspect job arguments and exceptions

  • Manual Actions: Requeue, delete, or retry jobs

Code Examples

Basic Scheduling

Cancelling a Scheduled Job

Scheduling with Absolute Time

High Availability with Multiple Servers

Hangfire supports multiple server instances for high availability:

How HA works:

  1. Multiple servers connect to the same storage backend

  2. Jobs are distributed among active servers

  3. If a server fails, other servers take over its jobs

  4. Each server processes jobs independently

  5. Dashboard shows all servers and their status

Best practices for HA:

  • Use shared persistent storage (SQL Server, PostgreSQL, etc.)

  • Configure unique server names

  • Monitor server health in dashboard

  • Set appropriate worker counts per server

  • Use queues to control job distribution

Monitoring and Observability

Job Filters

Monitor job execution with filters:

Health Checks

Monitor Hangfire health:

Best Practices

1. Always Use Persistent Storage in Production

2. Secure the Dashboard

3. Use Custom Queues for Organization

4. Store Scheduler IDs for Cancellation

5. Configure Appropriate Worker Counts

6. Monitor the Dashboard Regularly

7. Handle Timezone Correctly

8. Configure Automatic Retry Policies

Troubleshooting

Jobs Not Executing

Symptom: Jobs scheduled but never execute

Possible Causes:

  1. Hangfire server not started

  2. Database connectivity issues

  3. BrighterHangfireSchedulerJob not registered

  4. No workers processing the queue

Solutions:

Jobs Executing Multiple Times

Symptom: Same job executes multiple times

Cause: Multiple servers or configuration issues

Solution:

Dashboard Not Loading

Symptom: Dashboard URL returns 404

Possible Causes:

  1. Dashboard not registered

  2. Incorrect URL path

  3. Authentication blocking access

Solutions:

Migration from Other Schedulers

From InMemory Scheduler

From Quartz

If migrating from Quartz, you can run both schedulers during transition:

Comparison: Hangfire vs Quartz

Feature
Hangfire
Quartz.NET

Dashboard

✅ Built-in web UI

⚠️ Limited/third-party

Setup Complexity

✅ Easy

⚠️ Moderate

Persistence

✅ Multiple options

✅ Multiple options

Clustering

✅ Yes

✅ Yes

Cancellation

✅ Yes

✅ Yes

Strong Naming

❌ No

✅ Yes

Automatic Retry

✅ Built-in

⚠️ Manual

Recurring Jobs

✅ Native support

⚠️ Via triggers

Monitoring

✅ Excellent (dashboard)

⚠️ Limited

Choose Hangfire when:

  • You want a built-in monitoring dashboard

  • Easy setup is a priority

  • Strong naming is not required

  • You need automatic retry handling

Choose Quartz when:

  • Strong naming is required

  • You need advanced trigger types (Cron, Calendar)

  • You prefer more control over scheduling logic

  • Dashboard is not essential

Summary

Hangfire is an excellent production scheduler for Brighter offering:

  • Dashboard: Built-in web UI for monitoring and management

  • Easy Setup: Minimal configuration required

  • Persistent: Jobs survive restarts with multiple storage options

  • Reliable: Automatic retries and distributed processing

  • Visual Monitoring: Real-time job status and history

  • ⚠️ Strong Naming: NOT strong-named (use Quartz if required)

Use Hangfire when you need a production scheduler with a built-in dashboard and easy setup. If strong naming is required, use Quartz.NET instead.

Last updated

Was this helpful?