Skip to content

onursokullu/Hangfire.Poc

Repository files navigation

Hangfire Worker Service

A .NET 8 Worker Service that demonstrates Hangfire integration with Redis storage for distributed job processing.

Features

  • Hangfire Integration: Uses Hangfire with Redis storage for distributed job processing
  • Worker Service: Background service that processes jobs without web interface
  • Environment-based Configuration: Supports Development, Test, Staging, and Production environments
  • Job Scheduling: Supports Fire-and-forget, Delayed, Recurring, and Queue-based jobs
  • Structured Logging: Uses Serilog for comprehensive logging
  • Windows Service Support: Can run as a Windows service
  • Docker Support: Includes Docker and Docker Compose configuration

Prerequisites

  • .NET 8 SDK
  • Redis Server (for distributed job storage)
  • Docker (optional, for containerized deployment)

Configuration

Redis Configuration

The application uses different Redis configurations based on the environment:

  • Development: localhost:6379, Database 0
  • Test/Staging: redis.local:6379, Database 0
  • Production: Configurable Redis endpoint

Hangfire Configuration

Worker service configuration in appsettings.json:

{
  "HangfireConfiguration": {
    "WorkerCount": 20,
    "EnableMetrics": true,
    "EnableHeartbeat": true,
    "HeartbeatInterval": "00:00:30"
  }
}

Running the Application

Development (Local)

  1. Start Redis Server:

    # Using Docker
    docker run -d -p 6379:6379 redis:7-alpine
    
    # Or install Redis locally
  2. Run the Worker Service:

    dotnet run --environment Development
  3. Monitor Jobs:

    • Jobs are processed in the background
    • Check application logs for job execution details
    • Use Redis CLI to inspect job storage: redis-cli

Using Docker Compose

  1. Start the entire stack:

    docker-compose up -d
  2. Monitor the service:

    • Check logs: docker-compose logs -f hangfire-worker
    • Monitor Redis: docker-compose exec redis redis-cli

As Windows Service

  1. Publish the application:

    dotnet publish -c Release -o ./publish
  2. Install as Windows Service:

    sc create "Hangfire Service" binPath="C:\path\to\publish\Hangfire.Poc.exe"
    sc start "Hangfire Service"

Job Management

The worker service automatically processes jobs from these queues:

  • critical - High priority jobs
  • default - Standard priority jobs
  • normal - Normal priority jobs
  • low - Low priority jobs

Project Structure

├── Configuration/          # Configuration models
├── Extensions/            # Service extensions and Hangfire setup
├── Services/              # Background services and job processors
├── Program.cs             # Application entry point
├── appsettings.*.json     # Environment-specific configurations
├── Dockerfile            # Docker configuration
├── docker-compose.yml    # Docker Compose for development
└── README.md             # This file

Key Components

  • HangfireServiceExtensions: Configures Hangfire services and Redis storage
  • HangfireWorkerService: Background service for job processing and scheduling
  • JobService: Sample job implementations with different queue priorities
  • Configuration Classes: Strongly-typed configuration models

Sample Jobs

The worker service includes sample jobs that demonstrate different Hangfire features:

  • Fire-and-forget jobs: Executed once, immediately
  • Delayed jobs: Executed once, after a specified delay
  • Recurring jobs: Executed repeatedly based on CRON expressions
  • Queued jobs: Executed in specific queues (critical, default, normal, low)

Environment Variables

Key environment variables for configuration:

  • DOTNET_ENVIRONMENT: Sets the environment (Development, Test, Staging, Production)
  • ASPNETCORE_ENVIRONMENT: Alternative environment variable

Logging

The application uses Serilog for structured logging:

  • Console output for development
  • File logging in logs/ directory
  • Configurable log levels per environment

Monitoring

  • Job Execution: Monitor through application logs
  • Redis Storage: Use Redis CLI or management tools to inspect job storage
  • Performance: Built-in Hangfire metrics and heartbeat monitoring

Troubleshooting

  1. Redis Connection Issues:

    • Ensure Redis server is running and accessible
    • Check Redis configuration in appsettings files
    • Verify network connectivity and firewall settings
  2. Authentication Issues:

    • Verify credentials in appsettings files
    • Check browser developer tools for authentication errors
  3. Service Issues:

    • Check application logs in logs/ directory
    • Verify environment variables are set correctly
    • Ensure all required packages are installed

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors