|
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- using HealthMonitor.Common;
- using HealthMonitor.Core.Common.Extensions;
- using HealthMonitor.Service.Sub;
- using TDengineDriver;
- using TDengineTMQ;
-
- namespace HealthMonitor.WebApi
- {
- public class Worker : BackgroundService
- {
- private readonly ILogger<Worker> _logger;
- private readonly IServiceProvider _services;
- private readonly TDengineDataSubcribe _tdEngineDataSubcribe;
- private readonly PackageProcess _processor;
-
- private CancellationTokenSource _tokenSource=default!;
-
- public Worker(ILogger<Worker> logger, IServiceProvider services, PackageProcess processor,TDengineDataSubcribe tdEngineDataSubcribe)
- {
- _logger = logger;
- _tdEngineDataSubcribe = tdEngineDataSubcribe;
- _services = services;
- _processor = processor;
- }
-
- public override Task StartAsync(CancellationToken cancellationToken)
- {
- _logger.LogInformation("------StartAsync");
- _tokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
- // 创建消费者
- // _tdEngineDataSubcribe.CreateConsumer();
- return base.StartAsync(cancellationToken);
- }
-
- public override Task StopAsync(CancellationToken cancellationToken)
- {
- _logger.LogInformation("------StopAsync");
- _tokenSource.Cancel(); //停止工作线程
- // 关闭消费者
- // _tdEngineDataSubcribe.CloseConsumer();
- return base.StopAsync(cancellationToken);
- }
-
- protected override Task ExecuteAsync(CancellationToken stoppingToken)
- {
- // var processor = _services.GetService<PackageProcess>();
- TaskFactory factory = new(_tokenSource.Token);
- factory.StartNew(async () =>
- {
- if (_tokenSource.IsCancellationRequested)
- _logger.LogWarning("Worker exit");
-
- while (!_tokenSource.IsCancellationRequested)
- {
- await _processor.ResolveAsync().ConfigureAwait(false);
- // await _tdEngineDataSubcribe.ProcessMsg();
- }
-
- }, TaskCreationOptions.LongRunning);
-
- while (!_tokenSource.IsCancellationRequested)
- {
- _tdEngineDataSubcribe.BeginListen(_tokenSource.Token);
- }
- return Task.Delay(1000, _tokenSource.Token);
-
- }
-
- }
- }
|