手动启动,定时任务反射调用放到本地缓存,优化性能

This commit is contained in:
alex
2023-09-19 15:57:51 +08:00
parent 767076a513
commit f05ece37e5
2 changed files with 12 additions and 17 deletions

View File

@@ -173,7 +173,7 @@ namespace Tnb.WarehouseMgr
public override Task StopAsync(CancellationToken cancellationToken)
{
IsStarted = false;
return Task.FromResult(IsStarted);
return Task.CompletedTask;
//return base.StopAsync(cancellationToken);
}

View File

@@ -146,7 +146,7 @@ public class OAuthService : IDynamicApiController, ITransient
private readonly BackgroundService _backgroundService; //added by ly on 20230916
private static CancellationTokenSource stopTimedTaskSvcCTS = new();
private static Dictionary<string, Func<BackgroundService, object>> _fetchPropValue = new();
private static Dictionary<string, Func<BackgroundService, bool>> _fetchPropValue = new();
/// <summary>
@@ -296,21 +296,16 @@ public class OAuthService : IDynamicApiController, ITransient
//modify by ly on 20230918 登录成功后启动定时服务
if (!userId.IsNullOrWhiteSpace())
{
var isStartedProperty = _backgroundService.GetType().GetProperty("IsStarted");
//if (!_fetchPropValue.TryGetValue("IsStarted", out var action))
//{
// var isStartedProp = _backgroundService.GetType().GetProperty("IsStarted");
// var paramExp = Expression.Parameter(typeof(BackgroundService), "_backgroundSvc");
// var propExp = Expression.Property(paramExp, isStartedProp);
// var body = Expression.Lambda<Func<BackgroundService, object>>(propExp, paramExp);
// action = body.Compile();
// _fetchPropValue["IsStarted"] = action;
//}
//if(!action?.Invoke(_backgroundService)?.ParseToBool() ?? false)
//{
// await _backgroundService.StartAsync(stopTimedTaskSvcCTS.Token);
//}
if (isStartedProperty?.GetValue(_backgroundService) is bool isStarted && !isStarted)
if (!_fetchPropValue.TryGetValue("IsStarted", out var action))
{
var isStartedProp = _backgroundService.GetType().GetProperty("IsStarted");
var paramExp = Expression.Parameter(typeof(BackgroundService), "_backgroundSvc");
var propExp = Expression.Property(Expression.ConvertChecked(paramExp, isStartedProp.DeclaringType), isStartedProp.GetGetMethod());
var body = Expression.Lambda<Func<BackgroundService, Boolean>>(propExp, paramExp);
action = body.Compile();
_fetchPropValue["IsStarted"] = action;
}
if (!action?.Invoke(_backgroundService) ?? false)
{
await _backgroundService.StartAsync(stopTimedTaskSvcCTS.Token);
}