Exemplo n.º 1
0
        private void SetupRequestToProperDatabase(IHttpContext ctx)
        {
            var requestUrl = ctx.GetRequestUrl();
            var match      = TenantsQuery.Match(requestUrl);

            IResourceStore resourceStore;

            if (match.Success == false)
            {
                currentTenantId.Value      = "<default>";
                currentDatabase.Value      = DefaultResourceStore;
                currentConfiguration.Value = DefaultConfiguration;
            }
            else
            {
                var tenantId = match.Groups[1].Value;
                if (TryGetOrCreateResourceStore(tenantId, out resourceStore))
                {
                    databaseLastRecentlyUsed.AddOrUpdate(tenantId, DateTime.Now, (s, time) => DateTime.Now);

                    if (string.IsNullOrEmpty(Configuration.VirtualDirectory) == false && Configuration.VirtualDirectory != "/")
                    {
                        ctx.AdjustUrl(Configuration.VirtualDirectory + match.Value);
                    }
                    else
                    {
                        ctx.AdjustUrl(match.Value);
                    }
                    currentTenantId.Value      = tenantId;
                    currentDatabase.Value      = resourceStore;
                    currentConfiguration.Value = resourceStore.Configuration;
                }
                else
                {
                    throw new BadRequestException("Could not find a database named: " + tenantId);
                }
            }
        }
Exemplo n.º 2
0
        private void SetupRequestToProperDatabase(IHttpContext ctx)
        {
            var requestUrl = ctx.GetRequestUrl();
            var match      = TenantsQuery.Match(requestUrl);

            IResourceStore resourceStore;

            if (match.Success == false)
            {
                currentDatabase.Value      = DefaultResourceStore;
                currentConfiguration.Value = DefaultConfiguration;
            }
            else if (TryGetOrCreateResourceStore(match.Groups[1].Value, out resourceStore))
            {
                databaseLastRecentlyUsed.AddOrUpdate(match.Groups[1].Value, DateTime.Now, (s, time) => DateTime.Now);
                ctx.AdjustUrl(match.Value);
                currentDatabase.Value      = resourceStore;
                currentConfiguration.Value = resourceStore.Configuration;
            }
            else
            {
                throw new BadRequestException("Could not find a database named: " + match.Groups[1].Value);
            }
        }
Exemplo n.º 3
0
		private bool SetupRequestToProperDatabase(IHttpContext ctx)
		{
			var requestUrl = ctx.GetRequestUrlForTenantSelection();
			var match = databaseQuery.Match(requestUrl);
			var onBeforeRequest = BeforeRequest;
			if (match.Success == false)
			{
				currentTenantId.Value = Constants.SystemDatabase;
				currentDatabase.Value = SystemDatabase;
				currentConfiguration.Value = SystemConfiguration;
				databaseLastRecentlyUsed.AddOrUpdate("System", SystemTime.UtcNow, (s, time) => SystemTime.UtcNow);
				if (onBeforeRequest != null)
				{
					var args = new BeforeRequestEventArgs
					{
						Context = ctx,
						IgnoreRequest = false,
						TenantId = "System",
						Database = SystemDatabase
					};
					onBeforeRequest(this, args);
					if (args.IgnoreRequest)
						return false;
				}

				return true;
			}
			var tenantId = match.Groups[1].Value;
			Task<DocumentDatabase> resourceStoreTask;
			bool hasDb;
			try
			{
				hasDb = TryGetOrCreateResourceStore(tenantId, out resourceStoreTask);
			}
			catch (Exception e)
			{
				OutputDatabaseOpenFailure(ctx, tenantId, e);
				return false;
			}
			if (hasDb)
			{
				try
				{
					if (resourceStoreTask.Wait(TimeSpan.FromSeconds(30)) == false)
					{
						ctx.SetStatusToNotAvailable();
						ctx.WriteJson(new
						{
							Error = "The database " + tenantId + " is currently being loaded, but after 30 seconds, this request has been aborted. Please try again later, database loading continues.",
						});
						return false;
					}
					if (onBeforeRequest != null)
					{
						var args = new BeforeRequestEventArgs
						{
							Context = ctx,
							IgnoreRequest = false,
							TenantId = tenantId,
							Database = resourceStoreTask.Result
						};
						onBeforeRequest(this, args);
						if (args.IgnoreRequest)
							return false;
					}
				}
				catch (Exception e)
				{
					OutputDatabaseOpenFailure(ctx, tenantId, e);
					return false;
				}
				var resourceStore = resourceStoreTask.Result;

				databaseLastRecentlyUsed.AddOrUpdate(tenantId, SystemTime.UtcNow, (s, time) => SystemTime.UtcNow);

				if (string.IsNullOrEmpty(Configuration.VirtualDirectory) == false && Configuration.VirtualDirectory != "/")
				{
					ctx.AdjustUrl(Configuration.VirtualDirectory + match.Value);
				}
				else
				{
					ctx.AdjustUrl(match.Value);
				}
				currentTenantId.Value = tenantId;
				currentDatabase.Value = resourceStore;
				currentConfiguration.Value = resourceStore.Configuration;
			}
			else
			{
				ctx.SetStatusToNotFound();
				ctx.WriteJson(new
				{
					Error = "Could not find a database named: " + tenantId
				});
				return false;
			}
			return true;
		}
