using FastGithub.Scanner.Middlewares; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using System; using System.Linq; using System.Threading; using System.Threading.Tasks; namespace FastGithub.Scanner { [Service(ServiceLifetime.Singleton)] sealed class GithubScanService { private readonly GithubMetaService metaService; private readonly ILogger logger; private readonly GithubContextCollection contextCollection; private readonly InvokeDelegate fullScanDelegate; private readonly InvokeDelegate resultScanDelegate; public GithubScanService( GithubMetaService metaService, GithubContextCollection contextCollection, IServiceProvider appService, ILogger logger) { this.metaService = metaService; this.contextCollection = contextCollection; this.logger = logger; ; this.fullScanDelegate = new PipelineBuilder(appService, ctx => Task.CompletedTask) .Use() .Use() .Use() .Use() .Use() .Build(); this.resultScanDelegate = new PipelineBuilder(appService, ctx => Task.CompletedTask) .Use() .Use() .Use() .Use() .Build(); } public async Task ScanAllAsync(CancellationToken cancellationToken = default) { this.logger.LogInformation("完整扫描开始"); var meta = await this.metaService.GetMetaAsync(cancellationToken); if (meta != null) { var scanTasks = meta.ToGithubContexts().Select(ctx => ScanAsync(ctx)); await Task.WhenAll(scanTasks); } this.logger.LogInformation("完整扫描结束"); async Task ScanAsync(GithubContext context) { await this.fullScanDelegate(context); if (context.Available == true) { this.contextCollection.Add(context); } } } public async Task ScanResultAsync() { this.logger.LogInformation("结果扫描开始"); var contexts = this.contextCollection.ToArray(); foreach (var context in contexts) { await this.resultScanDelegate(context); } this.logger.LogInformation("结果扫描结束"); } } }