整理代码
This commit is contained in:
@@ -5,14 +5,15 @@ using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
namespace ConsoleApp2.HostedServices;
|
||||
|
||||
public class MainHostedService : BackgroundService
|
||||
public class MainHostedService : IHostedService
|
||||
{
|
||||
private readonly ILogger _logger;
|
||||
private readonly IInputService _input;
|
||||
private readonly ITransformService _transform;
|
||||
private readonly IOutputService _output;
|
||||
private readonly ProcessContext _context;
|
||||
|
||||
private readonly Timer? _bigTableTimer;
|
||||
private readonly Timer? _smallTableTimer;
|
||||
public MainHostedService(ILogger<MainHostedService> logger, IInputService input, ITransformService transform, IOutputService output, ProcessContext context)
|
||||
{
|
||||
_logger = logger;
|
||||
@@ -22,15 +23,15 @@ public class MainHostedService : BackgroundService
|
||||
_context = context;
|
||||
}
|
||||
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
public Task StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
var taskFun = (TasksOptions taskOp, DataRecordQueue producerQueue, DataRecordQueue consumerQueue, ProcessContext context) =>
|
||||
var taskFun = (TasksOptions taskOp, DataRecordQueue producerQueue, DataRecordQueue consumerQueue, ProcessContext context,Timer? timer) =>
|
||||
{
|
||||
var inputTask = Task.Factory.StartNew(async () =>
|
||||
Task.Factory.StartNew(async () =>
|
||||
{
|
||||
try
|
||||
{
|
||||
await _input.ExecuteAsync(taskOp, producerQueue, context, stoppingToken);
|
||||
await _input.ExecuteAsync(taskOp, producerQueue, context, cancellationToken);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -39,11 +40,11 @@ public class MainHostedService : BackgroundService
|
||||
}
|
||||
|
||||
});
|
||||
var transformTask = Task.Factory.StartNew(async () =>
|
||||
Task.Factory.StartNew(async () =>
|
||||
{
|
||||
try
|
||||
{
|
||||
await _transform.ExecuteAsync(taskOp, producerQueue, consumerQueue, context, stoppingToken);
|
||||
await _transform.ExecuteAsync(taskOp, producerQueue, consumerQueue, context, cancellationToken);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -52,11 +53,15 @@ public class MainHostedService : BackgroundService
|
||||
}
|
||||
|
||||
});
|
||||
var outputTask = Task.Factory.StartNew(async () =>
|
||||
Task.Factory.StartNew(() =>
|
||||
{
|
||||
try
|
||||
{
|
||||
await _output.ExecuteAsync(taskOp, consumerQueue, context,stoppingToken);
|
||||
timer = new Timer((object? state) =>
|
||||
{
|
||||
_output.ExecuteAsync(taskOp, consumerQueue, context, cancellationToken);
|
||||
},null, TimeSpan.Zero,TimeSpan.FromSeconds(0.5));
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -77,8 +82,8 @@ public class MainHostedService : BackgroundService
|
||||
{"simple_plan_order",new TableInfo{SimulaRowCount=351470 }},//CreateTime < 202301的删除
|
||||
};
|
||||
var bigTableContext = new ProcessContext();
|
||||
var bigTableOptions = new TasksOptions { TableInfoConfig = bigTablesDic, OutPutOptions = new OutPutOptions { FlushCount = 10000, OutPutTaskCount = 2 } };
|
||||
taskFun(bigTableOptions,new DataRecordQueue(), new DataRecordQueue(), bigTableContext);
|
||||
var bigTableOptions = new TasksOptions { TableInfoConfig = bigTablesDic, OutPutOptions = new OutPutOptions { FlushCount = 20000, OutPutTaskCount = 2 } };
|
||||
taskFun(bigTableOptions, new DataRecordQueue(), new DataRecordQueue(), bigTableContext,_bigTableTimer);
|
||||
var smallTablesDic = new Dictionary<string, TableInfo>
|
||||
{
|
||||
{"machine",new TableInfo{SimulaRowCount=14655 }},
|
||||
@@ -107,6 +112,14 @@ public class MainHostedService : BackgroundService
|
||||
};
|
||||
var smallTableContext = new ProcessContext();
|
||||
taskFun(new TasksOptions { TableInfoConfig = smallTablesDic, OutPutOptions = new OutPutOptions { FlushCount = 20000, OutPutTaskCount = 4 } },
|
||||
new DataRecordQueue(), new DataRecordQueue(), smallTableContext);
|
||||
new DataRecordQueue(), new DataRecordQueue(), smallTableContext,_smallTableTimer);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task StopAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
|
||||
}
|
Reference in New Issue
Block a user