MES-ETL/ConsoleApp2/HostedServices/InputService.cs

71 lines
2.6 KiB
C#
Raw Normal View History

2024-01-04 09:00:44 +08:00
using ConsoleApp2.Const;
using ConsoleApp2.Helpers;
using ConsoleApp2.HostedServices.Abstractions;
2023-12-29 16:16:05 +08:00
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.Logging;
using Microsoft.Extensions.Options;
2023-12-29 16:16:05 +08:00
namespace ConsoleApp2.HostedServices;
2023-12-28 15:18:03 +08:00
2024-01-04 09:00:44 +08:00
/// <summary>
/// 从MyDumper导出的CSV文件中导入表头和数据
/// </summary>
public class InputService : IInputService
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 DataRecordQueue _producerQueue;
private readonly ProcessContext _context;
2023-12-28 15:18:03 +08:00
2024-01-04 09:00:44 +08:00
public InputService(ILogger<InputService> logger,
IOptions<CsvOptions> csvOptions,
2023-12-29 16:16:05 +08:00
[FromKeyedServices(ProcessStep.Producer)]DataRecordQueue producerQueue,
ProcessContext context)
2023-12-28 15:18:03 +08:00
{
_logger = logger;
_csvOptions = csvOptions;
2023-12-29 16:16:05 +08:00
_producerQueue = producerQueue;
_context = context;
2023-12-28 15:18:03 +08:00
}
2024-01-04 09:00:44 +08:00
public async Task ExecuteAsync(CancellationToken cancellationToken)
2023-12-28 15:18:03 +08:00
{
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);
2024-01-04 09:00:44 +08:00
var source = new CsvSource(csvPath, headers, _csvOptions.Value.Delimiter, _csvOptions.Value.QuoteChar, _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 *****");
}
}