-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathStrategyRunnerBackgroundService.cs
54 lines (46 loc) · 2.18 KB
/
StrategyRunnerBackgroundService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
using DevelopmentInProgress.TradeView.Core.Server;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System;
using System.Threading;
using System.Threading.Tasks;
using System.Threading.Tasks.Dataflow;
namespace DevelopmentInProgress.TradeServer.StrategyExecution.WebHost.Web.HostedService
{
public class StrategyRunnerBackgroundService : BackgroundService
{
private readonly IServerMonitor serverMonitor;
private readonly ILogger logger;
private readonly IStrategyRunnerActionBlock strategyRunnerActionBlock;
private CancellationToken cancellationToken;
public StrategyRunnerBackgroundService(IServerMonitor serverMonitor, IStrategyRunnerActionBlock strategyRunnerActionBlock, ILoggerFactory loggerFactory)
{
this.serverMonitor = serverMonitor;
this.strategyRunnerActionBlock = strategyRunnerActionBlock;
logger = loggerFactory.CreateLogger<StrategyRunnerBackgroundService>();
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types")]
protected async override Task ExecuteAsync(CancellationToken cancellationToken)
{
this.cancellationToken = cancellationToken;
logger.LogInformation("ExecuteAsync");
try
{
strategyRunnerActionBlock.ActionBlock = new ActionBlock<StrategyRunnerActionBlockInput>(async actionBlockInput =>
{
await actionBlockInput.StrategyRunner.RunAsync(actionBlockInput.Strategy, actionBlockInput.DownloadsPath, this.cancellationToken).ConfigureAwait(false);
},
new ExecutionDataflowBlockOptions { MaxDegreeOfParallelism = serverMonitor.MaxDegreeOfParallelism });
while (!this.cancellationToken.IsCancellationRequested)
{
await Task.Delay(1000, this.cancellationToken).ConfigureAwait(false);
}
strategyRunnerActionBlock.ActionBlock.Complete();
}
catch (Exception ex)
{
logger.LogError(ex, "ExecuteAsync");
}
}
}
}