修改
This commit is contained in:
@@ -5,6 +5,8 @@ using ConsoleApp2.Services;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using MySqlConnector;
|
||||
using System.Threading;
|
||||
|
||||
namespace ConsoleApp2.HostedServices;
|
||||
|
||||
@@ -32,36 +34,40 @@ public class OutputService : IOutputService
|
||||
_taskManager = taskManager;
|
||||
}
|
||||
|
||||
public async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
public async Task ExecuteAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
_logger.LogInformation("***** Mysql output service started *****");
|
||||
|
||||
var records = new List<DataRecord>();
|
||||
while (!_context.IsTransformCompleted || _consumerQueue.Count > 0)
|
||||
var count = 0;
|
||||
_taskManager.CreateTasks(async () =>
|
||||
{
|
||||
if (!_consumerQueue.TryDequeue(out var record)) continue;
|
||||
records.Add(record);
|
||||
|
||||
if (records.Count >= _options.Value.FlushCount)
|
||||
var records = new List<DataRecord>();
|
||||
while (!_context.IsTransformCompleted || _consumerQueue.Count > 0)
|
||||
{
|
||||
var recordsCopy = records;
|
||||
_taskManager.CreateTask(async () => await FlushAsync(recordsCopy), stoppingToken);
|
||||
records = [];
|
||||
if (!_consumerQueue.TryDequeue(out var record)) continue;
|
||||
records.Add(record);
|
||||
count++;
|
||||
//_logger.LogInformation(@"*****OutputCount: {count} *****",count);
|
||||
if (records.Count >= _options.Value.FlushCount)
|
||||
{
|
||||
await FlushAsync(records);
|
||||
records.Clear();
|
||||
}
|
||||
if (_context.GetExceptions().Count>0)
|
||||
{
|
||||
_logger.LogInformation("***** Csv output service is canceled *****");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (_taskManager.TaskCount >= _options.Value.MaxTask)
|
||||
if (_context.IsTransformCompleted && records.Count > 0)
|
||||
{
|
||||
await _taskManager.WaitAll();
|
||||
_taskManager.ClearTask();
|
||||
await FlushAsync(records);
|
||||
records.Clear();
|
||||
_context.CompleteOutput();
|
||||
_logger.LogInformation("***** Mysql output service completed *****");
|
||||
}
|
||||
}
|
||||
}, _options.Value.TaskCount);
|
||||
|
||||
await _taskManager.WaitAll();
|
||||
await FlushAsync(records);
|
||||
|
||||
_context.CompleteOutput();
|
||||
|
||||
_logger.LogInformation("***** Mysql output service completed *****");
|
||||
}
|
||||
|
||||
private async Task FlushAsync(IEnumerable<DataRecord> records)
|
||||
@@ -69,15 +75,29 @@ public class OutputService : IOutputService
|
||||
var count = 0;
|
||||
await using var output = new MySqlDestination(
|
||||
_options.Value.ConnectionString ?? throw new InvalidOperationException("Connection string is required"),
|
||||
_logger, true);
|
||||
|
||||
_logger, _context,true);
|
||||
//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);
|
||||
foreach (var record in records)
|
||||
{
|
||||
await output.WriteRecordAsync(record);
|
||||
count++;
|
||||
}
|
||||
|
||||
await output.FlushAsync();
|
||||
await output.FlushAsync(_options.Value.MaxAllowedPacket);
|
||||
_context.AddOutput(count);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user