/// <summary> /// Checks if any of the neighbors expired. /// If so, it starts the process of their removal. /// </summary> public void CheckExpiredNeighbors() { log.Trace("()"); // If a neighbor server's LastRefreshTime is lower than this limit, it is expired. DateTime limitLastRefreshTime = DateTime.UtcNow.AddSeconds(-Base.Configuration.NeighborProfilesExpirationTimeSeconds); using (UnitOfWork unitOfWork = new UnitOfWork()) { bool success = false; DatabaseLock[] lockObjects = new DatabaseLock[] { UnitOfWork.NeighborLock, UnitOfWork.NeighborhoodActionLock }; using (IDbContextTransaction transaction = unitOfWork.BeginTransactionWithLock(lockObjects)) { try { List <Neighbor> expiredNeighbors = unitOfWork.NeighborRepository.Get(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 }; unitOfWork.NeighborhoodActionRepository.Insert(action); } unitOfWork.SaveThrow(); 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("(-)"); }