日志修改

parent 525d7c3e
...@@ -257,12 +257,12 @@ public ApiResponse GenerateAsync([CustomizeValidator(RuleSet = "Delete"), FromBo ...@@ -257,12 +257,12 @@ public ApiResponse GenerateAsync([CustomizeValidator(RuleSet = "Delete"), FromBo
_logManageService.WriteMsg("生成绩效准备中", $"准备生成{allot.Year}-{allot.Month.ToString().PadLeft(2, '0')}月份绩效,请稍等!", 1, allot.ID, "ReceiveMessage", true); _logManageService.WriteMsg("生成绩效准备中", $"准备生成{allot.Year}-{allot.Month.ToString().PadLeft(2, '0')}月份绩效,请稍等!", 1, allot.ID, "ReceiveMessage", true);
_allotService.UpdateAllotStates(allot.ID, (int)AllotStates.Wait, EnumHelper.GetDescription(AllotStates.Wait), allot.Generate); _allotService.UpdateAllotStates(allot.ID, (int)AllotStates.Wait, EnumHelper.GetDescription(AllotStates.Wait), allot.Generate);
if (_evn.IsEnvironment("Localhost")) //if (_evn.IsEnvironment("Localhost"))
{ //{
_allotService.Generate(allot); // _allotService.Generate(allot);
} //}
else //else
{ //{
_backgroundTaskQueue.QueueBackgroundWorkItem(async token => _backgroundTaskQueue.QueueBackgroundWorkItem(async token =>
{ {
using (var scope = _serviceScopeFactory.CreateScope()) using (var scope = _serviceScopeFactory.CreateScope())
...@@ -274,7 +274,7 @@ public ApiResponse GenerateAsync([CustomizeValidator(RuleSet = "Delete"), FromBo ...@@ -274,7 +274,7 @@ public ApiResponse GenerateAsync([CustomizeValidator(RuleSet = "Delete"), FromBo
}); });
//_publishEndpoint.Publish(allot).Wait(); //_publishEndpoint.Publish(allot).Wait();
} //}
_logManageService.WriteMsg("等待绩效生成", $"等待绩效生成{allot.Year}-{allot.Month.ToString().PadLeft(2, '0')}月份绩效!", 1, allot.ID, "ReceiveMessage"); _logManageService.WriteMsg("等待绩效生成", $"等待绩效生成{allot.Year}-{allot.Month.ToString().PadLeft(2, '0')}月份绩效!", 1, allot.ID, "ReceiveMessage");
//_allotService.Generate(allot, email); //_allotService.Generate(allot, email);
......
...@@ -3,70 +3,74 @@ ...@@ -3,70 +3,74 @@
using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using System; using System;
using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace Performance.Services namespace Performance.Services
{ {
public class HubGroupInfo
{
public DateTime AddTime { get; set; }
public string ConnectionId { get; set; }
}
public class AllotLogHub : Hub public class AllotLogHub : Hub
{ {
private readonly ILogger<AllotLogHub> logger; private readonly ILogger<AllotLogHub> logger;
private readonly IMemoryCache cache; private static ConcurrentDictionary<int, List<ConnectionUser>> _pairs
= new ConcurrentDictionary<int, List<ConnectionUser>>();
public AllotLogHub( public AllotLogHub(ILogger<AllotLogHub> logger)
ILogger<AllotLogHub> logger,
IMemoryCache cache)
{ {
this.logger = logger; this.logger = logger;
this.cache = cache;
} }
public override Task OnConnectedAsync() public override Task OnConnectedAsync()
{ {
logger.LogDebug($"日志推送 连接{Context.ConnectionId}"); logger.LogDebug($"日志推送 连接{Context.ConnectionId}");
return base.OnConnectedAsync(); return base.OnConnectedAsync();
} }
public override Task OnDisconnectedAsync(Exception exception) public override Task OnDisconnectedAsync(Exception exception)
{ {
var connectionId = Context.ConnectionId; var connectionId = Context.ConnectionId;
logger.LogDebug($"日志推送 断开连接{connectionId}"); var userId = Context.User?.Claims.FirstOrDefault(w => w.Type == "id").Value ?? "";
string key = $"AllotLogGroup_{connectionId}"; if (!string.IsNullOrEmpty(userId))
//1 查询用户分组信息
var groupName = "";
//2 删除数据库中用户分组数据
if (cache.TryGetValue(key, out string value))
{ {
cache.Remove(key); foreach (var item in _pairs)
{
var conn = item.Value.FirstOrDefault(w => w.ConnectionId == connectionId);
if (conn != null)
{
Groups.RemoveFromGroupAsync(connectionId, item.Key.ToString()).Wait();
logger.LogDebug($"日志推送 断开连接{connectionId}-{item.Key}-{userId}");
}
}
} }
logger.LogDebug($"日志推送 断开连接{connectionId}-{groupName}");
//3 分组中删除用户
Groups.RemoveFromGroupAsync(connectionId, groupName);
return base.OnDisconnectedAsync(exception); return base.OnDisconnectedAsync(exception);
} }
public async Task AddGroup(string token, int groupName) public async Task AddGroup(string token, int groupName)
{ {
var connectionId = Context.ConnectionId; var connectionId = Context.ConnectionId;
string key = $"AllotLogGroup_{connectionId}";
if (cache.TryGetValue(key, out string value)) var userId = Context.User?.Claims.FirstOrDefault(w => w.Type == "id").Value ?? "";
if (!string.IsNullOrEmpty(userId))
{ {
cache.Remove(key); if (_pairs.ContainsKey(groupName))
_pairs[groupName].Add(new ConnectionUser { ConnectionId = connectionId, UserId = userId });
else
_pairs[groupName] = new List<ConnectionUser> { new ConnectionUser { ConnectionId = connectionId, UserId = userId } };
await Groups.AddToGroupAsync(connectionId, groupName.ToString());
} }
cache.Set(key, groupName, new TimeSpan(1, 0, 0));
logger.LogDebug($"日志推送 添加用户组{connectionId}-{groupName}"); logger.LogDebug($"日志推送 添加用户组{connectionId}-{groupName}-{userId}");
}
//2 将用户插入分组 class ConnectionUser
await Groups.AddToGroupAsync(connectionId, groupName.ToString()); {
} public string ConnectionId { get; set; }
public string UserId { get; set; }
}
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment