35 lines
1.2 KiB
C#
35 lines
1.2 KiB
C#
|
using ConsoleApp2.Services;
|
|||
|
using Microsoft.Extensions.DependencyInjection;
|
|||
|
using Microsoft.Extensions.Hosting;
|
|||
|
using Microsoft.Extensions.Logging;
|
|||
|
|
|||
|
namespace ConsoleApp2.HostedServices;
|
|||
|
|
|||
|
public class VoidOutputService : BackgroundService
|
|||
|
{
|
|||
|
private readonly ILogger _logger;
|
|||
|
private readonly DataRecordQueue _consumerQueue;
|
|||
|
private readonly ProcessContext _context;
|
|||
|
|
|||
|
public VoidOutputService([FromKeyedServices(ProcessStep.Consumer)] DataRecordQueue consumerQueue,
|
|||
|
ProcessContext context, ILogger<VoidOutputService> logger)
|
|||
|
{
|
|||
|
_consumerQueue = consumerQueue;
|
|||
|
_context = context;
|
|||
|
_logger = logger;
|
|||
|
}
|
|||
|
|
|||
|
protected override Task ExecuteAsync(CancellationToken stoppingToken)
|
|||
|
{
|
|||
|
_logger.LogInformation("***** Void output service started, thread id: {ThreadId} *****", Environment.CurrentManagedThreadId);
|
|||
|
while (!_context.IsTransformCompleted || _consumerQueue.Count > 0)
|
|||
|
{
|
|||
|
if (_consumerQueue.TryDequeue(out var record))
|
|||
|
_context.AddOutput();
|
|||
|
}
|
|||
|
|
|||
|
_context.CompleteOutput();
|
|||
|
_logger.LogInformation("***** Void output service completed *****");
|
|||
|
return Task.CompletedTask;
|
|||
|
}
|
|||
|
}
|