private void AggregateFeeds(object interval) { if (!Monitor.TryEnter(aggregateLock)) { return; } try { var start = DateTime.UtcNow; log.DebugFormat("Start of collecting feeds..."); var unreadUsers = new Dictionary <int, Dictionary <Guid, int> >(); foreach (var module in Modules) { var result = new List <FeedRow>(); var fromTime = FeedAggregateDataProvider.GetLastTimeAggregate(module.GetType().Name); if (fromTime == default(DateTime)) { fromTime = DateTime.UtcNow.Subtract((TimeSpan)interval); } var toTime = DateTime.UtcNow; var tenants = Attempt(10, () => module.GetTenantsWithFeeds(fromTime)).ToList(); log.DebugFormat("Find {1} tenants for module {0}.", module.GetType().Name, tenants.Count()); foreach (var tenant in tenants) { // Warning! There is hack here! // clearing the cache to get the correct acl var cache = AscCache.Memory; cache.Remove("acl" + tenant); cache.Remove("/webitemsecurity/" + tenant); cache.Remove(string.Format("sub/{0}/{1}/{2}", tenant, "6045b68c-2c2e-42db-9e53-c272e814c4ad", NotifyConstants.Event_NewCommentForMessage.ID)); HttpContext.Current = null; try { if (CoreContext.TenantManager.GetTenant(tenant) == null) { continue; } CoreContext.TenantManager.SetCurrentTenant(tenant); var users = CoreContext.UserManager.GetUsers(); // fake httpcontext break configuration manager for mono if (!WorkContext.IsMono) { HttpContext.Current = new HttpContext( new HttpRequest("hack", CommonLinkUtility.GetFullAbsolutePath("/"), string.Empty), new HttpResponse(new StringWriter())); } var feeds = Attempt(10, () => module.GetFeeds(new FeedFilter(fromTime, toTime) { Tenant = tenant }).Where(r => r.Item1 != null).ToList()); log.DebugFormat("{0} feeds in {1} tenant.", feeds.Count, tenant); var tenant1 = tenant; var module1 = module; var feedsRow = feeds .Select(tuple => new Tuple <FeedRow, object>(new FeedRow(tuple.Item1) { Tenant = tenant1, ProductId = module1.Product }, tuple.Item2)) .ToList(); foreach (var u in users) { if (isStopped) { return; } if (!TryAuthenticate(u.ID)) { continue; } module.VisibleFor(feedsRow, u.ID); } result.AddRange(feedsRow.Select(r => r.Item1)); } catch (Exception ex) { log.ErrorFormat("Tenant: {0}, {1}", tenant, ex); } finally { // fake httpcontext break configuration manager for mono if (!WorkContext.IsMono) { if (HttpContext.Current != null) { new DisposableHttpContext(HttpContext.Current).Dispose(); HttpContext.Current = null; } } } } FeedAggregateDataProvider.SaveFeeds(result, module.GetType().Name, toTime); foreach (var res in result) { foreach (var userGuid in res.Users.Where(userGuid => !userGuid.Equals(res.ModifiedById))) { Dictionary <Guid, int> dictionary; if (!unreadUsers.TryGetValue(res.Tenant, out dictionary)) { dictionary = new Dictionary <Guid, int>(); } if (dictionary.ContainsKey(userGuid)) { ++dictionary[userGuid]; } else { dictionary.Add(userGuid, 1); } unreadUsers[res.Tenant] = dictionary; } } } signalrServiceClient.SendUnreadUsers(unreadUsers); log.DebugFormat("Time of collecting news: {0}", DateTime.UtcNow - start); } catch (Exception ex) { log.Error(ex); } finally { Monitor.Exit(aggregateLock); } }
private void AggregateFeeds(object interval) { if (!Monitor.TryEnter(aggregateLock)) { return; } try { var start = DateTime.UtcNow; log.DebugFormat("Start of collecting feeds..."); foreach (var module in Modules) { var result = new List <FeedRow>(); var fromTime = FeedAggregateDataProvider.GetLastTimeAggregate(module.GetType().Name); if (fromTime == default(DateTime)) { fromTime = DateTime.UtcNow.Subtract((TimeSpan)interval); } var toTime = DateTime.UtcNow; var tenants = Attempt(10, () => module.GetTenantsWithFeeds(fromTime)).ToList(); log.DebugFormat("Find {1} tenants for module {0}.", module.GetType().Name, tenants.Count()); foreach (var tenant in tenants) { // Warning! There is hack here! // clearing the cache to get the correct acl var cache = AscCache.Default; cache.Remove("acl" + tenant); cache.Remove("/webitemsecurity/" + tenant); cache.Remove(string.Format("sub/{0}/{1}/{2}", tenant, "6045b68c-2c2e-42db-9e53-c272e814c4ad", NotifyConstants.Event_NewCommentForMessage.ID)); HttpContext.Current = null; try { if (CoreContext.TenantManager.GetTenant(tenant) == null) { continue; } CoreContext.TenantManager.SetCurrentTenant(tenant); // fake httpcontext break configuration manager for mono if (!WorkContext.IsMono) { HttpContext.Current = new HttpContext( new HttpRequest("hack", CommonLinkUtility.GetFullAbsolutePath("/"), string.Empty), new HttpResponse(new StringWriter())); } var feeds = Attempt(10, () => module.GetFeeds(new FeedFilter(fromTime, toTime) { Tenant = tenant })); log.DebugFormat("{0} feeds in {1} tenant.", feeds.Count(), tenant); foreach (var tuple in feeds) { if (tuple.Item1 == null) { continue; } var r = new FeedRow { Id = tuple.Item1.Id, ClearRightsBeforeInsert = tuple.Item1.Variate, Tenant = tenant, ProductId = module.Product, ModuleId = tuple.Item1.Module, AuthorId = tuple.Item1.LastModifiedBy, CreatedDate = tuple.Item1.Date, GroupId = tuple.Item1.GroupId, Json = JsonConvert.SerializeObject(tuple.Item1, new JsonSerializerSettings { DateTimeZoneHandling = DateTimeZoneHandling.Utc }), Keywords = tuple.Item1.Keywords }; foreach (var u in CoreContext.UserManager.GetUsers()) { if (isStopped) { return; } if (TryAuthenticate(u.ID) && module.VisibleFor(tuple.Item1, tuple.Item2, u.ID)) { r.Users.Add(u.ID); } } result.Add(r); } } catch (Exception ex) { log.ErrorFormat("Tenant: {0}, {1}", tenant, ex); } finally { // fake httpcontext break configuration manager for mono if (!WorkContext.IsMono) { if (HttpContext.Current != null) { new DisposableHttpContext(HttpContext.Current).Dispose(); HttpContext.Current = null; } } } } FeedAggregateDataProvider.SaveFeeds(result, module.GetType().Name, toTime); } log.DebugFormat("Time of collecting news: {0}", DateTime.UtcNow - start); } catch (Exception ex) { log.Error(ex); } finally { Monitor.Exit(aggregateLock); } }