/// <summary> /// 根据在线用户的静态变量 /// 将在线用户和userId缓存 /// signal-userId /// </summary> private void CreateSignalIdUserIdCache() { LogWrite.WriteLogInfo("--------------创建online-user缓存--------"); var onlineList = GetOnlineCache(); if (onlineList == null || onlineList.Count <= 0) { return; } foreach (var online in onlineList) { string userId = GetUserIdByRedis(online); var contains = _cache.Contains(online); if (contains) { _cache.Remove(online); } if (!string.IsNullOrWhiteSpace(userId)) { _cache.Set(online, userId, TimeSpan.FromDays(1)); } } }
//自选股发生改变时 public void ChangeOwnCodeList(string sessionId) { var userId = GetUserIdByRedis(sessionId); if (string.IsNullOrWhiteSpace(userId)) { LogWrite.WriteLogInfo($"---------未能在sessionRedis中获取到用户的UserId,对应的sessionId为{sessionId}------"); return; } List <string> codeList = new List <string>(); //得到 该用户 所有创建的组合 List <string> combinationGuidList = _dao.GetCombinationByCreateId(Convert.ToInt32(userId)); foreach (var combinationGuid in combinationGuidList) { //得到 该组合的 股票代码List var stockCodeList = _fundDao.GetStockCodeByGuidList(combinationGuid); codeList.AddRange(stockCodeList); } var contains = _cache.Contains(userId); if (contains) { _cache.Remove(userId); } _cache.Set(userId, codeList, TimeSpan.FromDays(365)); }
//在线用户发生改变时 public void ChangeOnline() { LogWrite.WriteLogInfo("---在线用户发生了变化,更新online-user缓存,更新signal-codeList缓存Start---"); CreateSignalIdUserIdCache(); CreateSignalCodeListCache(); LogWrite.WriteLogInfo("---在线用户发生了变化,更新signal-userId缓存,更新signal-codeList缓存end---"); }
/// <summary> /// 这个方法执行时间很长 /// 将mysql中 user和codeList的关系 存入了缓存 /// user-codeList /// </summary> public void CreateUserIdCodeListCache() { LogWrite.WriteLogInfo("-----初始化mysql数据库中的user-codeList缓存-----------"); //所有去重过的用户 var allCreateUserId = _dao.GetAllCreateUserId(); foreach (var userId in allCreateUserId) { List <string> codeList = new List <string>(); //得到 该用户 所有创建的组合 List <string> combinationGuidList = _dao.GetCombinationByCreateId(userId); foreach (var combinationGuid in combinationGuidList) { //得到 该组合的 股票代码List var stockCodeList = _fundDao.GetStockCodeByGuidList(combinationGuid); codeList.AddRange(stockCodeList); } var contains = _cache.Contains(userId.ToString()); if (contains) { _cache.Remove(userId.ToString()); } _cache.Set(userId.ToString(), codeList, TimeSpan.FromDays(365)); } LogWrite.WriteLogInfo("最长缓存结束了,已经将所有的userId-codeList关联存入缓存"); }
//初始化涨跌幅 public void InitRange() { LogWrite.WriteLogInfo($"------------用户请求初始化涨跌幅---------------"); var chgModel = MongoService.GetChgData(); Clients.Caller.upordown(chgModel); }
//用户改变自选股时,管理内存 public void ReloadOwnStockCode(string sessionId) { Console.WriteLine("自选股发生改变,用户" + sessionId); LogWrite.WriteLogInfo($"-------------------------用户修改自选股----------------------"); LogWrite.WriteLogInfo($"---------------------用户:{sessionId}修改了自选股------------"); _service.ChangeOwnCodeList(sessionId); _service.ChangeOnline(); }
public Task Execute(IJobExecutionContext context) { var isMarketDay = InitService.IsMarketDay(DateTime.Now.Date); if (!isMarketDay) { Console.WriteLine("非交易日,不进行自选股推送"); LogWrite.WriteLogInfo("非交易日,不进行自选股推送"); return(Task.FromResult(0)); } Console.WriteLine("首页自选股任务调度"); LogWrite.WriteLogInfo($"------------首页自选股任务调度,时间:{DateTime.Now.ToLongTimeString()}---------------"); /* * 新的思路 * 循环在线用户 * 通过signal-codeList缓存 * 找到每个用户需要推送的codeList * 在循环codeList 构建要推送的新消息 得到一个List 这应该是放在另一个方法中 * foreach(online){c.user(online).alert(codeList)} */ var onlineCache = InitService.GetOnlineCache(); if (onlineCache == null || onlineCache.Count <= 0) { return(Task.FromResult(0)); } foreach (var online in onlineCache) { List <string> codeList = InitService.GetOnlineCodeList(online); if (codeList == null || codeList.Count <= 0) { continue; } List <HashPortfolio> signalList = MongoService.GetPortfolioList(codeList); if (signalList == null || signalList.Count <= 0) { continue; } LogWrite.WriteLogInfo($"------------首页自选股推送了Start,时间:{DateTime.Now.ToLongTimeString()}---------------"); GlobalHost.ConnectionManager.GetHubContext <SignalHub>().Clients .User(online) .home_follow(signalList); LogWrite.WriteLogInfo($"接收首页自选股的用户:{online}"); LogWrite.WriteLogInfo($"接收首页自选股的信号数量:{signalList.Count}"); //详细日志,当没有问题是,注释掉下面的for循环提高性能 foreach (var hashPortfolio in signalList) { LogWrite.WriteLogInfo($"接收首页自选股的信号代码:{hashPortfolio.StockCode}"); } LogWrite.WriteLogInfo($"------------首页自选股推送了End,时间:{DateTime.Now.ToLongTimeString()}---------------"); } return(Task.FromResult(0)); }
//初始化首页自选股信息 public void InitPortfolio(string sessionId) { LogWrite.WriteLogInfo($"------------用户:{sessionId}请求初始化首页自选股---------------"); //获得当前用户所关注的code var onlineCodeList = _service.GetOnlineCodeList(sessionId); //获得当前用户的自选股信息 List <HashPortfolio> signalList = MongoService.GetPortfolioList(onlineCodeList); Clients.Caller.home_follow(signalList); }
/// <summary> /// 每次重启时,清空队列 /// </summary> public static void DeleteQueue() { using (IConnection conn = RabbitMqFactory.CreateConnection()) { using (IModel channel = conn.CreateModel()) { try { channel.QueueDelete(QueueName, true, false); } catch (Exception e) { LogWrite.WriteLogInfo($"重启时,存在消费者,不删除队列,异常{e.Message}"); } } } }
public Task Execute(IJobExecutionContext context) { var isMarketDay = InitService.IsMarketDay(DateTime.Now.Date); if (!isMarketDay) { Console.WriteLine("非交易日,不进行涨跌幅推送"); LogWrite.WriteLogInfo("非交易日,不进行涨跌幅推送"); return(Task.FromResult(0)); } Console.WriteLine("涨跌幅任务调度"); LogWrite.WriteLogInfo($"------------涨跌幅任务调度,时间:{DateTime.Now.ToLongTimeString()}---------------"); var chgModel = Service.GetChgData(); GlobalHost.ConnectionManager.GetHubContext <SignalHub>().Clients .All .upordown(chgModel); return(Task.FromResult(0)); }
/// <summary> /// 重连时 /// </summary> /// <returns></returns> public override Task OnReconnected() { var signalRid = GetSignalRid(); Console.WriteLine($"session_id{signalRid}重连"); LogWrite.WriteLogInfo($"-------------------------重新链接----------------------"); LogWrite.WriteLogInfo($"----sessionId:{signalRid}与TrumguSignalR重新链接-------"); try { _service.AddOnlineCache(signalRid); _service.ChangeOnline(); } catch (Exception e) { Console.WriteLine(e); LogWrite.WriteLogError(e); } return(base.OnReconnected()); }
/// <summary> /// 断开连接时 /// </summary> /// <param name="stopCalled"></param> /// <returns></returns> public override Task OnDisconnected(bool stopCalled) { var signalRid = GetSignalRid(); Console.WriteLine($"session_id{signalRid}断开连接"); LogWrite.WriteLogInfo($"-------------------------断开链接----------------------"); LogWrite.WriteLogInfo($"----sessionId:{signalRid}与TrumguSignalR断开链接-------"); try { _service.DelOnlineCache(signalRid); _service.ChangeOnline(); } catch (Exception e) { Console.WriteLine(e); LogWrite.WriteLogError(e); } return(base.OnDisconnected(stopCalled)); }
public string GetUserIdByRedis(string sessionId) { LogWrite.WriteLogInfo("--------------从sessionRedis中得到userID--------"); if (_sessionCache.Contains(sessionId)) { var sessionKey = sessionId.Contains("WX_") ? "user_info" : "UserInfo"; t_bf_sys_userObj userInfo = _sessionCache.HashGet <t_bf_sys_userObj>(sessionId, sessionKey); if (userInfo == null) { LogWrite.WriteLogError(new ApplicationException($"sessionRedis没有成功解析{sessionId}的用户信息")); return(null); } string userId = userInfo.id.ToString(); LogWrite.WriteLogInfo($"从sessionRedis获取到了{sessionId}的userId:{userId}"); return(userId); } LogWrite.WriteLogError(new ApplicationException($"sessionRedis中没有找到key为:{sessionId}的用户信息缓存")); return(null); }
/// <summary> /// 构建用来推送的缓存 /// push+signal-codeList /// </summary> private void CreateSignalCodeListCache() { LogWrite.WriteLogInfo("--------------创建推送缓存 push_online-codeList缓存--------"); var onlineList = GetOnlineCache(); if (onlineList == null || onlineList.Count <= 0) { return; } foreach (var online in onlineList) { if (!_cache.Contains(online)) { LogWrite.WriteLogError(new ApplicationException($"构建推送缓存时,缓存中不存在当前session:{online}为key的 online-userId缓存")); return; } var userId = _cache.Get <string>(online); if (!_cache.Contains(userId)) { LogWrite.WriteLogError(new Exception($"构建推送缓存时,缓存中不存在用户userid{userId}为key的 user-codeList缓存")); return; } var codeList = _cache.Get <List <string> >(userId); //online的key已经存在,所以我要加个前缀 string key = $"push{online}"; var contains = _cache.Contains(key); if (contains) { _cache.Remove(key); } if (codeList != null && codeList.Count > 0) { _cache.Set(key, codeList, TimeSpan.FromDays(1)); } } }
public Task Execute(IJobExecutionContext context) { var isMarketDay = InitService.IsMarketDay(DateTime.Now.Date); if (!isMarketDay) { Console.WriteLine("非交易日,不进行微信推送"); LogWrite.WriteLogInfo("非交易日,不进行微信推送"); return(Task.FromResult(0)); } Console.WriteLine("微信推送"); #region 正式数据 /* * 思路 * 首先 扫库 这应该放到其他类中 * 循环在线用户 * 对比扫库中的List和在线用户对应的codeList * 取交集推送 */ DateTime time2 = DateTime.Now.AddHours(8); var signalNowList = MongoService.GetStockSignalSingleByTime(_time1, time2); if (signalNowList != null && signalNowList.Count > 0) { foreach (var single in signalNowList) { single.time = single.time.AddHours(-8); } Console.WriteLine($"时间:{signalNowList[0].time}"); DirectExchangeSendMsg(signalNowList); } _time1 = time2; #endregion return(Task.FromResult(0)); }
public static void Main(string[] args) { //删除rabbitMQ积压的数据 WxPopJob.DeleteQueue(); Console.WriteLine("删除rabbitMQ积压的数据"); InitService initService = new InitService(); //移除在线用户 initService.InitOnline(); //重启时,更新所有 userid-codeList的缓存 initService.CreateUserIdCodeListCache(); Console.WriteLine("更新所有 userid-codeList的缓存---完毕"); Console.WriteLine("主进程开始.."); LogWrite.WriteLogInfo("主进程开始"); var url = Config.GetValue("SignalRUrl"); LogWrite.WriteLogInfo($"服务地址{url}"); Console.WriteLine($"服务地址{url}"); using (WebApp.Start(url)) { try { LogWrite.WriteLogInfo("服务端口开启成功"); Console.WriteLine("服务端口开启成功"); QuartzJob(); } catch (Exception e) { Console.WriteLine(e); LogWrite.WriteLogError(e); } finally { Console.ReadKey(); } } }
//测试数据方法 public Task Execute2(IJobExecutionContext context) { Console.WriteLine("PopSend任务调度"); LogWrite.WriteLogInfo("PopSend任务调度"); #region 测试 var stockSignalSingles = MongoService.GetAllStockSignalSingle(); var model1 = stockSignalSingles.FindLast(m => m.code == "000001"); var model2 = stockSignalSingles.FindLast(m => m.code == "600000"); var model3 = stockSignalSingles.FindLast(m => m.code == "000063"); var model4 = stockSignalSingles.FindLast(m => m.code == "603999"); var model5 = stockSignalSingles.FindLast(m => m.code == "603998"); List <stock_signal_single> list = new List <stock_signal_single> { model1, model2, model3, model4, model5 }; foreach (var online in InitService.GetOnlineCache()) { var memoryCacheManager = new MemoryCacheManager(); var cacheUserId = memoryCacheManager.Get <string>(online); Console.WriteLine($"signal:{online}-userId:{cacheUserId}"); var cacheCodeList = memoryCacheManager.Get <List <string> >(cacheUserId); foreach (var s in cacheCodeList) { Console.WriteLine($"user:{cacheUserId}-codeList{s}"); } var cacheCodeList1 = memoryCacheManager.Get <List <string> >($"push{online}"); Console.WriteLine($"signal:push{online}-codeList{cacheCodeList1.Count}"); var codeList = InitService.GetOnlineCodeList(online); //signalNowList和codeList取交集 var nowCodeList = list.Select(m => m.code).ToList(); //得到了交集 if (codeList == null || codeList.Count <= 0 || nowCodeList.Count <= 0) { continue; } var intersect = codeList.Intersect(nowCodeList).ToList(); if (intersect.Count <= 0) { continue; } //构建推送架构 List <HashPop> hashPops = MongoService.CreatePopList(list, intersect); if (hashPops == null || hashPops.Count <= 0) { continue; } Console.WriteLine($"信号推送:用户{online},信号{hashPops.Count}"); // LogWrite.WriteLogInfo("---------信号推送start-------------"); // LogWrite.WriteLogInfo($"用户:{online}"); // foreach (var pop in hashPops) // { // LogWrite.WriteLogInfo($"信号代码:{pop.Code}"); // } // LogWrite.WriteLogInfo("---------信号推送end-------------"); GlobalHost.ConnectionManager.GetHubContext <SignalHub>().Clients .User(online) .alert_signal(hashPops); } #endregion return(Task.FromResult(0)); }
public Task Execute(IJobExecutionContext context) { var isMarketDay = InitService.IsMarketDay(DateTime.Now.Date); if (!isMarketDay) { Console.WriteLine("非交易日,不进行信号推送"); LogWrite.WriteLogInfo("非交易日,不进行信号推送"); return(Task.FromResult(0)); } Console.WriteLine("右下角推送任务"); LogWrite.WriteLogInfo($"------------右下角推送任务,时间:{DateTime.Now.ToLongTimeString()}---------------"); /* * 思路 * 首先 扫库 这应该放到其他类中 * 循环在线用户 * 对比扫库中的List和在线用户对应的codeList * 取交集推送 * * 修改思路 不用count值作为推送条件 用时间作为推送筛选条件 */ DateTime time2 = DateTime.Now.AddHours(8); LogWrite.WriteLogInfo($"time1:{_time1}"); LogWrite.WriteLogInfo($"time2:{time2}"); var signalNowList = MongoService.GetStockSignalSingleByTime(_time1, time2); if (signalNowList != null && signalNowList.Count > 0) { #region debug代码,影响应能 foreach (var stockSignalSingle in signalNowList) { LogWrite.WriteLogInfo($"signalNowList-mongodb中新增的 未经用户过滤的信号:{stockSignalSingle.code}"); } #endregion var onlineCache = InitService.GetOnlineCache(); if (onlineCache == null || onlineCache.Count <= 0) { return(Task.FromResult(0)); } LogWrite.WriteLogInfo($"InitService.OnlineList-在线用户列表:{onlineCache.Count}"); foreach (var online in onlineCache) { LogWrite.WriteLogInfo($"online-循环的在线用户:{online}"); var codeList = InitService.GetOnlineCodeList(online); LogWrite.WriteLogInfo($"codeList-用户关注的信号个数:{codeList.Count}"); #region debug代码 影响性能 foreach (var code in codeList) { LogWrite.WriteLogInfo($"当前用户:{online}关注的信号code:{code}"); } #endregion //signalNowList和codeList取交集 var nowCodeList = signalNowList.Select(m => m.code).ToList(); //得到了交集 if (codeList.Count <= 0 || nowCodeList.Count <= 0) { continue; } var intersect = codeList.Intersect(nowCodeList).ToList(); #region debug代码 影响性能 交集信号 foreach (var code in intersect) { LogWrite.WriteLogInfo($"交集信号有{code}"); } #endregion if (intersect.Count <= 0) { continue; } //构建推送架构 List <HashPop> hashPops = MongoService.CreatePopList(signalNowList, intersect); LogWrite.WriteLogInfo($"hashPops-构建好的准备推送信号个数:{nowCodeList.Count}"); if (hashPops == null || hashPops.Count <= 0) { continue; } Console.WriteLine($"信号推送:用户{online},信号{hashPops.Count}"); LogWrite.WriteLogInfo("---------信号推送start-------------"); LogWrite.WriteLogInfo($"用户:{online}"); foreach (var pop in hashPops) { LogWrite.WriteLogInfo($"信号代码:{pop.Code}"); } LogWrite.WriteLogInfo("---------信号推送end-------------"); GlobalHost.ConnectionManager.GetHubContext <SignalHub>().Clients .User(online) .alert_signal(hashPops); } } _time1 = time2; return(Task.FromResult(0)); }