Skip to content

Getting Started ​

Prerequisites ​

  • .NET 10.0 or later
  • PostgreSQL (for Hangfire storage and job metadata)
  • A Gryd.IO-based application

Installation ​

bash
dotnet add package GrydJobs

This includes Core, Application, Infrastructure (Hangfire + EF Core), and API layers.

Individual packages ​

bash
dotnet add package GrydJobs.Core            # Abstractions only
dotnet add package GrydJobs.Application     # CQRS layer
dotnet add package GrydJobs.Infrastructure  # Hangfire + EF Core
dotnet add package GrydJobs.API             # REST controllers

Configuration ​

Minimal setup ​

csharp
// Program.cs
builder.Services.AddGrydJobs(options =>
{
    options.ConnectionString = builder.Configuration
        .GetConnectionString("Jobs")!;
});

var app = builder.Build();
app.UseGrydJobs();

if (app.Environment.IsDevelopment())
{
    await app.ApplyGrydJobsMigrationsAsync();
}

app.MapControllers();
app.Run();

Full configuration ​

csharp
builder.Services.AddGrydJobs(options =>
{
    // Required: PostgreSQL connection string
    options.ConnectionString = builder.Configuration
        .GetConnectionString("Jobs")!;

    // Queues and worker counts for the shared server worker pool
    options.Queues =
    [
        new QueueOptions { Name = "default", WorkerCount = 10 },
        new QueueOptions { Name = "critical", WorkerCount = 4 },
        new QueueOptions { Name = "low", WorkerCount = 2 },
        new QueueOptions { Name = "notifications", WorkerCount = 2 }
    ];

    // Auto-discover and register IRecurringJob implementations (default: true)
    options.AutoRegisterRecurringJobs = true;

    // Assemblies to scan for IRecurringJob implementations
    options.JobAssemblies = [typeof(MyJob).Assembly];

    // Dead letter queue cleanup (default: 30 days, null to disable)
    options.DeadLetterRetentionDays = 30;

    // Retry policies
    options.RetryPolicies
        .Add<SendEmailJob>(new RetryPolicyOptions
        {
            MaxRetries = 5,
            BaseDelay = TimeSpan.FromSeconds(30)
        })
        .Add<ImportDataJob>(new RetryPolicyOptions
        {
            MaxRetries = 10,
            BaseDelay = TimeSpan.FromMinutes(2)
        });

    // Default retry policy (for jobs without a specific policy)
    options.DefaultRetryPolicy = new RetryPolicyOptions
    {
        MaxRetries = 3,
        BaseDelay = TimeSpan.FromSeconds(10),
        MaxDelay = TimeSpan.FromMinutes(5),
        BackoffType = BackoffType.Exponential
    };
});

// Health checks (optional)
builder.Services.AddGrydJobsHealthChecks();

var app = builder.Build();

// Middleware pipeline
app.UseGrydJobs();
app.UseGrydJobsDashboard(); // Dashboard at /grydjobs/dashboard

if (app.Environment.IsDevelopment())
{
    await app.ApplyGrydJobsMigrationsAsync();
}

app.MapControllers();
app.MapHealthChecks("/health");
app.Run();

Hangfire durably schedules retries for GrydJobs adapters. MaxRetries counts retries after the initial execution, so the default value of 3 allows up to 4 total attempts. The effective policy is captured when a job is submitted.

appsettings.json ​

json
{
  "ConnectionStrings": {
    "Jobs": "Host=localhost;Port=5432;Database=gryd_jobs;Username=postgres;Password=postgres"
  }
}

Creating Your First Job ​

1. Define the job arguments ​

csharp
public record SendWelcomeEmailArgs(
    string UserId,
    string Email,
    string UserName);

2. Implement the job ​

csharp
using GrydJobs.Core.Abstractions;

public class SendWelcomeEmailJob : IJob<SendWelcomeEmailArgs>
{
    private readonly IEmailSender _emailSender;
    private readonly ILogger<SendWelcomeEmailJob> _logger;

    public SendWelcomeEmailJob(
        IEmailSender emailSender,
        ILogger<SendWelcomeEmailJob> logger)
    {
        _emailSender = emailSender;
        _logger = logger;
    }

    public async Task ExecuteAsync(
        SendWelcomeEmailArgs args,
        JobExecutionContext context,
        CancellationToken cancellationToken)
    {
        _logger.LogInformation(
            "Sending welcome email to {Email} (Job: {JobId})",
            args.Email, context.JobId);

        await _emailSender.SendTemplateAsync(
            args.Email,
            "welcome",
            new { Name = args.UserName },
            cancellationToken);
    }
}

3. Enqueue the job ​

csharp
public class UserService
{
    private readonly IJobScheduler _scheduler;

    public UserService(IJobScheduler scheduler)
    {
        _scheduler = scheduler;
    }

    public async Task RegisterUserAsync(CreateUserRequest request)
    {
        var user = await CreateUser(request);

        // Fire-and-forget: runs immediately in the background
        await _scheduler.EnqueueAsync<SendWelcomeEmailJob, SendWelcomeEmailArgs>(
            new SendWelcomeEmailArgs(user.Id, user.Email, user.Name));
    }
}

Next Steps ​

Released under the MIT License.