Exemplo n.º 4
0
		private void SetupRequestToProperDatabase(IHttpContext ctx)
		{
			var requestUrl = ctx.GetRequestUrlForTenantSelection();
			var match = databaseQuery.Match(requestUrl);

			if (match.Success == false)
			{
				currentTenantId.Value = Constants.DefaultDatabase;
				currentDatabase.Value = DefaultResourceStore;
				currentConfiguration.Value = DefaultConfiguration;
			}
			else
			{
				var tenantId = match.Groups[1].Value;
				DocumentDatabase resourceStore;
				if (TryGetOrCreateResourceStore(tenantId, out resourceStore))
				{
					databaseLastRecentlyUsed.AddOrUpdate(tenantId, SystemTime.Now, (s, time) => SystemTime.Now);

					if (string.IsNullOrEmpty(Configuration.VirtualDirectory) == false && Configuration.VirtualDirectory != "/")
					{
						ctx.AdjustUrl(Configuration.VirtualDirectory + match.Value);
					}
					else
					{
						ctx.AdjustUrl(match.Value);
					}
					currentTenantId.Value = tenantId;
					currentDatabase.Value = resourceStore;
					currentConfiguration.Value = resourceStore.Configuration;
				}
				else
				{
					throw new BadRequestException("Could not find a database named: " + tenantId);
				}
			}
		}
Exemplo n.º 5
0
		private bool SetupRequestToProperDatabase(IHttpContext ctx)
		{
			var requestUrl = ctx.GetRequestUrlForTenantSelection();
			var match = databaseQuery.Match(requestUrl);
			var onBeforeRequest = BeforeRequest;
			if (match.Success == false)
			{
				currentTenantId.Value = Constants.SystemDatabase;
				currentDatabase.Value = SystemDatabase;
				currentConfiguration.Value = SystemConfiguration;
				SystemDatabase.WorkContext.UpdateFoundWork();
				if (onBeforeRequest != null)
				{
					var args = new BeforeRequestEventArgs
					{
						Context = ctx,
						IgnoreRequest = false,
						TenantId = "System",
						Database = SystemDatabase
					};
					onBeforeRequest(this, args);
					if (args.IgnoreRequest)
						return false;
				}

				return true;
			}
			var tenantId = match.Groups[1].Value;
			Task<DocumentDatabase> resourceStoreTask;
			bool hasDb;
			try
			{
				hasDb = TryGetOrCreateResourceStore(tenantId, out resourceStoreTask);
			}
			catch (Exception e)
			{
				OutputDatabaseOpenFailure(ctx, tenantId, e);
				return false;
			}

			if (hasDb)
			{
				try
				{
					const int timeToWaitForDatabaseToLoad = 5;
					if (resourceStoreTask.IsCompleted == false && resourceStoreTask.IsFaulted == false)
					{
						if (_maxNumberOfThreadsForDatabaseToLoad.Wait(0) == false)
						{
							ctx.SetStatusToNotAvailable();
							ctx.WriteJson(new
							{
								Error =
									string.Format(
										"The database {0} is currently being loaded, but there are too many requests waiting for database load. Please try again later, database loading continues.",
										tenantId),
							});
							return false;
						}
						try
						{
							if (resourceStoreTask.Wait(TimeSpan.FromSeconds(timeToWaitForDatabaseToLoad)) == false)
							{
								ctx.SetStatusToNotAvailable();
								ctx.WriteJson(new
								{
									Error =
										string.Format(
											"The database {0} is currently being loaded, but after {1} seconds, this request has been aborted. Please try again later, database loading continues.",
											tenantId, timeToWaitForDatabaseToLoad),
								});
								return false;
							}
						}
						finally
						{
							_maxNumberOfThreadsForDatabaseToLoad.Release();
						}
					}
					if (onBeforeRequest != null)
					{
						var args = new BeforeRequestEventArgs
						{
							Context = ctx,
							IgnoreRequest = false,
							TenantId = tenantId,
							Database = resourceStoreTask.Result
						};
						onBeforeRequest(this, args);
						if (args.IgnoreRequest)
							return false;
					}
				}
				catch (Exception e)
				{
					OutputDatabaseOpenFailure(ctx, tenantId, e);
					return false;
				}
				var resourceStore = resourceStoreTask.Result;

				resourceStore.WorkContext.UpdateFoundWork();

				if (string.IsNullOrEmpty(Configuration.VirtualDirectory) == false && Configuration.VirtualDirectory != "/")
				{
					ctx.AdjustUrl(Configuration.VirtualDirectory + match.Value);
				}
				else
				{
					ctx.AdjustUrl(match.Value);
				}
				currentTenantId.Value = tenantId;
				currentDatabase.Value = resourceStore;
				currentConfiguration.Value = resourceStore.Configuration;
			}
			else
			{
				ctx.SetStatusToNotFound();
				ctx.WriteJson(new
				{
					Error = "Could not find a database named: " + tenantId
				});
				return false;
			}

			return true;
		}
