Esempio n. 1
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("(-)");
        }