ProfileWorker changes: - Implement IHealthCheck interface - Replace _workCount with DateTime-based tracking - Track _lastSuccessfulRun, _consecutiveFailures, _lastException - Graceful shutdown handling (OperationCanceledException) - Health states: Healthy, Degraded (1-2 failures), Unhealthy (3x interval) DependencyInjection changes: - Register ProfileWorker as singleton (for health check access) - Use factory pattern for IHostedService registration Program.cs changes: - Add health check service with ProfileWorker - Map /health endpoint (full JSON response with all checks) - Map /health/ready endpoint (filtered by 'ready' tag) - Custom JSON response writer with detailed metrics
189 lines
6.0 KiB
C#
189 lines
6.0 KiB
C#
using ECMJobRunner.Application;
|
|
using ECMJobRunner.Infrastructure;
|
|
using ECMJobRunner.WebCron;
|
|
using ECMJobRunner.WebCron.ProfileWorker;
|
|
using Hangfire;
|
|
using Hangfire.SqlServer;
|
|
using Microsoft.Data.SqlClient;
|
|
using Serilog;
|
|
using Serilog.Ui.Core.Extensions;
|
|
using Serilog.Ui.SqliteDataProvider.Extensions;
|
|
using Serilog.Ui.Web.Extensions;
|
|
|
|
// Enable Serilog self-diagnostics
|
|
Serilog.Debugging.SelfLog.Enable(msg => Console.WriteLine($"[SERILOG] {msg}"));
|
|
|
|
// Build temporary configuration to read log directory
|
|
var tempConfig = new ConfigurationBuilder()
|
|
.SetBasePath(Directory.GetCurrentDirectory())
|
|
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
|
|
.Build();
|
|
|
|
// Get log directory from configuration
|
|
var logDirectory = tempConfig.GetValue<string>("Application:LogDirectory")
|
|
?? throw new InvalidOperationException("Application:LogDirectory not found in configuration.");
|
|
var sqliteDbPath = Path.Combine(logDirectory, "logs.db");
|
|
|
|
Console.WriteLine($"[INFO] SQLite Log Database Path: {sqliteDbPath}");
|
|
|
|
// Configure Serilog with SQLite sink
|
|
Log.Logger = new LoggerConfiguration()
|
|
.MinimumLevel.Information()
|
|
.MinimumLevel.Override("Microsoft.AspNetCore", Serilog.Events.LogEventLevel.Warning)
|
|
.MinimumLevel.Override("Microsoft.EntityFrameworkCore", Serilog.Events.LogEventLevel.Warning)
|
|
.MinimumLevel.Override("Hangfire", Serilog.Events.LogEventLevel.Information)
|
|
.WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}")
|
|
.WriteTo.SQLite(sqliteDbPath, storeTimestampInUtc: true)
|
|
.Enrich.FromLogContext()
|
|
.CreateLogger();
|
|
|
|
try
|
|
{
|
|
Log.Information("Starting ECMJobRunner.WebCron application");
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
// Use Serilog for logging
|
|
builder.Host.UseSerilog();
|
|
|
|
// Configure Windows Service hosting if enabled
|
|
if (builder.Configuration.GetValue<bool>("HostingOptions:UseWindowsService"))
|
|
{
|
|
builder.Host.UseWindowsService();
|
|
}
|
|
|
|
// Add services to the container.
|
|
builder.Services.AddControllers();
|
|
builder.Services.AddProfileWorker(builder.Configuration);
|
|
|
|
// Register services
|
|
var cnnStr = builder.Configuration.GetConnectionString("SDD-VMP04-SQL17")
|
|
?? throw new InvalidOperationException("Connection string 'SDD-VMP04-SQL17' not found.");
|
|
builder.Services.AddJobRunnerInfrastructure(cnnStr);
|
|
|
|
var recClientApiUrl = builder.Configuration.GetValue<string>("ReC:ApiUrl")
|
|
?? throw new InvalidOperationException("ReC:ApiUrl not found.");
|
|
builder.Services.AddJobRunnerServices(recClientApiUrl);
|
|
|
|
// Get Hangfire storage configuration
|
|
var useInMemory = builder.Configuration.GetValue<bool>("Hangfire:InMemory");
|
|
|
|
// Add Hangfire services with configurable storage
|
|
builder.Services.AddHangfire(configuration =>
|
|
{
|
|
configuration
|
|
.SetDataCompatibilityLevel(CompatibilityLevel.Version_180)
|
|
.UseSimpleAssemblyNameTypeSerializer()
|
|
.UseRecommendedSerializerSettings();
|
|
|
|
// Configure storage based on appsettings
|
|
if (useInMemory)
|
|
{
|
|
configuration.UseInMemoryStorage();
|
|
}
|
|
else // Use SQL Server storage
|
|
{
|
|
configuration.UseSqlServerStorage(cnnStr, new SqlServerStorageOptions
|
|
{
|
|
CommandBatchMaxTimeout = TimeSpan.FromMinutes(5),
|
|
SlidingInvisibilityTimeout = TimeSpan.FromMinutes(5),
|
|
QueuePollInterval = TimeSpan.Zero,
|
|
UseRecommendedIsolationLevel = true,
|
|
DisableGlobalLocks = true,
|
|
SqlClientFactory = SqlClientFactory.Instance
|
|
});
|
|
}
|
|
});
|
|
|
|
// Add Hangfire server
|
|
builder.Services.AddHangfireServer();
|
|
|
|
builder.Services.AddEndpointsApiExplorer();
|
|
builder.Services.AddSwaggerGen();
|
|
|
|
// Add health checks
|
|
builder.Services.AddHealthChecks()
|
|
.AddCheck<ProfileWorker>("profile-worker", tags: new[] { "ready", "worker" });
|
|
|
|
// Add Serilog.UI with SQLite provider - use same path from configuration
|
|
var serilogUiLogDirectory = builder.Configuration.GetValue<string>("Application:LogDirectory")
|
|
?? throw new InvalidOperationException("Application:LogDirectory not found in configuration.");
|
|
var serilogUiDbPath = Path.Combine(serilogUiLogDirectory, "logs.db");
|
|
|
|
builder.Services.AddSerilogUi(logUIOpt =>
|
|
{
|
|
logUIOpt.UseSqliteServer(dbOpt =>
|
|
{
|
|
dbOpt.WithConnectionString($"Data Source={serilogUiDbPath}");
|
|
dbOpt.WithTable("Logs");
|
|
});
|
|
});
|
|
|
|
var app = builder.Build();
|
|
|
|
// Configure the HTTP request pipeline.
|
|
if (app.Environment.IsDevelopment())
|
|
{
|
|
app.UseSwagger();
|
|
app.UseSwaggerUI();
|
|
}
|
|
|
|
app.UseHttpsRedirection();
|
|
|
|
app.UseAuthorization();
|
|
|
|
// Add Hangfire Dashboard with no authentication (for development)
|
|
app.UseHangfireDashboard("/hangfire", new DashboardOptions
|
|
{
|
|
Authorization = new[] { new AllowAllDashboardAuthorizationFilter() }
|
|
});
|
|
|
|
// Add Serilog.UI Dashboard
|
|
app.UseSerilogUi();
|
|
|
|
app.MapControllers();
|
|
|
|
// Map health check endpoints
|
|
app.MapHealthChecks("/health", new Microsoft.AspNetCore.Diagnostics.HealthChecks.HealthCheckOptions
|
|
{
|
|
Predicate = _ => true,
|
|
ResponseWriter = async (context, report) =>
|
|
{
|
|
context.Response.ContentType = "application/json";
|
|
var result = System.Text.Json.JsonSerializer.Serialize(new
|
|
{
|
|
status = report.Status.ToString(),
|
|
timestamp = DateTime.UtcNow,
|
|
checks = report.Entries.Select(e => new
|
|
{
|
|
name = e.Key,
|
|
status = e.Value.Status.ToString(),
|
|
description = e.Value.Description,
|
|
duration = e.Value.Duration.TotalMilliseconds,
|
|
exception = e.Value.Exception?.Message,
|
|
data = e.Value.Data
|
|
})
|
|
});
|
|
await context.Response.WriteAsync(result);
|
|
}
|
|
});
|
|
|
|
app.MapHealthChecks("/health/ready", new Microsoft.AspNetCore.Diagnostics.HealthChecks.HealthCheckOptions
|
|
{
|
|
Predicate = check => check.Tags.Contains("ready")
|
|
});
|
|
|
|
// Redirect root path to Hangfire dashboard
|
|
app.MapGet("/", () => Results.Redirect("/hangfire"));
|
|
|
|
app.Run();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Fatal(ex, "Application terminated unexpectedly");
|
|
}
|
|
finally
|
|
{
|
|
Log.CloseAndFlush();
|
|
}
|