RSCG – HangfireRecurringJob

RSCG – HangfireRecurringJob
 
 

name HangfireRecurringJob
nuget https://www.nuget.org/packages/IeuanWalker.Hangfire.RecurringJob/
link https://github.com/IeuanWalker/Hangfire.RecurringJob
author Ieuan Walker

Generating recurring jobs for Hangfire from class attribute

 

This is how you can use HangfireRecurringJob .

The code that you start with is


<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>


The code that you will use is


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");
    }
}



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);
}


 

The code that is generated is

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;
	}
}

Code and pdf at

https://ignatandrei.github.io/RSCG_Examples/v2/docs/HangfireRecurringJob