Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

71 lines
2.4KB

  1. using HealthMonitor.Common;
  2. using HealthMonitor.Core.Common.Extensions;
  3. using HealthMonitor.Service.Sub;
  4. using TDengineDriver;
  5. using TDengineTMQ;
  6. namespace HealthMonitor.WebApi
  7. {
  8. public class Worker : BackgroundService
  9. {
  10. private readonly ILogger<Worker> _logger;
  11. private readonly IServiceProvider _services;
  12. private readonly TDengineDataSubcribe _tdEngineDataSubcribe;
  13. private readonly PackageProcess _processor;
  14. private CancellationTokenSource _tokenSource=default!;
  15. public Worker(ILogger<Worker> logger, IServiceProvider services, PackageProcess processor,TDengineDataSubcribe tdEngineDataSubcribe)
  16. {
  17. _logger = logger;
  18. _tdEngineDataSubcribe = tdEngineDataSubcribe;
  19. _services = services;
  20. _processor = processor;
  21. }
  22. public override Task StartAsync(CancellationToken cancellationToken)
  23. {
  24. _logger.LogInformation("------StartAsync");
  25. _tokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
  26. // 创建消费者
  27. // _tdEngineDataSubcribe.CreateConsumer();
  28. return base.StartAsync(cancellationToken);
  29. }
  30. public override Task StopAsync(CancellationToken cancellationToken)
  31. {
  32. _logger.LogInformation("------StopAsync");
  33. _tokenSource.Cancel(); //停止工作线程
  34. // 关闭消费者
  35. // _tdEngineDataSubcribe.CloseConsumer();
  36. return base.StopAsync(cancellationToken);
  37. }
  38. protected override Task ExecuteAsync(CancellationToken stoppingToken)
  39. {
  40. // var processor = _services.GetService<PackageProcess>();
  41. TaskFactory factory = new(_tokenSource.Token);
  42. factory.StartNew(async () =>
  43. {
  44. if (_tokenSource.IsCancellationRequested)
  45. _logger.LogWarning("Worker exit");
  46. while (!_tokenSource.IsCancellationRequested)
  47. {
  48. await _processor.ResolveAsync().ConfigureAwait(false);
  49. // await _tdEngineDataSubcribe.ProcessMsg();
  50. }
  51. }, TaskCreationOptions.LongRunning);
  52. while (!_tokenSource.IsCancellationRequested)
  53. {
  54. _tdEngineDataSubcribe.BeginListen(_tokenSource.Token);
  55. }
  56. return Task.Delay(1000, _tokenSource.Token);
  57. }
  58. }
  59. }