Remove MetricsBackgroundService registration
This commit is contained in:
parent
681e1aa3e9
commit
d919516f2d
@ -1,25 +1,72 @@
|
|||||||
using Microsoft.AspNetCore.Builder;
|
using Microsoft.AspNetCore.Builder;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.Hosting;
|
using Microsoft.Extensions.Hosting;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
using TransmissionRssManager.Core;
|
using TransmissionRssManager.Core;
|
||||||
|
using TransmissionRssManager.Data;
|
||||||
|
using TransmissionRssManager.Data.Models;
|
||||||
|
using TransmissionRssManager.Data.Repositories;
|
||||||
using TransmissionRssManager.Services;
|
using TransmissionRssManager.Services;
|
||||||
|
using Serilog;
|
||||||
|
using Serilog.Events;
|
||||||
|
|
||||||
|
// Configure Serilog
|
||||||
|
var logsDirectory = Path.Combine(AppContext.BaseDirectory, "logs");
|
||||||
|
Directory.CreateDirectory(logsDirectory);
|
||||||
|
|
||||||
|
Log.Logger = new LoggerConfiguration()
|
||||||
|
.MinimumLevel.Information()
|
||||||
|
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
|
||||||
|
.MinimumLevel.Override("Microsoft.Hosting.Lifetime", LogEventLevel.Information)
|
||||||
|
.Enrich.FromLogContext()
|
||||||
|
.WriteTo.Console()
|
||||||
|
.WriteTo.File(
|
||||||
|
Path.Combine(logsDirectory, "log-.txt"),
|
||||||
|
rollingInterval: RollingInterval.Day,
|
||||||
|
retainedFileCountLimit: 31,
|
||||||
|
fileSizeLimitBytes: 10 * 1024 * 1024)
|
||||||
|
.CreateLogger();
|
||||||
|
|
||||||
var builder = WebApplication.CreateBuilder(args);
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
|
|
||||||
|
// Add Serilog to the application
|
||||||
|
builder.Host.UseSerilog();
|
||||||
|
|
||||||
// Add services to the container
|
// Add services to the container
|
||||||
builder.Services.AddControllers();
|
builder.Services.AddControllers();
|
||||||
builder.Services.AddEndpointsApiExplorer();
|
builder.Services.AddEndpointsApiExplorer();
|
||||||
builder.Services.AddSwaggerGen();
|
builder.Services.AddSwaggerGen();
|
||||||
|
|
||||||
// Add custom services
|
// Configure database
|
||||||
builder.Services.AddSingleton<IConfigService, ConfigService>();
|
var connectionString = builder.Configuration["ConnectionStrings:DefaultConnection"]
|
||||||
builder.Services.AddSingleton<ITransmissionClient, TransmissionClient>();
|
?? "Host=localhost;Database=torrentmanager;Username=postgres;Password=postgres";
|
||||||
builder.Services.AddSingleton<IRssFeedManager, RssFeedManager>();
|
builder.Services.AddDbContext<TorrentManagerContext>(options =>
|
||||||
builder.Services.AddSingleton<IPostProcessor, PostProcessor>();
|
options.UseNpgsql(connectionString));
|
||||||
|
|
||||||
// Add background services
|
// Register repositories
|
||||||
|
builder.Services.AddScoped(typeof(IRepository<>), typeof(Repository<>));
|
||||||
|
builder.Services.AddScoped<ITorrentRepository, TorrentRepository>();
|
||||||
|
builder.Services.AddScoped<DataMigrationService>();
|
||||||
|
|
||||||
|
// Add custom services (now scoped instead of singleton to work with DbContext)
|
||||||
|
builder.Services.AddScoped<IConfigService, ConfigService>();
|
||||||
|
builder.Services.AddScoped<ITransmissionClient, TransmissionClient>();
|
||||||
|
builder.Services.AddScoped<IRssFeedManager, RssFeedManager>();
|
||||||
|
builder.Services.AddScoped<IPostProcessor, PostProcessor>();
|
||||||
|
builder.Services.AddScoped<ILoggingService, LoggingService>();
|
||||||
|
builder.Services.AddScoped<IMetricsService, MetricsService>();
|
||||||
|
builder.Services.AddScoped<ISchedulerService, SchedulerService>();
|
||||||
|
|
||||||
|
// Add background services (these remain singleton)
|
||||||
builder.Services.AddHostedService<RssFeedBackgroundService>();
|
builder.Services.AddHostedService<RssFeedBackgroundService>();
|
||||||
builder.Services.AddHostedService<PostProcessingBackgroundService>();
|
builder.Services.AddHostedService<PostProcessingBackgroundService>();
|
||||||
|
builder.Services.AddHostedService<FeedSchedulerBackgroundService>();
|
||||||
|
// MetricsBackgroundService is not needed with the simplified implementation
|
||||||
|
|
||||||
var app = builder.Build();
|
var app = builder.Build();
|
||||||
|
|
||||||
@ -35,4 +82,38 @@ app.UseRouting();
|
|||||||
app.UseAuthorization();
|
app.UseAuthorization();
|
||||||
app.MapControllers();
|
app.MapControllers();
|
||||||
|
|
||||||
app.Run();
|
// Initialize database and run migrations
|
||||||
|
using (var scope = app.Services.CreateScope())
|
||||||
|
{
|
||||||
|
var services = scope.ServiceProvider;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var context = services.GetRequiredService<TorrentManagerContext>();
|
||||||
|
context.Database.Migrate();
|
||||||
|
|
||||||
|
// Run data migration from file-based storage if needed
|
||||||
|
if (!context.UserPreferences.Any())
|
||||||
|
{
|
||||||
|
var migrationService = services.GetRequiredService<DataMigrationService>();
|
||||||
|
await migrationService.MigrateDataAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
var logger = services.GetRequiredService<ILogger<Program>>();
|
||||||
|
logger.LogError(ex, "An error occurred during database initialization or migration");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await app.RunAsync();
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Log.Fatal(ex, "Application terminated unexpectedly");
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
Log.CloseAndFlush();
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user