JobQueue for import layers

This commit is contained in:
Michał Zieliński
2025-06-07 12:33:33 +02:00
parent e56be55274
commit 787df8b838
8 changed files with 972 additions and 114 deletions

View File

@@ -10,20 +10,24 @@ using DiunaBI.Core.Database.Context;
using DiunaBI.Core.Services;
using Google.Apis.Sheets.v4;
using Serilog;
using DiunaBI.Core.Interfaces;
var builder = WebApplication.CreateBuilder(args);
// ✅ DODAJ SERILOG CONFIGURATION
builder.Host.UseSerilog((context, configuration) =>
// ✅ SERILOG TYLKO DLA PRODUKCJI
if (builder.Environment.IsProduction())
{
configuration
.ReadFrom.Configuration(context.Configuration)
.Enrich.FromLogContext()
.Enrich.WithProperty("Application", "DiunaBI")
.Enrich.WithProperty("Version", Assembly.GetExecutingAssembly().GetName().Version?.ToString() ?? "unknown")
.Enrich.WithEnvironmentName()
.Enrich.WithMachineName();
});
builder.Host.UseSerilog((context, configuration) =>
{
configuration
.ReadFrom.Configuration(context.Configuration)
.Enrich.FromLogContext()
.Enrich.WithProperty("Application", "DiunaBI")
.Enrich.WithProperty("Version", Assembly.GetExecutingAssembly().GetName().Version?.ToString() ?? "unknown")
.Enrich.WithEnvironmentName()
.Enrich.WithMachineName();
});
}
var connectionString = builder.Configuration.GetConnectionString("SQLDatabase");
builder.Services.AddDbContext<AppDbContext>(x =>
@@ -50,7 +54,6 @@ builder.Services.AddCors(options =>
builder.Services.AddControllers();
builder.Services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
@@ -66,10 +69,13 @@ builder.Services.AddAuthentication(options =>
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(builder.Configuration["Secret"]!))
};
});
builder.Services.AddAuthentication();
// Queue services
builder.Services.AddScoped<IJobQueueService, JobQueueService>();
builder.Services.AddHostedService<JobQueueProcessor>();
// Zarejestruj Google Sheets dependencies
builder.Services.AddSingleton<GoogleSheetsHelper>();
builder.Services.AddSingleton<GoogleDriveHelper>();
@@ -90,32 +96,46 @@ builder.Services.AddSingleton<PluginManager>();
var app = builder.Build();
app.UseSerilogRequestLogging(options =>
// ✅ SERILOG REQUEST LOGGING TYLKO DLA PRODUKCJI
if (app.Environment.IsProduction())
{
options.MessageTemplate = "HTTP {RequestMethod} {RequestPath} responded {StatusCode} in {Elapsed:0.0000} ms";
options.EnrichDiagnosticContext = (diagnosticContext, httpContext) =>
app.UseSerilogRequestLogging(options =>
{
diagnosticContext.Set("RequestHost", httpContext.Request.Host.Value);
diagnosticContext.Set("RequestScheme", httpContext.Request.Scheme);
var userAgent = httpContext.Request.Headers.UserAgent.FirstOrDefault();
if (!string.IsNullOrEmpty(userAgent))
options.MessageTemplate = "HTTP {RequestMethod} {RequestPath} responded {StatusCode} in {Elapsed:0.0000} ms";
options.EnrichDiagnosticContext = (diagnosticContext, httpContext) =>
{
diagnosticContext.Set("UserAgent", userAgent);
}
diagnosticContext.Set("RemoteIP", httpContext.Connection.RemoteIpAddress?.ToString() ?? "unknown");
diagnosticContext.Set("RequestContentType", httpContext.Request.ContentType ?? "none");
};
});
diagnosticContext.Set("RequestHost", httpContext.Request.Host.Value);
diagnosticContext.Set("RequestScheme", httpContext.Request.Scheme);
var userAgent = httpContext.Request.Headers.UserAgent.FirstOrDefault();
if (!string.IsNullOrEmpty(userAgent))
{
diagnosticContext.Set("UserAgent", userAgent);
}
diagnosticContext.Set("RemoteIP", httpContext.Connection.RemoteIpAddress?.ToString() ?? "unknown");
diagnosticContext.Set("RequestContentType", httpContext.Request.ContentType ?? "none");
};
});
}
var pluginManager = app.Services.GetRequiredService<PluginManager>();
var executablePath = Assembly.GetExecutingAssembly().Location;
var executableDir = Path.GetDirectoryName(executablePath)!;
var pluginsPath = Path.Combine(executableDir, "Plugins");
Log.Information("Starting DiunaBI application");
Log.Information("Loading plugins from: {PluginsPath}", pluginsPath);
// ✅ RÓŻNE LOGGERY W ZALEŻNOŚCI OD ŚRODOWISKA
if (app.Environment.IsProduction())
{
Log.Information("Starting DiunaBI application");
Log.Information("Loading plugins from: {PluginsPath}", pluginsPath);
}
else
{
var logger = app.Services.GetRequiredService<ILogger<Program>>();
logger.LogInformation("Starting DiunaBI application (Development)");
logger.LogInformation("Loading plugins from: {PluginsPath}", pluginsPath);
}
pluginManager.LoadPluginsFromDirectory(pluginsPath);
@@ -145,5 +165,8 @@ app.MapControllers();
app.Run();
// ✅ DODAJ CLEANUP
Log.CloseAndFlush();
// ✅ SERILOG CLEANUP TYLKO DLA PRODUKCJI
if (app.Environment.IsProduction())
{
Log.CloseAndFlush();
}