2024-01-22 15:44:37 +08:00
|
|
|
|
|
2024-01-04 09:00:44 +08:00
|
|
|
|
using ConsoleApp2.HostedServices.Abstractions;
|
2023-12-29 16:16:05 +08:00
|
|
|
|
using ConsoleApp2.Options;
|
|
|
|
|
using ConsoleApp2.Services;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using Microsoft.Extensions.Options;
|
|
|
|
|
namespace ConsoleApp2.HostedServices;
|
|
|
|
|
|
2024-01-04 09:00:44 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 数据导出服务,将数据导出至MySql服务
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class OutputService : IOutputService
|
2023-12-29 16:16:05 +08:00
|
|
|
|
{
|
|
|
|
|
private readonly ILogger _logger;
|
2024-01-16 15:35:54 +08:00
|
|
|
|
private readonly IOptions<DatabaseOutputOptions> _outputOptions;
|
|
|
|
|
private readonly IOptions<DataTransformOptions> _transformOptions;
|
2023-12-29 16:16:05 +08:00
|
|
|
|
private readonly ProcessContext _context;
|
2024-01-04 09:00:44 +08:00
|
|
|
|
private readonly TaskManager _taskManager;
|
2024-01-18 15:03:45 +08:00
|
|
|
|
private readonly ErrorRecorder _errorRecorder;
|
2023-12-29 16:16:05 +08:00
|
|
|
|
|
2024-01-04 09:00:44 +08:00
|
|
|
|
public OutputService(ILogger<OutputService> logger,
|
2024-01-16 15:35:54 +08:00
|
|
|
|
IOptions<DatabaseOutputOptions> outputOptions,
|
2024-01-04 09:00:44 +08:00
|
|
|
|
ProcessContext context,
|
2024-01-16 15:35:54 +08:00
|
|
|
|
TaskManager taskManager,
|
2024-01-18 15:03:45 +08:00
|
|
|
|
IOptions<DataTransformOptions> transformOptions,
|
|
|
|
|
ErrorRecorder errorRecorder)
|
2023-12-29 16:16:05 +08:00
|
|
|
|
{
|
|
|
|
|
_logger = logger;
|
2024-01-16 15:35:54 +08:00
|
|
|
|
_outputOptions = outputOptions;
|
2023-12-29 16:16:05 +08:00
|
|
|
|
_context = context;
|
2024-01-04 09:00:44 +08:00
|
|
|
|
_taskManager = taskManager;
|
2024-01-16 15:35:54 +08:00
|
|
|
|
_transformOptions = transformOptions;
|
2024-01-18 15:03:45 +08:00
|
|
|
|
_errorRecorder = errorRecorder;
|
2023-12-29 16:16:05 +08:00
|
|
|
|
}
|
2024-01-22 15:44:37 +08:00
|
|
|
|
private int _runingTaskCount;
|
|
|
|
|
public int RuningTaskCount
|
2023-12-29 16:16:05 +08:00
|
|
|
|
{
|
2024-01-22 15:44:37 +08:00
|
|
|
|
get => _runingTaskCount;
|
|
|
|
|
}
|
|
|
|
|
public void DoTask() => Interlocked.Increment(ref _runingTaskCount);
|
|
|
|
|
public void FinishTask() => Interlocked.Decrement(ref _runingTaskCount);
|
|
|
|
|
public void ExecuteAsync(TasksOptions tasksOptions, DataRecordQueue consumerQueue, ProcessContext context, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
if (context.IsTransformCompleted == false && consumerQueue.Count < tasksOptions.OutPutOptions.FlushCount) return;
|
|
|
|
|
if (RuningTaskCount >= tasksOptions.OutPutOptions.OutPutTaskCount ) return;
|
2024-01-19 13:47:35 +08:00
|
|
|
|
var records = new List<DataRecord>();
|
|
|
|
|
|
2024-01-22 15:44:37 +08:00
|
|
|
|
for (int i = 0; i < tasksOptions.OutPutOptions.FlushCount; i++)
|
|
|
|
|
{
|
|
|
|
|
if (consumerQueue.TryDequeue(out var record)) records.Add(record);
|
|
|
|
|
else break;
|
2024-01-19 13:47:35 +08:00
|
|
|
|
}
|
|
|
|
|
if (records.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
ThreadPool.QueueUserWorkItem(async (queueState) =>
|
|
|
|
|
{
|
2024-01-22 15:44:37 +08:00
|
|
|
|
DoTask();
|
|
|
|
|
await FlushAsync(records);
|
|
|
|
|
FinishTask();
|
2024-01-19 13:47:35 +08:00
|
|
|
|
});
|
|
|
|
|
}
|
2023-12-29 16:16:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task FlushAsync(IEnumerable<DataRecord> records)
|
|
|
|
|
{
|
|
|
|
|
var count = 0;
|
2024-01-04 09:00:44 +08:00
|
|
|
|
await using var output = new MySqlDestination(
|
2024-01-16 15:35:54 +08:00
|
|
|
|
_outputOptions.Value.ConnectionString ?? throw new InvalidOperationException("Connection string is required"),
|
2024-01-18 15:03:45 +08:00
|
|
|
|
_logger, _context, _transformOptions, _errorRecorder);
|
2024-01-12 16:50:37 +08:00
|
|
|
|
//if (records == null || records.Count() == 0) return;
|
|
|
|
|
//var dbName = $"cferp_test_1";
|
|
|
|
|
//if (records != null && records.Count() > 0)
|
|
|
|
|
//{
|
|
|
|
|
// dbName = $"cferp_test_{records.FirstOrDefault()?.CompanyID}";
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
//await using var output = new MySqlDestination(new MySqlConnectionStringBuilder
|
|
|
|
|
//{
|
|
|
|
|
// Server = "127.0.0.1",
|
|
|
|
|
// Port = 34309,
|
|
|
|
|
// Database = dbName,
|
|
|
|
|
// UserID = "root",
|
|
|
|
|
// Password = "123456",
|
|
|
|
|
// MaximumPoolSize = 50,
|
|
|
|
|
//}.ConnectionString, _logger,true);
|
2023-12-29 16:16:05 +08:00
|
|
|
|
foreach (var record in records)
|
|
|
|
|
{
|
|
|
|
|
await output.WriteRecordAsync(record);
|
|
|
|
|
count++;
|
|
|
|
|
}
|
2024-01-16 15:35:54 +08:00
|
|
|
|
await output.FlushAsync(_outputOptions.Value.MaxAllowedPacket);
|
2023-12-29 16:16:05 +08:00
|
|
|
|
_context.AddOutput(count);
|
|
|
|
|
}
|
|
|
|
|
}
|