agentleFS
fpindejfpindej/netrock/.claude/skills/add-background-job/SKILL.md

add-background-job

Skill234 starsChanged 3 months agoMITView on GitHub

Add a Hangfire background job (recurring or one-time)

1 other project uses a version of this.

Adopted by

Most recently taken up by

The file

We split this skill into the rules it gives an agent

2 rules. About 0 are common, 0 are distinctive, and 2 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈0

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 2

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    2. Register in DI: - add two lines to src/backend/MyProject.Infrastructure/Features/Jobs/Extensions/ServiceCollectionExtensions.cs:

    line 46
  2. Very specific:

    All parameters must be JSON-serializable (Hangfire persists them).

    line 81

≈ “Common” is an estimate: at least 1 in 5 of the 364,980 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Adds a recurring or one-time Hangfire background job.

Recurring Job

1. Create the job class in src/backend/MyProject.Infrastructure/Features/Jobs/RecurringJobs/{JobName}Job.cs:

using Hangfire;
using Microsoft.Extensions.Logging;

namespace MyProject.Infrastructure.Features.Jobs.RecurringJobs;

/// <summary>
/// Brief description of what this job does and why.
/// </summary>
internal sealed class MyCleanupJob(
    MyProjectDbContext dbContext,
    TimeProvider timeProvider,
    ILogger<MyCleanupJob> logger) : IRecurringJobDefinition
{
    /// <inheritdoc />
    public string JobId => "my-cleanup";

    /// <inheritdoc />
    public string CronExpression => Cron.Daily();

    /// <inheritdoc />
    public async Task ExecuteAsync()
    {
        // Job logic here - each execution gets its own DI scope
        logger.LogInformation("Job completed");
    }
}

Key conventions:

  • Mark internal sealed, use primary constructor
  • Descriptive JobId (kebab-case, e.g. "expired-token-cleanup")
  • Hangfire.Cron helpers: Cron.Hourly(), Cron.Daily(), Cron.Weekly()

2. Register in DI - add two lines to src/backend/MyProject.Infrastructure/Features/Jobs/Extensions/ServiceCollectionExtensions.cs:

services.AddScoped<MyCleanupJob>();
services.AddScoped<IRecurringJobDefinition>(sp => sp.GetRequiredService<MyCleanupJob>());

3. Verify: dotnet build src/backend/MyProject.slnx

UseJobScheduling() discovers all IRecurringJobDefinition implementations automatically. The job appears in admin panel at /admin/jobs. Pause state persists to DB (hangfire.pausedjobs).

Configuration via appsettings.json: "JobScheduling": { "Enabled": true, "WorkerCount": 4 }. Dev dashboard at http://localhost:8080/hangfire.

One-Time Job

For ad-hoc background work (call API, process file, generate report), use IBackgroundJobClient directly.

Note: transactional emails are already queued automatically - ITemplatedEmailSender.SendSafeAsync() routes through BackgroundEmailService -> EmailDeliveryJob (Features/Email/Jobs/) whenever email and job scheduling are both enabled. Do not wrap email sends in another job.

1. Create the job class in src/backend/MyProject.Infrastructure/Features/{Feature}/Jobs/ (or Features/Jobs/ for cross-cutting jobs):

internal sealed class ReportGenerationJob(
    IReportService reportService,
    ILogger<ReportGenerationJob> logger)
{
    [AutomaticRetry(Attempts = 3, DelaysInSeconds = [30, 120, 600])]
    public async Task ExecuteAsync(Guid reportId, CancellationToken cancellationToken)
    {
        await reportService.GenerateAsync(reportId, cancellationToken);
        logger.LogInformation("Generated report '{ReportId}'", reportId);
    }
}

All parameters must be JSON-serializable (Hangfire persists them). Never pass IServiceProvider, HttpContext, or DbContext as arguments. A trailing CancellationToken parameter is injected by Hangfire (server shutdown) - pass CancellationToken.None in the enqueue expression.

Let exceptions propagate - [AutomaticRetry] only retries when the method throws. Retries re-run the whole method, so keep it idempotent.

2. Register: services.AddScoped<ReportGenerationJob>();

3. Enqueue:

// Fire-and-forget
backgroundJobClient.Enqueue<ReportGenerationJob>(job => job.ExecuteAsync(reportId, CancellationToken.None));

// Delayed
backgroundJobClient.Schedule<ReportGenerationJob>(job => job.ExecuteAsync(reportId, CancellationToken.None), TimeSpan.FromMinutes(30));

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.