using System.Reflection; using GB5Shared.DaprCache; using GB5Shared.DTO.Framework.Login; using GB5Shared.Export; using GB5Shared.Hosting; using GB5Shared.PubSub.OutBox; using JobEngineBLL.Interfaces; using JobEngineSL.Hubs; using JobEngineSL.Services; using Newtonsoft.Json; using Quartz; namespace JobEngineSL; public class JobEngineModule : IModule { public void Register(IServiceCollection services, IConfiguration configuration) { services.AddHttpContextAccessor(); services.AddScoped(sp => { var ctx = sp.GetRequiredService().HttpContext; if (ctx != null && ctx.Request.Headers.TryGetValue("Login", out var h)) return JsonConvert.DeserializeObject(h!)!; return new LoginDTO(); }); services.AddHttpClient("jobengine", c => c.Timeout = TimeSpan.FromMinutes(5)); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddSignalR(options => { options.EnableDetailedErrors = true; options.MaximumReceiveMessageSize = 32 * 1024; options.ClientTimeoutInterval = TimeSpan.FromSeconds(60); options.KeepAliveInterval = TimeSpan.FromSeconds(15); }); var quartzConnStr = configuration.GetConnectionString("QuartzStore") ?? throw new InvalidOperationException("ConnectionStrings:QuartzStore is required"); services.AddQuartz(q => { q.UseSimpleTypeLoader(); q.UseDefaultThreadPool(tp => tp.MaxConcurrency = 20); q.UsePersistentStore(s => { s.UseProperties = true; s.RetryInterval = TimeSpan.FromSeconds(15); s.UseSqlServer(quartzConnStr); s.UseJsonSerializer(); s.UseClustering(); }); }); services.AddQuartzHostedService(opt => opt.WaitForJobsToComplete = true); services.AddSingleton(); services.AddSingleton(sp => sp.GetRequiredService()); services.AddHostedService(); services.AddHostedService(); var bll = Assembly.Load("JobEngineBLL"); var dal = Assembly.Load("JobEngineDAL"); services.Scan(scan => scan .FromAssemblies(bll, dal) .AddClasses(c => c.Where(t => !t.IsAbstract && !t.IsInterface && t.Namespace != null && !t.Namespace.Contains(".DTO") && !typeof(Exception).IsAssignableFrom(t) && t.GetMethod("$") == null)) .AsImplementedInterfaces() .WithScopedLifetime()); } public void MapEndpoints(WebApplication app) { app.MapHub("/hubs/jobengine"); } }