| | | 1 | | using ClientManager.Shared.Configuration; |
| | | 2 | | using ClientManager.Shared.Data; |
| | | 3 | | using ClientManager.Shared.Messaging; |
| | | 4 | | using ClientManager.Worker; |
| | | 5 | | using ClientManager.Worker.Administration; |
| | | 6 | | using ClientManager.Worker.Messaging; |
| | | 7 | | using ClientManager.Worker.Repositories; |
| | | 8 | | using Microsoft.EntityFrameworkCore; |
| | | 9 | | using Microsoft.Extensions.Options; |
| | | 10 | | using Serilog; |
| | | 11 | | |
| | 0 | 12 | | var builder = Host.CreateApplicationBuilder(args); |
| | | 13 | | |
| | | 14 | | // Load .env file if it exists |
| | 0 | 15 | | ConfigurationHelper.LoadDotEnvFile(); |
| | 0 | 16 | | builder.Configuration.AddEnvironmentVariables(); |
| | | 17 | | |
| | | 18 | | // Map configuration sections to strongly typed classes |
| | 0 | 19 | | builder.Services.Configure<PostgresConnectionConfiguration>(builder.Configuration.GetSection("POSTGRES")); |
| | 0 | 20 | | builder.Services.Configure<RabbitMQConnectionConfiguration>(builder.Configuration.GetSection("RABBITMQ")); |
| | | 21 | | |
| | 0 | 22 | | Directory.CreateDirectory(Path.Combine(AppContext.BaseDirectory, "Logs")); |
| | | 23 | | |
| | | 24 | | // Configure Serilog |
| | 0 | 25 | | Log.Logger = new LoggerConfiguration() |
| | 0 | 26 | | .MinimumLevel.Information() |
| | 0 | 27 | | .Enrich.WithProperty("Service", "Worker") |
| | 0 | 28 | | .WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] [{Service}] {Message:lj}{NewLine}{Exception}") |
| | 0 | 29 | | .WriteTo.File(Path.Combine(AppContext.BaseDirectory, "Logs", "worker.log")) |
| | 0 | 30 | | .CreateLogger(); |
| | | 31 | | |
| | | 32 | | // Hook Serilog into .NET logging |
| | 0 | 33 | | builder.Logging.ClearProviders(); |
| | 0 | 34 | | builder.Logging.AddSerilog(); |
| | | 35 | | |
| | 0 | 36 | | builder.Services.AddHostedService<Worker>(); |
| | 0 | 37 | | builder.Services.AddScoped<IMessageContextAccessor, MessageContextAccessor>(); |
| | 0 | 38 | | builder.Services.AddScoped<IRoutingConvention, RoutingConvention>(); |
| | 0 | 39 | | builder.Services.AddScoped<IClientRepository, ClientRepository>(); |
| | 0 | 40 | | builder.Services.AddScoped<IMessagePublisher, MessagePublisher>(); |
| | 0 | 41 | | builder.Services.AddSingleton<MessagePublishPipeline>(); |
| | 0 | 42 | | builder.Services.AddSingleton<IMessageConsumer, RabbitMQMessageConsumer>(); |
| | 0 | 43 | | builder.Services.AddSingleton<IMessageBrokerFactory>(sp => |
| | 0 | 44 | | { |
| | 0 | 45 | | // Get RabbitMQ connection configuration from DI |
| | 0 | 46 | | var connectionConfiguration = sp.GetRequiredService<IOptions<RabbitMQConnectionConfiguration>>().Value; |
| | 0 | 47 | | return new MessageBrokerFactory(connectionConfiguration); |
| | 0 | 48 | | }); |
| | 0 | 49 | | builder.Services.AddDbContext<AppDbContext>( |
| | 0 | 50 | | (sp, options) => |
| | 0 | 51 | | { |
| | 0 | 52 | | // Get Postgres connection configuration from DI |
| | 0 | 53 | | var postgresConfig = sp.GetRequiredService<IOptions<PostgresConnectionConfiguration>>().Value; |
| | 0 | 54 | | options.UseNpgsql(postgresConfig.ToConnectionString(), b => b.MigrationsAssembly("ClientManager.Worker")); |
| | 0 | 55 | | } |
| | 0 | 56 | | ); |
| | | 57 | | |
| | 0 | 58 | | builder.Services.AddMessageHandlers(AppDomain.CurrentDomain.GetAssemblies()); |
| | | 59 | | |
| | 0 | 60 | | var host = builder.Build(); |
| | | 61 | | |
| | 0 | 62 | | using (var scope = host.Services.CreateScope()) |
| | | 63 | | { |
| | 0 | 64 | | var db = scope.ServiceProvider.GetRequiredService<AppDbContext>(); |
| | 0 | 65 | | var retries = 0; |
| | | 66 | | const int maxRetries = 10; |
| | | 67 | | |
| | 0 | 68 | | while (retries < maxRetries) |
| | | 69 | | { |
| | | 70 | | try |
| | | 71 | | { |
| | 0 | 72 | | db.Database.Migrate(); |
| | 0 | 73 | | Console.WriteLine("Database migration successful"); |
| | 0 | 74 | | break; |
| | | 75 | | } |
| | 0 | 76 | | catch (Exception ex) |
| | | 77 | | { |
| | 0 | 78 | | retries++; |
| | 0 | 79 | | Console.WriteLine($"Database not ready (attempt {retries}/{maxRetries}): {ex.Message}"); |
| | 0 | 80 | | Thread.Sleep(TimeSpan.FromSeconds(5)); |
| | 0 | 81 | | } |
| | | 82 | | } |
| | 0 | 83 | | } |
| | | 84 | | |
| | 0 | 85 | | host.Run(); |