using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; namespace Admin.NET.Plugin.AiDOP.DataPlatform.MdpRebuild; public static class ModuleRebuildJobRunner { public static async Task<(int Scopes, int Accepted, int Skipped)> EnqueueEnabledScopesAsync( IServiceProvider sp, string moduleCode, string triggerType, CancellationToken stoppingToken, ILogger logger) { var catalog = sp.GetRequiredService(); var rebuild = sp.GetRequiredService(); var scopes = await catalog.ListEnabledScopesAsync(moduleCode, stoppingToken); var accepted = 0; var skipped = 0; foreach (var runScope in scopes) { stoppingToken.ThrowIfCancellationRequested(); var (status, body) = await rebuild.EnqueueAsync(runScope.ModuleCode, runScope.TenantId, runScope.FactoryId, null, triggerType, stoppingToken); if (status == 202) accepted++; else skipped++; logger.LogInformation("Enqueue {Module} {Tenant}:{Factory} -> {Status} {Message}", runScope.ModuleCode, runScope.TenantId, runScope.FactoryId, status, body.Message); } return (scopes.Count, accepted, skipped); } }