Exemplo n.º 6
0
        private void SetupRequestToProperDatabase(IHttpContext ctx)
        {
            var requestUrl = ctx.GetRequestUrl();
            var match = TenantsQuery.Match(requestUrl);

            IResourceStore resourceStore;
            if (match.Success == false)
            {
                currentTenantId.Value = "<default>";
                currentDatabase.Value = DefaultResourceStore;
                currentConfiguration.Value = DefaultConfiguration;
            } 
            else
            {
                var tenantId = match.Groups[1].Value;
                if(TryGetOrCreateResourceStore(tenantId, out resourceStore))
                {
                    databaseLastRecentlyUsed.AddOrUpdate(tenantId, DateTime.Now, (s, time) => DateTime.Now);
                    ctx.AdjustUrl(match.Value);
                    currentTenantId.Value = tenantId;
                    currentDatabase.Value = resourceStore;
                    currentConfiguration.Value = resourceStore.Configuration;
                }
                else
                {
                    throw new BadRequestException("Could not find a database named: " + tenantId);
                }
            }
        }
Exemplo n.º 7
0
        private bool SetupRequestToProperDatabase(IHttpContext ctx)
        {
            var requestUrl = ctx.GetRequestUrlForTenantSelection();
            var match      = databaseQuery.Match(requestUrl);

            if (match.Success == false)
            {
                currentTenantId.Value      = Constants.SystemDatabase;
                currentDatabase.Value      = SystemDatabase;
                currentConfiguration.Value = SystemConfiguration;
                databaseLastRecentlyUsed.AddOrUpdate("System", SystemTime.UtcNow, (s, time) => SystemTime.UtcNow);
                return(true);
            }
            var tenantId = match.Groups[1].Value;
            Task <DocumentDatabase> resourceStoreTask;
            bool hasDb;

            try
            {
                hasDb = TryGetOrCreateResourceStore(tenantId, out resourceStoreTask);
            }
            catch (Exception e)
            {
                OutputDatabaseOpenFailure(ctx, tenantId, e);
                return(false);
            }
            if (hasDb)
            {
                try
                {
                    if (resourceStoreTask.Wait(TimeSpan.FromSeconds(30)) == false)
                    {
                        ctx.SetStatusToNotAvailable();
                        ctx.WriteJson(new
                        {
                            Error = "The database " + tenantId + " is currently being loaded, but after 30 seconds, this request has been aborted. Please try again later, database loading continues.",
                        });
                        return(false);
                    }
                }
                catch (Exception e)
                {
                    OutputDatabaseOpenFailure(ctx, tenantId, e);
                    return(false);
                }
                var resourceStore = resourceStoreTask.Result;

                databaseLastRecentlyUsed.AddOrUpdate(tenantId, SystemTime.UtcNow, (s, time) => SystemTime.UtcNow);

                if (string.IsNullOrEmpty(Configuration.VirtualDirectory) == false && Configuration.VirtualDirectory != "/")
                {
                    ctx.AdjustUrl(Configuration.VirtualDirectory + match.Value);
                }
                else
                {
                    ctx.AdjustUrl(match.Value);
                }
                currentTenantId.Value      = tenantId;
                currentDatabase.Value      = resourceStore;
                currentConfiguration.Value = resourceStore.Configuration;
            }
            else
            {
                ctx.SetStatusToNotAvailable();
                ctx.WriteJson(new
                {
                    Error = "Could not find a database named: " + tenantId
                });
                return(false);
            }
            return(true);
        }
Exemplo n.º 8
0
        private void SetupRequestToProperDatabase(IHttpContext ctx)
        {
            var requestUrl = ctx.GetRequestUrl();
            var match = databaseQuery.Match(requestUrl);

            DocumentDatabase database;
            if (match.Success == false)
            {
                currentDatabase.Value = defaultDatabase;
                currentConfiguration.Value = defaultConfiguration;
            } 
            else if(TryGetOrCreateDatabase(match.Groups[1].Value, out database))
            {
                databaseLastRecentlyUsed.AddOrUpdate(match.Groups[1].Value, DateTime.Now, (s, time) => DateTime.Now);
                ctx.AdjustUrl(match.Value);
                currentDatabase.Value = database;
                currentConfiguration.Value = database.Configuration;
            }
            else
            {
                throw new BadRequestException("Could not find a database named: " + match.Groups[1].Value);
            }
        }