64 lines
2.1 KiB
C#
64 lines
2.1 KiB
C#
using ConsoleApp2.Entities;
|
|
using ConsoleApp2.Helpers;
|
|
using ConsoleApp2.Services;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Hosting;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace ConsoleApp2.HostedServices;
|
|
|
|
public class SqlFileOutputService : BackgroundService
|
|
{
|
|
private readonly string _outputFile = "D:/DumpOutput/cferp_test_1.sql"; //
|
|
private readonly DataRecordQueue _consumerQueue;
|
|
private readonly ILogger _logger;
|
|
private readonly ProcessContext _context;
|
|
|
|
public SqlFileOutputService(
|
|
ILogger<SqlFileOutputService> logger,
|
|
[FromKeyedServices(ProcessStep.Consumer)]
|
|
DataRecordQueue consumerQueue,
|
|
ProcessContext context)
|
|
{
|
|
_logger = logger;
|
|
_consumerQueue = consumerQueue;
|
|
_context = context;
|
|
}
|
|
|
|
|
|
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
|
{
|
|
_logger.LogInformation("***** Sql file output service started, thread id: {ThreadId} *****", Environment.CurrentManagedThreadId);
|
|
var count = 0;
|
|
var tableRecords = new Dictionary<string, IList<DataRecord>>();
|
|
while (!_context.IsTransformCompleted || _consumerQueue.Count > 0)
|
|
{
|
|
if (!_consumerQueue.TryDequeue(out var record)) continue;
|
|
|
|
tableRecords.AddOrUpdate(record.TableName, [record], (key, value) =>
|
|
{
|
|
value.Add(record);
|
|
return value;
|
|
});
|
|
|
|
++count;
|
|
|
|
if (count >= 200)
|
|
{
|
|
await File.AppendAllTextAsync(_outputFile,
|
|
MySqlDestination.SerializeRecords(tableRecords), stoppingToken);
|
|
tableRecords.Clear();
|
|
_context.AddOutput(count);
|
|
count = 0;
|
|
}
|
|
}
|
|
await File.AppendAllTextAsync(_outputFile,
|
|
MySqlDestination.SerializeRecords(tableRecords), stoppingToken);
|
|
tableRecords.Clear();
|
|
_context.AddOutput(count);
|
|
_context.CompleteOutput();
|
|
|
|
_logger.LogInformation("***** Sql file output service completed *****");
|
|
}
|
|
|
|
} |