GithubResultScanHostedService.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. using FastGithub.Scanner;
  2. using Microsoft.Extensions.Hosting;
  3. using Microsoft.Extensions.Options;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. namespace FastGithub
  7. {
  8. /// <summary>
  9. /// 扫描结果轮询扫描后台服务
  10. /// </summary>
  11. sealed class GithubResultScanHostedService : BackgroundService
  12. {
  13. private readonly GithubScanService githubScanService;
  14. private readonly IOptionsMonitor<GithubScanOptions> options;
  15. /// <summary>
  16. /// 扫描结果轮询扫描后台服务
  17. /// </summary>
  18. /// <param name="githubScanService"></param>
  19. /// <param name="options"></param>
  20. public GithubResultScanHostedService(
  21. GithubScanService githubScanService,
  22. IOptionsMonitor<GithubScanOptions> options)
  23. {
  24. this.githubScanService = githubScanService;
  25. this.options = options;
  26. }
  27. /// <summary>
  28. /// 后台轮询扫描
  29. /// </summary>
  30. /// <param name="stoppingToken"></param>
  31. /// <returns></returns>
  32. protected override async Task ExecuteAsync(CancellationToken stoppingToken)
  33. {
  34. while (stoppingToken.IsCancellationRequested == false)
  35. {
  36. await Task.Delay(this.options.CurrentValue.ResultScanInterval, stoppingToken);
  37. await githubScanService.ScanResultAsync();
  38. }
  39. }
  40. }
  41. }