HangfireRecurringJob by Ieuan Walker
Nuget / site data
Details
Info
Name: HangfireRecurringJob
This is a package that automatically generates the hangfire recurring jobs AddOrUpdate code, using source generators.
Author: Ieuan Walker
NuGet: https://www.nuget.org/packages/IeuanWalker.Hangfire.RecurringJob/
You can find more details at https://github.com/IeuanWalker/Hangfire.RecurringJob
Source : https://github.com/IeuanWalker/Hangfire.RecurringJob
Original Readme
Hangfire.RecurringJob
Automatically generates the recurring job registration code using source generators
How to use it?
- Install the NuGet package into your project.
Install-Package IeuanWalker.Hangfire.RecurringJob
- Add the
RecurringJob
attribute to a class, and create anExecute()
method.
[RecurringJob]
public class RecurringJob1
{
public Task Execute()
{
throw new NotImplementedException();
}
}
[RecurringJob("* * * *")]
public class RecurringJob2
{
public void Execute()
{
throw new NotImplementedException();
}
}
[RecurringJob("* * * *", "Priority")]
public class RecurringJob3
{
public void Execute()
{
throw new NotImplementedException();
}
}
[RecurringJob]
[RecurringJob("*/5 * * * *", "GMT", "Priority", "DataRetention")]
public class RecurringJob4
{
public void Execute()
{
throw new NotImplementedException();
}
}
- Register the recurring jobs
Once a
RecurringJob
attribute has been added to a class in your project an extension method forIApplicationBuilder
will automatically be created. The extension method name convention is AddRecurringJobsFrom + your assembly name.
app.AddRecurringJobsFromExampleProject();
Example
Here is an example of what it looks like in use -
Left is the example code, and right is the generated code
About
Generating recurring jobs for Hangfire from class attribute
How to use
Example ( source csproj, source files )
- CSharp Project
- MyNewJob.cs
- Program.cs
This is the CSharp Project that references HangfireRecurringJob
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<InvariantGlobalization>true</InvariantGlobalization>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Hangfire.AspNetCore" Version="1.8.9" />
<PackageReference Include="Hangfire.Core" Version="1.8.9" />
<PackageReference Include="Hangfire.InMemory" Version="0.7.0" />
<!--<PackageReference Include="Hangfire.MemoryStorage" Version="1.8.0" />-->
<PackageReference Include="Hangfire.SqlServer" Version="1.8.9" />
<PackageReference Include="IeuanWalker.Hangfire.RecurringJob" Version="1.0.1" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.1" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
</ItemGroup>
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
</Project>
This is the use of HangfireRecurringJob in MyNewJob.cs
using IeuanWalker.Hangfire.RecurringJob.Attributes;
namespace HangFireRec;
[RecurringJob("*/1 * * * *")]
public class MyNewJob
{
public async Task Execute()
{
await Task.Delay(1000);
Console.WriteLine("Hello from recurring job hangfire");
}
}
This is the use of HangfireRecurringJob in Program.cs
using Hangfire;
//using Hangfire.MemoryStorage;
using HangFireRec;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddHangfire(it =>
{
it.UseInMemoryStorage();
});
builder.Services.AddHangfireServer(opt =>
{
//config.UseMemoryStorage();
});
//GlobalConfiguration.Configuration.UseInMemoryStorage();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHangfireDashboard();
//app.UseHangfireServer();
var summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
app.MapGet("/weatherforecast", () =>
{
var forecast = Enumerable.Range(1, 5).Select(index =>
new WeatherForecast
(
DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
Random.Shared.Next(-20, 55),
summaries[Random.Shared.Next(summaries.Length)]
))
.ToArray();
return forecast;
})
.WithName("GetWeatherForecast")
.WithOpenApi();
BackgroundJob.Enqueue(() =>Console.WriteLine("Hello from hangfire"));
RecurringJob.AddOrUpdate(() => Console.WriteLine("Hello from hangfire"), Cron.Minutely());
app.AddRecurringJobsFromHangFireRec();
app.Run();
internal record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary)
{
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
}
Generated Files
Those are taken from $(BaseIntermediateOutputPath)\GX
- RecurringJobRegistrationExtensions.g.cs
namespace HangFireRec;
// <auto-generated/>
using Hangfire;
using Microsoft.AspNetCore.Builder;
public static class RecurringJobRegistrationExtensions
{
public static IApplicationBuilder AddRecurringJobsFromHangFireRec(this IApplicationBuilder app)
{
RecurringJob.AddOrUpdate<HangFireRec.MyNewJob>("MyNewJob", "default", x => x.Execute(), "*/1 * * * *", new RecurringJobOptions
{
TimeZone = TimeZoneInfo.FindSystemTimeZoneById("UTC")
});
return app;
}
}
Usefull
Download Example (.NET C# )
Share HangfireRecurringJob
https://ignatandrei.github.io/RSCG_Examples/v2/docs/HangfireRecurringJob