Files
zszq-trs/YLErpWeb/App/HostedTaskService.cs
T

161 lines
5.1 KiB
C#

using Microsoft.AspNetCore.Hosting.Server;
using Microsoft.AspNetCore.Hosting.Server.Features;
using YLErp.DBModels.Consts;
using YLErp.Modules.AppModule;
using YLErp.Modules.EodModule.SettlementModule;
using YLErp.Modules.RiskEngine;
using YLErp.Modules.RiskModule;
using YLErp.Modules.SuperviseReportModule.SAC.Service;
using YLErp.Modules.TradeRiskCalcModule;
namespace YLErp.Web.App
{
//--------------------------------------
// 后台执行任务
//--------------------------------------
public class HostedTaskService : IHostedService
{
Timer _timer, _timerMonitor;
private readonly IServer server;
private readonly IHostApplicationLifetime hostApplicationLifetime;
public HostedTaskService(IServer server, IHostApplicationLifetime hostApplicationLifetime)
{
this.server = server;
this.hostApplicationLifetime = hostApplicationLifetime;
}
private void TimerExecute(object state)
{
if (state != null)
{
Task.Run(() =>
{
try
{
EodTaskRunner.Execute();
}
catch (Exception ex)
{
LogFactory.GetLogger("EodTaskRunner").Error(ex);
}
});
}
if (AppHelper.RunTradeRiskCalcV2)
{
Task.Run(() =>
{
var index = 0;
var voltypes = new string[5];
if (AppHelper.IsSupportVolTypeOnRiskCalc("对冲"))
{
voltypes[index++] = "对冲";
}
if (AppHelper.IsSupportVolTypeOnRiskCalc("持仓"))
{
voltypes[index++] = "持仓";
}
if (AppHelper.IsSupportVolTypeOnRiskCalc("交易曲面"))
{
voltypes[index++] = "交易曲面";
}
if (AppHelper.IsSupportVolTypeOnRiskCalc("场内实时持仓API"))
{
AppContext.SetSwitch("场内实时持仓API", true);
}
TradeRiskCalcTaskRunner.Execute(voltypes);
});
}
Task.Run(() =>
{
try
{
if (PS.Config.ErpElement.SecuritiesEnvironment)
{
ReportService.ListensResponse();
}
}
catch { }
});
}
public Task StartAsync(CancellationToken cancellationToken)
{
object _state = System.Diagnostics.Debugger.IsAttached ? null : this;
_timer = new Timer(TimerExecute, _state, 1000, 2000);
_timerMonitor = new Timer(state =>
{
try
{
if (PS.Config.ErpElement.IsCalcRealtimeWarning)
{
WarningService.QuotaMonitorMsgQuery();
}
}
catch
{
}
}, _state, 100000, 100000);
Modules.DataCacheModule.DataCacheManager.StartUpdate();
// 项目启动时初始化项目YLErpWebUrl地址配置
hostApplicationLifetime.ApplicationStarted.Register(
() =>
{
var address = server.Features.Get<IServerAddressesFeature>().Addresses?.FirstOrDefault()?.Replace("*", "localhost");
if (string.IsNullOrEmpty(address))
{
return;
}
// 更新YLErpWebUrl配置
var count = new AppConfigService(OptUserInfo.SystemUser).SaveYLWebApiConfig(address);
if (count > 0)
{
PS.SetConfig(ConsAppConfig.YLErpWebUrlConfig, address);
}
// 预热风控引擎:加载规则与应用到内存,并预编译所有规则
Task.Run(() =>
{
try
{
RiskEngineService.GetInstance().Preload();
}
catch (Exception ex)
{
LogFactory.GetLogger("HostedTaskService").Error("[风控引擎] 启动预热失败", ex);
}
});
});
return Task.CompletedTask;
}
public Task StopAsync(CancellationToken cancellationToken)
{
if (_timer != null)
{
_timer.Dispose();
_timer = null;
}
if (_timerMonitor != null)
{
_timerMonitor.Dispose();
_timerMonitor = null;
}
return Task.CompletedTask;
}
}
}