コード例 #1
0
        /// <summary>
        /// Checks if any of the hosted identities expired.
        /// If so, it deletes them.
        /// </summary>
        public async Task CheckExpiredHostedIdentitiesAsync()
        {
            log.Trace("()");

            DateTime      now            = DateTime.UtcNow;
            List <byte[]> imagesToDelete = new List <byte[]>();

            using (UnitOfWork unitOfWork = new UnitOfWork())
            {
                // Disable change tracking for faster multiple deletes.
                unitOfWork.Context.ChangeTracker.AutoDetectChangesEnabled = false;

                DatabaseLock lockObject = UnitOfWork.HostedIdentityLock;
                await unitOfWork.AcquireLockAsync(lockObject);

                try
                {
                    List <HostedIdentity> expiredIdentities = (await unitOfWork.HostedIdentityRepository.GetAsync(i => i.ExpirationDate < now, null, true)).ToList();
                    if (expiredIdentities.Count > 0)
                    {
                        log.Debug("There are {0} expired hosted identities.", expiredIdentities.Count);
                        foreach (HostedIdentity identity in expiredIdentities)
                        {
                            if (identity.ProfileImage != null)
                            {
                                imagesToDelete.Add(identity.ProfileImage);
                            }
                            if (identity.ThumbnailImage != null)
                            {
                                imagesToDelete.Add(identity.ThumbnailImage);
                            }

                            unitOfWork.HostedIdentityRepository.Delete(identity);
                            log.Debug("Identity ID '{0}' expired and will be deleted.", identity.IdentityId.ToHex());
                        }

                        await unitOfWork.SaveThrowAsync();

                        log.Debug("{0} expired hosted identities were deleted.", expiredIdentities.Count);
                    }
                    else
                    {
                        log.Debug("No expired hosted identities found.");
                    }
                }
                catch (Exception e)
                {
                    log.Error("Exception occurred: {0}", e.ToString());
                }

                unitOfWork.ReleaseLock(lockObject);
            }


            if (imagesToDelete.Count > 0)
            {
                ImageManager imageManager = (ImageManager)Base.ComponentDictionary[ImageManager.ComponentName];

                foreach (byte[] hash in imagesToDelete)
                {
                    imageManager.RemoveImageReference(hash);
                }
            }


            log.Trace("(-)");
        }
コード例 #2
0
        /// <summary>
        /// Checks if any of the neighbors expired.
        /// If so, it starts the process of their removal.
        /// </summary>
        public async Task CheckExpiredNeighborsAsync()
        {
            log.Trace("()");

            // If a neighbor server's LastRefreshTime is lower than this limit, it is expired.
            DateTime limitLastRefreshTime = DateTime.UtcNow.AddSeconds(-Config.Configuration.NeighborProfilesExpirationTimeSeconds);

            using (UnitOfWork unitOfWork = new UnitOfWork())
            {
                bool           success     = false;
                DatabaseLock[] lockObjects = new DatabaseLock[] { UnitOfWork.NeighborLock, UnitOfWork.NeighborhoodActionLock };
                using (IDbContextTransaction transaction = await unitOfWork.BeginTransactionWithLockAsync(lockObjects))
                {
                    try
                    {
                        List <Neighbor> expiredNeighbors = (await unitOfWork.NeighborRepository.GetAsync(n => n.LastRefreshTime < limitLastRefreshTime, null, true)).ToList();
                        if (expiredNeighbors.Count > 0)
                        {
                            log.Debug("There are {0} expired neighbors.", expiredNeighbors.Count);
                            foreach (Neighbor neighbor in expiredNeighbors)
                            {
                                // This action will cause our profile server to erase all profiles of the neighbor that has been removed.
                                NeighborhoodAction action = new NeighborhoodAction()
                                {
                                    ServerId         = neighbor.NeighborId,
                                    Timestamp        = DateTime.UtcNow,
                                    Type             = NeighborhoodActionType.RemoveNeighbor,
                                    TargetIdentityId = null,
                                    AdditionalData   = null
                                };
                                await unitOfWork.NeighborhoodActionRepository.InsertAsync(action);
                            }

                            await unitOfWork.SaveThrowAsync();

                            transaction.Commit();
                        }
                        else
                        {
                            log.Debug("No expired neighbors found.");
                        }

                        success = true;
                    }
                    catch (Exception e)
                    {
                        log.Error("Exception occurred: {0}", e.ToString());
                    }

                    if (!success)
                    {
                        log.Warn("Rolling back transaction.");
                        unitOfWork.SafeTransactionRollback(transaction);
                    }

                    unitOfWork.ReleaseLock(lockObjects);
                }
            }

            log.Trace("(-)");
        }
コード例 #3
0
        /// <summary>
        /// Removes neighborhood actions whose target servers do not exist in our database.
        /// </summary>
        /// <returns>true if the function succeeds, false otherwise.</returns>
        private bool DeleteInvalidNeighborhoodActions()
        {
            log.Info("()");

            bool res = false;

            using (UnitOfWork unitOfWork = new UnitOfWork())
            {
                DatabaseLock[] lockObjects = new DatabaseLock[] { UnitOfWork.NeighborLock, UnitOfWork.FollowerLock, UnitOfWork.NeighborhoodActionLock };
                unitOfWork.AcquireLock(lockObjects);
                try
                {
                    List <byte[]>    neighborIds        = unitOfWork.NeighborRepository.Get().Select(n => n.NeighborId).ToList();
                    HashSet <byte[]> neighborIdsHashSet = new HashSet <byte[]>(neighborIds, StructuralEqualityComparer <byte[]> .Default);

                    List <byte[]>    followerIds        = unitOfWork.FollowerRepository.Get().Select(f => f.FollowerId).ToList();
                    HashSet <byte[]> followerIdsHashSet = new HashSet <byte[]>(followerIds, StructuralEqualityComparer <byte[]> .Default);

                    List <NeighborhoodAction> actions = unitOfWork.NeighborhoodActionRepository.Get().ToList();
                    bool saveDb = false;
                    foreach (NeighborhoodAction action in actions)
                    {
                        bool actionValid = false;
                        if (action.IsProfileAction())
                        {
                            // Action's serverId should be our follower.
                            actionValid = followerIdsHashSet.Contains(action.ServerId);
                        }
                        else
                        {
                            // Action's serverId should be our neighbor.
                            actionValid = neighborIdsHashSet.Contains(action.ServerId);
                        }

                        if (!actionValid)
                        {
                            log.Debug("Removing invalid action ID {0}, type {1}, server ID '{2}'.", action.Id, action.Type, action.ServerId.ToHex());
                            unitOfWork.NeighborhoodActionRepository.Delete(action);
                            saveDb = true;
                        }
                    }

                    if (saveDb)
                    {
                        res = unitOfWork.Save();
                    }
                    else
                    {
                        log.Debug("No invalid neighborhood actions found.");
                        res = true;
                    }
                }
                catch (Exception e)
                {
                    log.Error("Exception occurred: {0}", e.ToString());
                }

                unitOfWork.ReleaseLock(lockObjects);
            }

            log.Info("(-):{0}", res);
            return(res);
        }