private DomainAccountReservationSet GetReservedAccounts(bool mockReserve = false) { DomainAccountReservationSet result = new DomainAccountReservationSet(Ticket.SessionId); using (AssetInventoryContext context = DbConnect.AssetInventoryContext()) { if (mockReserve) { //Build a mock reservation foreach (var poolType in Quantities.DomainAccountQuantity.Keys) { DomainAccountPool pool = DomainAccountService.SelectPool(context, poolType); result.Add(poolType, pool, 0, Quantities.DomainAccountQuantity[poolType]); } } else { foreach (DomainAccountReservation reservation in DomainAccountService.SelectReservationsBySession(context, Ticket.SessionId)) { DomainAccountPool pool = DomainAccountService.SelectPool(context, reservation.DomainAccountKey); result.Add(reservation.DomainAccountKey, pool, reservation.StartIndex, reservation.Count); } } } return(result); }
/// <summary> /// Reserves a block of users. This MUST be called before being able to get user credentials. /// </summary> /// <param name="sessionId">The session id.</param> /// <param name="accountQuantity">The account quantity which is separated by account pool.</param> /// <returns>A <see cref="DomainAccountReservationSet"/> containing information on the reserved accounts.</returns> /// <exception cref="System.ArgumentNullException">accountQuantity</exception> /// <exception cref="System.InvalidOperationException">Domain accounts have already been reserved for this Session Id</exception> public static DomainAccountReservationSet Reserve(string sessionId, DomainAccountQuantityDictionary accountQuantity) { if (accountQuantity == null) { throw new ArgumentNullException("accountQuantity"); } DomainAccountReservationSet reservedBlock = new DomainAccountReservationSet(sessionId); Action action = new Action(() => { try { using (AssetInventoryContext context = DbConnect.AssetInventoryContext()) { if (context.DomainAccountReservations.Any(e => e.SessionId.Equals(sessionId, StringComparison.OrdinalIgnoreCase))) { //This could be a subsequent call to reserve. Clear all reservations before proceeding. Release(sessionId); } foreach (var poolType in accountQuantity.Keys) { int userCount = accountQuantity[poolType]; DomainAccountPool pool = SelectPool(context, poolType); int startIndex = ReserveBlock(context, sessionId, pool, userCount); TraceFactory.Logger.Debug($"New pool reserved: StartIndex: {startIndex}, UserCount: {userCount}, PoolName: {pool.DomainAccountKey}"); reservedBlock.Add(poolType, pool, startIndex, userCount); } } } catch (InsufficientDomainAccountsException) { ReleaseSessionReservations(sessionId); throw; } }); var token = new GlobalLockToken("DomainAccountReservation", TimeSpan.FromMinutes(11), TimeSpan.FromMinutes(2)); ExecutionServices.CriticalSection.Run(token, action); return(reservedBlock); }