MES-ETL/ConsoleApp2/HostedServices/CsvInputService.cs

70 lines
2.7 KiB
C#
Raw Normal View History

2023-12-29 16:16:05 +08:00
using ConsoleApp2.Helpers;
using ConsoleApp2.Options;
2023-12-28 15:18:03 +08:00
using ConsoleApp2.Services;
2023-12-29 16:16:05 +08:00
using Microsoft.Extensions.DependencyInjection;
2023-12-28 15:18:03 +08:00
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
2023-12-29 16:16:05 +08:00
namespace ConsoleApp2.HostedServices;
2023-12-28 15:18:03 +08:00
2023-12-29 16:16:05 +08:00
public class CsvInputService : BackgroundService
2023-12-28 15:18:03 +08:00
{
private readonly ILogger _logger;
private readonly IOptions<CsvOptions> _csvOptions;
2023-12-29 16:16:05 +08:00
private readonly TaskManager _taskManager; // TBD
private readonly DataRecordQueue _producerQueue;
private readonly ProcessContext _context;
2023-12-28 15:18:03 +08:00
2023-12-29 16:16:05 +08:00
public CsvInputService(ILogger<CsvInputService> logger,
IOptions<CsvOptions> csvOptions,
[FromKeyedServices(ProcessStep.Producer)]TaskManager taskManager,
[FromKeyedServices(ProcessStep.Producer)]DataRecordQueue producerQueue,
ProcessContext context)
2023-12-28 15:18:03 +08:00
{
_logger = logger;
_csvOptions = csvOptions;
_taskManager = taskManager;
2023-12-29 16:16:05 +08:00
_producerQueue = producerQueue;
_context = context;
2023-12-28 15:18:03 +08:00
}
protected override async Task ExecuteAsync(CancellationToken cancellationToken)
{
var inputDir = _csvOptions.Value.InputDir;
2023-12-29 16:16:05 +08:00
_logger.LogInformation("***** Csv input service start, working dir: {InputDir}, thread id: {ThreadId} *****", inputDir, Environment.CurrentManagedThreadId);
2023-12-28 15:18:03 +08:00
var files = Directory.GetFiles(inputDir).Where(s => s.EndsWith(".sql") && !s.Contains("schema")).ToArray();
if (files.Length == 0)
{
_logger.LogInformation("No sql files found in {InputDir}", inputDir);
return;
}
2023-12-29 16:16:05 +08:00
foreach (var sqlPath in files)
2023-12-28 15:18:03 +08:00
{
_logger.LogInformation("Working sql file: {SqlPath}", sqlPath);
var headers = await DumpDataHelper.GetCsvHeadersFromSqlFileAsync(sqlPath);
var csvFiles = await DumpDataHelper.GetCsvFileNamesFromSqlFileAsync(sqlPath);
foreach (var csvFile in csvFiles)
{
var csvPath = Path.Combine(inputDir, csvFile);
2023-12-29 16:16:05 +08:00
// var source = new JsvSource(csvPath, headers, _logger);
var source = new NewCsvSource(csvPath, headers, logger: _logger);
2023-12-28 15:18:03 +08:00
while (await source.ReadAsync())
{
2023-12-29 16:16:05 +08:00
_context.AddInput();
_producerQueue.Enqueue(source.Current);
if (cancellationToken.IsCancellationRequested)
return;
2023-12-28 15:18:03 +08:00
}
}
2023-12-29 16:16:05 +08:00
2023-12-28 15:18:03 +08:00
_logger.LogInformation("File '{File}' input completed", Path.GetFileName(sqlPath));
}
2023-12-29 16:16:05 +08:00
_context.CompleteInput();
2023-12-28 15:18:03 +08:00
_logger.LogInformation("***** Csv input service completed *****");
}
}