69 lines
2.5 KiB
C#
69 lines
2.5 KiB
C#
using ConsoleApp2.Const;
|
|
using ConsoleApp2.Helpers;
|
|
using ConsoleApp2.HostedServices.Abstractions;
|
|
using ConsoleApp2.Options;
|
|
using ConsoleApp2.Services;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.Extensions.Options;
|
|
|
|
namespace ConsoleApp2.HostedServices;
|
|
|
|
/// <summary>
|
|
/// 从MyDumper导出的CSV文件中导入表头和数据
|
|
/// </summary>
|
|
public class InputService : IInputService
|
|
{
|
|
private readonly ILogger _logger;
|
|
private readonly IOptions<DataInputOptions> _dataInputOptions;
|
|
private readonly IOptions<InputTableOptions> _tableOptions;
|
|
private readonly DataRecordQueue _producerQueue;
|
|
private readonly ProcessContext _context;
|
|
|
|
public InputService(ILogger<InputService> logger,
|
|
IOptions<DataInputOptions> dataInputOptions,
|
|
IOptions<InputTableOptions> tableOptions,
|
|
[FromKeyedServices(ProcessStep.Producer)] DataRecordQueue producerQueue,
|
|
ProcessContext context)
|
|
{
|
|
_logger = logger;
|
|
_dataInputOptions = dataInputOptions;
|
|
_tableOptions = tableOptions;
|
|
_producerQueue = producerQueue;
|
|
_context = context;
|
|
}
|
|
|
|
public async Task ExecuteAsync(CancellationToken cancellationToken)
|
|
{
|
|
var inputDir = _dataInputOptions.Value.InputDir;
|
|
_logger.LogInformation("***** Csv input service start, working dir: {InputDir}, thread id: {ThreadId} *****", inputDir, Environment.CurrentManagedThreadId);
|
|
var files = Directory.GetFiles(inputDir);
|
|
if (files.Length == 0)
|
|
{
|
|
_logger.LogInformation("No source files found in {InputDir}", inputDir);
|
|
return;
|
|
}
|
|
var count = 0;
|
|
foreach (var tableName in _tableOptions.Value.TableInfoConfig.Keys)
|
|
{
|
|
_logger.LogInformation("Working table: {tableName}", tableName);
|
|
var source = _dataInputOptions.Value.CreateSource?.Invoke(tableName);
|
|
await source.DoEnqueue((record) =>
|
|
{
|
|
_context.AddInput();
|
|
_producerQueue.Enqueue(record);
|
|
count++;
|
|
|
|
});
|
|
if (_context.GetExceptions().Count > 0)
|
|
{
|
|
_logger.LogInformation("***** Csv input service is canceled *****");
|
|
return;
|
|
}
|
|
_logger.LogInformation("table:'{tableName}' input completed", tableName);
|
|
}
|
|
|
|
_context.CompleteInput();
|
|
_logger.LogInformation("***** Csv input service completed *****");
|
|
}
|
|
} |