Esempio n. 1
0
        /// <summary>
        /// Checks if any of the follower servers need refresh.
        /// If so, a neighborhood action is created.
        /// </summary>
        public async Task CheckFollowersRefreshAsync()
        {
            log.Trace("()");

            // If a follower server's LastRefreshTime is lower than this limit, it should be refreshed.
            DateTime limitLastRefreshTime = DateTime.UtcNow.AddSeconds(-Config.Configuration.FollowerRefreshTimeSeconds);

            using (UnitOfWork unitOfWork = new UnitOfWork())
            {
                DatabaseLock[] lockObjects = new DatabaseLock[] { UnitOfWork.FollowerLock, UnitOfWork.NeighborhoodActionLock };
                await unitOfWork.AcquireLockAsync(lockObjects);

                try
                {
                    List <Follower> followersToRefresh = (await unitOfWork.FollowerRepository.GetAsync(f => f.LastRefreshTime < limitLastRefreshTime, null, true)).ToList();
                    if (followersToRefresh.Count > 0)
                    {
                        log.Debug("There are {0} followers that need refresh.", followersToRefresh.Count);
                        foreach (Follower follower in followersToRefresh)
                        {
                            NeighborhoodAction action = new NeighborhoodAction()
                            {
                                ServerId         = follower.FollowerId,
                                Type             = NeighborhoodActionType.RefreshProfiles,
                                Timestamp        = DateTime.UtcNow,
                                ExecuteAfter     = DateTime.UtcNow,
                                TargetIdentityId = null,
                                AdditionalData   = null
                            };

                            await unitOfWork.NeighborhoodActionRepository.InsertAsync(action);

                            log.Debug("Refresh neighborhood action for follower ID '{0}' will be inserted to the database.", follower.FollowerId.ToHex());
                        }

                        await unitOfWork.SaveThrowAsync();

                        log.Debug("{0} new neighborhood actions saved to the database.", followersToRefresh.Count);
                    }
                    else
                    {
                        log.Debug("No followers need refresh now.");
                    }
                }
                catch (Exception e)
                {
                    log.Error("Exception occurred: {0}", e.ToString());
                }

                unitOfWork.ReleaseLock(lockObjects);
            }

            log.Trace("(-)");
        }
Esempio n. 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("(-)");
        }
Esempio n. 3
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("(-)");
        }
Esempio n. 4
0
        /// <summary>
        /// Checks if any of the follower servers need refresh. If so, a neighborhood action is created.
        /// <para>This function also checks if there are unprocessed refresh neighborhood actions
        /// and if there are 3 such requests already, the follower is deleted as it is considered as unresponsive for too long.</para>
        /// </summary>
        public async Task CheckFollowersRefreshAsync()
        {
            log.Trace("()");

            // If a follower server's LastRefreshTime is lower than this limit, it should be refreshed.
            DateTime limitLastRefreshTime = DateTime.UtcNow.AddSeconds(-Config.Configuration.FollowerRefreshTimeSeconds);

            List <byte[]> followersToDeleteIds = new List <byte[]>();

            using (UnitOfWork unitOfWork = new UnitOfWork())
            {
                DatabaseLock[] lockObjects = new DatabaseLock[] { UnitOfWork.FollowerLock, UnitOfWork.NeighborhoodActionLock };
                await unitOfWork.AcquireLockAsync(lockObjects);

                try
                {
                    List <Follower> followersToRefresh = (await unitOfWork.FollowerRepository.GetAsync(f => f.LastRefreshTime < limitLastRefreshTime, null, true)).ToList();
                    if (followersToRefresh.Count > 0)
                    {
                        bool saveDb          = false;
                        int  actionsInserted = 0;

                        log.Debug("There are {0} followers that need refresh.", followersToRefresh.Count);
                        foreach (Follower follower in followersToRefresh)
                        {
                            int unprocessedRefreshProfileActions = await unitOfWork.NeighborhoodActionRepository.CountAsync(a => (a.ServerId == follower.NetworkId) && (a.Type == NeighborhoodActionType.RefreshNeighborStatus));

                            if (unprocessedRefreshProfileActions < 3)
                            {
                                NeighborhoodAction action = new NeighborhoodAction()
                                {
                                    ServerId         = follower.NetworkId,
                                    Type             = NeighborhoodActionType.RefreshNeighborStatus,
                                    Timestamp        = DateTime.UtcNow,
                                    ExecuteAfter     = DateTime.UtcNow,
                                    TargetIdentityId = null,
                                    AdditionalData   = null
                                };

                                await unitOfWork.NeighborhoodActionRepository.InsertAsync(action);

                                log.Debug("Refresh neighborhood action for follower ID '{0}' will be inserted to the database.", follower.NetworkId.ToHex());
                                saveDb = true;
                                actionsInserted++;
                            }
                            else
                            {
                                log.Debug("There are {0} unprocessed RefreshNeighborStatus neighborhood actions for follower ID '{1}'. Follower will be deleted.", unprocessedRefreshProfileActions, follower.NetworkId.ToHex());
                                followersToDeleteIds.Add(follower.NetworkId);
                            }
                        }

                        if (saveDb)
                        {
                            await unitOfWork.SaveThrowAsync();

                            log.Debug("{0} new neighborhood actions saved to the database.", actionsInserted);
                        }
                    }
                    else
                    {
                        log.Debug("No followers need refresh now.");
                    }
                }
                catch (Exception e)
                {
                    log.Error("Exception occurred: {0}", e.ToString());
                }

                unitOfWork.ReleaseLock(lockObjects);


                if (followersToDeleteIds.Count > 0)
                {
                    log.Debug("There are {0} followers to be deleted.", followersToDeleteIds.Count);
                    foreach (byte[] followerToDeleteId in followersToDeleteIds)
                    {
                        Iop.Shared.Status status = unitOfWork.FollowerRepository.DeleteFollowerAsync(followerToDeleteId).Result;
                        if (status == Iop.Shared.Status.Ok)
                        {
                            log.Debug("Follower ID '{0}' deleted.", followerToDeleteId.ToHex());
                        }
                        else
                        {
                            log.Warn("Unable to delete follower ID '{0}', error code {1}.", followerToDeleteId.ToHex(), status);
                        }
                    }
                }
                else
                {
                    log.Debug("No followers to delete now.");
                }
            }

            log.Trace("(-)");
        }