Add hangfire
This commit is contained in:
@@ -1,7 +1,10 @@
|
||||
using System.Text;
|
||||
using BimAI.API.Services;
|
||||
using BimAI.Infrastructure.Data;
|
||||
using BimAI.Infrastructure.Jobs;
|
||||
using BimAI.Infrastructure.Sync;
|
||||
using Hangfire;
|
||||
using Hangfire.SqlServer;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
@@ -18,6 +21,29 @@ builder.Services.AddControllers();
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen();
|
||||
|
||||
// Start Hangfire section
|
||||
builder.Services.AddHangfire(configuration => configuration
|
||||
.SetDataCompatibilityLevel(CompatibilityLevel.Version_180)
|
||||
.UseSimpleAssemblyNameTypeSerializer()
|
||||
.UseRecommendedSerializerSettings()
|
||||
.UseSqlServerStorage(builder.Configuration.GetConnectionString("HangfireConnection"),
|
||||
new SqlServerStorageOptions
|
||||
{
|
||||
CommandBatchMaxTimeout = TimeSpan.FromMinutes(5),
|
||||
SlidingInvisibilityTimeout = TimeSpan.FromMinutes(5),
|
||||
QueuePollInterval = TimeSpan.Zero,
|
||||
UseRecommendedIsolationLevel = true,
|
||||
DisableGlobalLocks = true,
|
||||
SchemaName = "Hangfire"
|
||||
}
|
||||
)
|
||||
);
|
||||
builder.Services.AddHangfireServer(options =>
|
||||
{
|
||||
options.ServerName = builder.Configuration["Hangfire:ServerName"];
|
||||
options.WorkerCount = builder.Configuration.GetValue<int>("Hangfire:WorkerCount", 5);
|
||||
});
|
||||
// End Hangfire section
|
||||
// Start auth section
|
||||
var jwtSettings = builder.Configuration.GetSection("JwtSettings");
|
||||
var secretKey = jwtSettings["SecretKey"];
|
||||
@@ -65,7 +91,22 @@ if (app.Environment.IsDevelopment())
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
app.UseCors("AllowAll");
|
||||
app.UseHangfireDashboard(builder.Configuration["Hangfire:DashboardPath"] ?? "/hangfire", new DashboardOptions
|
||||
{
|
||||
AsyncAuthorization = new[] { new HangfireAuthorizationFilter() },
|
||||
DashboardTitle = "BimAI - Job Dashboard"
|
||||
});
|
||||
app.UseAuthorization();
|
||||
app.UseAuthorization();
|
||||
app.MapControllers();
|
||||
|
||||
RecurringJob.AddOrUpdate<ProductSyncJob>(
|
||||
"product-sync",
|
||||
job => job.ExecuteAsync(),
|
||||
Cron.Daily(2, 0), // Every day at 2:00 AM
|
||||
new RecurringJobOptions
|
||||
{
|
||||
TimeZone = TimeZoneInfo.Local
|
||||
});
|
||||
|
||||
app.Run();
|
||||
Reference in New Issue
Block a user