private void CleanupFileSystems(DatabasesLandlord databaseLandlord, FileSystemsLandlord fileSystemLandlord)
		{
			var systemDatabase = databaseLandlord.SystemDatabase;

			int nextStart = 0;
			var fileSystemDocuments = systemDatabase
				.Documents
                .GetDocumentsWithIdStartingWith(Constants.FileSystem.Prefix, null, null, 0, int.MaxValue, CancellationToken.None, ref nextStart);

			var fileSystemIds = fileSystemDocuments
				.Select(x => ((RavenJObject)x)["@metadata"])
				.Where(x => x != null)
				.Select(x => x.Value<string>("@id"))
				.Where(x => x != null && x != Constants.SystemDatabase)
				.ToList();

			foreach (var fileSystemId in fileSystemIds)
			{
				try
				{
					var key = fileSystemId;
                    if (key.StartsWith(Constants.FileSystem.Prefix))
                        key = key.Substring(Constants.FileSystem.Prefix.Length);

					var shouldCleanup = false;

					DateTime value;
					if (fileSystemLandlord.IsFileSystemLoaded(key) == false) 
						shouldCleanup = true;
					else if (fileSystemLandlord.LastRecentlyUsed.TryGetValue(key, out value) == false || (SystemTime.UtcNow - value) > maxTimeResourceCanBeIdle) 
						shouldCleanup = true;

					if (shouldCleanup == false) 
						continue;

					var configuration = fileSystemLandlord.CreateTenantConfiguration(key, true);

					fileSystemLandlord.Cleanup(key, maxTimeResourceCanBeIdle, database => false);

                    var docKey = Constants.FileSystem.Prefix + key;
					systemDatabase.Documents.Delete(docKey, null, null);

					if (configuration == null)
						continue;

					IOExtensions.DeleteDirectory(configuration.FileSystem.DataDirectory);
				}
				catch (Exception e)
				{
					log.WarnException(string.Format("Failed to cleanup '{0}' filesystem.", fileSystemId), e);
				}
			}
		}
Пример #2
0
        private void CleanupFileSystems(DatabasesLandlord databaseLandlord, FileSystemsLandlord fileSystemLandlord)
        {
            var systemDatabase = databaseLandlord.SystemDatabase;

            int nextStart           = 0;
            var fileSystemDocuments = systemDatabase
                                      .Documents
                                      .GetDocumentsWithIdStartingWith(Constants.FileSystem.Prefix, null, null, 0, int.MaxValue, CancellationToken.None, ref nextStart);

            var fileSystemIds = fileSystemDocuments
                                .Select(x => ((RavenJObject)x)["@metadata"])
                                .Where(x => x != null)
                                .Select(x => x.Value <string>("@id"))
                                .Where(x => x != null && x != Constants.SystemDatabase)
                                .ToList();

            foreach (var fileSystemId in fileSystemIds)
            {
                try
                {
                    var key = fileSystemId;
                    if (key.StartsWith(Constants.FileSystem.Prefix))
                    {
                        key = key.Substring(Constants.FileSystem.Prefix.Length);
                    }

                    var shouldCleanup = false;

                    DateTime value;
                    if (fileSystemLandlord.IsFileSystemLoaded(key) == false)
                    {
                        shouldCleanup = true;
                    }
                    else if (fileSystemLandlord.LastRecentlyUsed.TryGetValue(key, out value) == false || (SystemTime.UtcNow - value) > maxTimeResourceCanBeIdle)
                    {
                        shouldCleanup = true;
                    }

                    if (shouldCleanup == false)
                    {
                        continue;
                    }

                    var configuration = fileSystemLandlord.CreateTenantConfiguration(key, true);

                    fileSystemLandlord.Cleanup(key, maxTimeResourceCanBeIdle, database => false);

                    var docKey = Constants.FileSystem.Prefix + key;
                    systemDatabase.Documents.Delete(docKey, null, null);

                    if (configuration == null)
                    {
                        continue;
                    }

                    IOExtensions.DeleteDirectory(configuration.FileSystem.DataDirectory);
                }
                catch (Exception e)
                {
                    log.WarnException(string.Format("Failed to cleanup '{0}' filesystem.", fileSystemId), e);
                }
            }
        }