Guides Hangfire background job setup in .NET: fire-and-forget, delayed, recurring, continuations, retry patterns, storage config (SQL Server/Redis/PostgreSQL), queue prioritization, and dashboard auth.
Ships in claude-skills-collection. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
[AutomaticRetry].WorkerCount, ServerTimeout.IRecurringJobManager ou RecurringJob.AddOrUpdate.| Type | Quand l'utiliser | API |
|---|---|---|
| Fire-and-forget | Traitement hors request pipeline, sans résultat attendu | BackgroundJob.Enqueue |
| Delayed | Action différée (ex: relance J+1) | BackgroundJob.Schedule |
| Recurring | Batch quotidien, rapport, purge CRON | RecurringJob.AddOrUpdate |
| Continuation | Chaînage (import → notification) | BackgroundJob.ContinueJobWith |
| Batch (Pro) | Fan-out + agrégation (Hangfire Pro requis) | BatchJob.StartNew |
Règle : si le job peut dépasser 30 s ou doit survivre au redémarrage du process → Hangfire plutôt que
Task.Run.
// NuGet : Hangfire, Hangfire.SqlServer, Hangfire.AspNetCore
builder.Services.AddHangfire(config => config
.SetDataCompatibilityLevel(CompatibilityLevel.Version_180)
.UseSimpleAssemblyNameTypeSerializer()
.UseRecommendedSerializerSettings() // Newtonsoft.Json avec camelCase
.UseSqlServerStorage(connectionString, new SqlServerStorageOptions
{
CommandBatchMaxTimeout = TimeSpan.FromMinutes(5),
SlidingInvisibilityTimeout = TimeSpan.FromMinutes(5),
QueuePollInterval = TimeSpan.FromSeconds(15),
UseRecommendedIsolationLevel = true,
DisableGlobalLocks = true, // recommandé Hangfire 1.8+
SchemaName = "hangfire"
}));
builder.Services.AddHangfireServer(options =>
{
options.Queues = ["critical", "default", "low"];
options.WorkerCount = Environment.ProcessorCount * 2; // ajuster selon le workload
options.ServerTimeout = TimeSpan.FromMinutes(5);
});
// Dashboard — TOUJOURS derrière un filtre en production
app.UseHangfireDashboard("/hangfire", new DashboardOptions
{
Authorization = [new HangfireRoleAuthFilter("Admin")],
IsReadOnlyFunc = ctx => !ctx.GetHttpContext().User.IsInRole("Admin")
});
public interface IEmailJobService
{
Task SendWelcomeEmailAsync(string userId, CancellationToken ct = default);
Task SendMonthlyReportAsync(int month, int year, CancellationToken ct = default);
}
public class EmailJobService : IEmailJobService
{
private readonly IEmailSender _sender;
private readonly IUserRepository _users;
public EmailJobService(IEmailSender sender, IUserRepository users)
=> (_sender, _users) = (sender, users);
// Retry exponentiel, échoue proprement sur erreur métier
[AutomaticRetry(Attempts = 3, DelaysInSeconds = [60, 300, 900],
OnAttemptsExceeded = AttemptsExceededAction.Fail)]
[Queue("default")]
public async Task SendWelcomeEmailAsync(string userId, CancellationToken ct = default)
{
var user = await _users.GetByIdAsync(userId, ct)
?? throw new InvalidOperationException($"User {userId} not found");
await _sender.SendAsync(user.Email, "Bienvenue", "welcome-template", ct);
}
[AutomaticRetry(Attempts = 2)]
[Queue("low")]
public async Task SendMonthlyReportAsync(int month, int year, CancellationToken ct = default)
{
// Job long → vérifier l'annulation entre les étapes
var users = await _users.GetAllActiveAsync(ct);
foreach (var u in users)
{
ct.ThrowIfCancellationRequested();
var report = await BuildReportAsync(u, month, year, ct);
await _sender.SendAsync(u.Email, $"Rapport {month}/{year}", report, ct);
}
}
}
// Fire-and-forget
BackgroundJob.Enqueue<IEmailJobService>(x => x.SendWelcomeEmailAsync(userId, CancellationToken.None));
// Delayed (dans 24 h)
BackgroundJob.Schedule<IEmailJobService>(
x => x.SendWelcomeEmailAsync(userId, CancellationToken.None),
TimeSpan.FromHours(24));
// Recurring — via IRecurringJobManager (testable)
services.AddScoped<IJobBootstrapper, JobBootstrapper>();
public class JobBootstrapper : IJobBootstrapper
{
private readonly IRecurringJobManager _jobs;
public JobBootstrapper(IRecurringJobManager jobs) => _jobs = jobs;
public void Register()
{
_jobs.AddOrUpdate<IEmailJobService>(
"monthly-report",
x => x.SendMonthlyReportAsync(0, 0, CancellationToken.None),
"0 8 1 * *", // 1er du mois à 8 h
new RecurringJobOptions
{
TimeZone = TimeZoneInfo.FindSystemTimeZoneById("Africa/Tunis")
});
}
}
// Continuation (chaînage)
var importId = BackgroundJob.Enqueue<IDataService>(x => x.ImportDataAsync(CancellationToken.None));
BackgroundJob.ContinueJobWith<INotificationService>(
importId,
x => x.NotifyImportCompleteAsync(CancellationToken.None));
public class SmartRetryFilter : JobFilterAttribute, IElectStateFilter
{
public void OnStateElection(ElectStateContext context)
{
if (context.CandidateState is FailedState { Exception: BusinessException })
context.CandidateState = new DeletedState { Reason = "Erreur métier — pas de retry" };
}
}
// Enregistrement global
GlobalJobFilters.Filters.Add(new SmartRetryFilter());
[AutomaticRetry(
Attempts = 5,
DelaysInSeconds = [30, 120, 600, 3600, 14400], // 30 s → 4 h
OnAttemptsExceeded = AttemptsExceededAction.Fail)]
public async Task ProcessPaymentAsync(string paymentId, CancellationToken ct = default) { ... }
// Serveur avec ordre de consommation strict
options.Queues = ["critical", "default", "low"];
// Le serveur vide "critical" avant de traiter "default", etc.
// Déploiement multi-serveurs : dédier des serveurs par queue
// Serveur 1 : critical + default
// Serveur 2 : low (batch nocturne)
public async Task ImportInvoiceAsync(string invoiceId, CancellationToken ct = default)
{
// Vérifier si déjà traité avant tout side-effect
if (await _db.Invoices.AnyAsync(i => i.ExternalId == invoiceId && i.Imported, ct))
return; // idempotent : deuxième exécution sans effet
var invoice = await _externalApi.GetInvoiceAsync(invoiceId, ct);
await _db.Invoices.AddAsync(new Invoice { ExternalId = invoiceId, Data = invoice, Imported = true }, ct);
await _db.SaveChangesAsync(ct);
}
# Vérifier les jobs Failed depuis le dashboard
# URL : https://myapp/hangfire → onglet "Failed"
# Requête SQL de diagnostic (SQL Server)
SELECT State, COUNT(*) AS Count
FROM hangfire.Job
GROUP BY State;
# Jobs bloqués depuis > 1 h
SELECT Id, StateName, CreatedAt, DATEDIFF(MINUTE, CreatedAt, GETUTCDATE()) AS AgeMinutes
FROM hangfire.Job
WHERE StateName IN ('Processing', 'Enqueued')
AND DATEDIFF(MINUTE, CreatedAt, GETUTCDATE()) > 60;
Intégrer avec OpenTelemetry / Serilog :
// Serilog sink — chaque job loggue son JobId automatiquement
Log.ForContext("HangfireJobId", PerformContext?.BackgroundJob.Id)
.Information("Traitement démarré pour {UserId}", userId);
| Anti-pattern | Risque | Correction |
|---|---|---|
Passer un DbContext ou HttpContext en paramètre | Sérialisation échoue / contexte périmé | Passer un id simple, résoudre via DI dans le job |
Capturer DateTime.Now dans un job récurrent | Valeur figée à l'enregistrement, pas à l'exécution | Calculer la date à l'intérieur du job |
Thread.Sleep dans un job | Bloque un worker thread | await Task.Delay(...) |
WorkerCount trop élevé sur SQL Server | Contention sur les tables Hangfire | Limiter à 2–4× CPU ; monitorer les deadlocks |
| Dashboard sans auth en production | Exposition des données sensibles et contrôle des jobs | Toujours un IDashboardAuthorizationFilter |
| Jobs non idempotents | Données dupliquées si retry ou déploiement blue/green | Vérifier l'état avant chaque side-effect |
| Expressions CRON sans timezone | Décalage heure d'été / UTC | Toujours RecurringJobOptions { TimeZone = ... } |
| Stocker de gros payloads dans les paramètres | Table hangfire.Job enflée | Stocker en DB/blob, passer seulement un identifiant |
IRecurringJobManager injecté plutôt que la méthode statique RecurringJob (testabilité).IHostedService, démarrer JobBootstrapper.Register() dans IHostApplicationLifetime.ApplicationStarted.DisableGlobalLocks = true (SQL Server 1.8+) pour réduire la contention.GlobalJobFilters.Filters.Add(new ProlongExpirationTimeAttribute(TimeSpan.FromDays(7))).BackgroundJobClientFake (Hangfire.InMemory) ou un mock de l'interface de service.What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
.NET, C#, SQL Server, Redis, PostgreSQL
hangfire, background-jobs, job-scheduling, dotnet, cron-jobs
/claude-skills-collection:dev-hangfire-job-schedulerfrom claude-skills-collection
npx claudepluginhub vantaflow/claude-skills-collection --plugin claude-skills-collection348 skills in this plugin
/agent-a2a-protocol-guideGuide du protocole Agent-to-Agent (A2A) de Google pour l'interopérabilité entre agents IA — découverte, communication et collaboration inter-agents, avec exemples de code, critères de décision et pièges à éviter. Se déclenche avec "A2A", "agent-to-agent", "protocole A2A", "Google A2A", "interopérabilité agents". Also triggers on "agent-to-agent protocol", "agent interoperability", "connect two agents"./agent-agent-observabilityInstrumentation technique d'un agent IA — tracing distribué des appels LLM et outils, spans, métriques custom, corrélation de logs et dashboards de supervision. Pour les alertes et garde-fous de production, voir agent-monitoring-setup. Se déclenche avec "observabilité agent", "tracing agent", "LangSmith", "Langfuse", "OpenTelemetry agent", "spans LLM", "corréler les logs agent". Also triggers on "trace my agent", "LLM tracing", "agent spans"./agent-api-caller-subagentSous-agent spécialisé dans les appels API REST/GraphQL avec retry, auth et transformation de données. Se déclenche avec "sous-agent API", "API caller agent", "agent qui appelle une API", "REST agent", "HTTP agent", "API integration subagent", "external API agent". Also triggers on "subagent that calls an API", "API calling agent", "agent HTTP requests".Other plugins' skills for the same job