MES-ETL/ConsoleApp2/HostedServices/InputService.cs

63 lines
2.2 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;
2024-01-12 16:50:37 +08:00
private readonly IOptions<DataInputOptions> _dataInputOptions;
2023-12-29 16:16:05 +08:00
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,
2024-01-12 16:50:37 +08:00
IOptions<DataInputOptions> dataInputOptions,
ProcessContext context)
2023-12-28 15:18:03 +08:00
{
_logger = logger;
2024-01-12 16:50:37 +08:00
_dataInputOptions = dataInputOptions;
2023-12-29 16:16:05 +08:00
_context = context;
2023-12-28 15:18:03 +08:00
}
2024-01-18 14:36:36 +08:00
public async Task ExecuteAsync(TasksOptions tasksOptions, DataRecordQueue producerQueue, ProcessContext context,CancellationToken cancellationToken)
2023-12-28 15:18:03 +08:00
{
2024-01-12 16:50:37 +08:00
var inputDir = _dataInputOptions.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);
2024-01-12 16:50:37 +08:00
var files = Directory.GetFiles(inputDir);
2023-12-28 15:18:03 +08:00
if (files.Length == 0)
{
2024-01-12 16:50:37 +08:00
_logger.LogInformation("No source files found in {InputDir}", inputDir);
2023-12-28 15:18:03 +08:00
return;
}
2024-01-12 16:50:37 +08:00
var count = 0;
2024-01-18 14:36:36 +08:00
foreach (var tableName in tasksOptions.TableInfoConfig.Keys)
2023-12-28 15:18:03 +08:00
{
2024-01-12 16:50:37 +08:00
_logger.LogInformation("Working table: {tableName}", tableName);
var source = _dataInputOptions.Value.CreateSource?.Invoke(tableName);
await source.DoEnqueue((record) =>
2023-12-28 15:18:03 +08:00
{
2024-01-12 16:50:37 +08:00
_context.AddInput();
2024-01-18 14:36:36 +08:00
producerQueue.Enqueue(record);
2024-01-12 16:50:37 +08:00
count++;
2023-12-28 15:18:03 +08:00
2024-01-12 16:50:37 +08:00
});
if (_context.GetExceptions().Count > 0)
{
_logger.LogInformation("***** Csv input service is canceled *****");
return;
2023-12-28 15:18:03 +08:00
}
2024-01-12 16:50:37 +08:00
_logger.LogInformation("table:'{tableName}' input completed", tableName);
2023-12-28 15:18:03 +08:00
}
2024-01-18 14:36:36 +08:00
context.CompleteInput();
2023-12-28 15:18:03 +08:00
_logger.LogInformation("***** Csv input service completed *****");
}
}