public async Task CleanUpAsync(PerProcessContext context)
        {
            var blobClient = BlobStorageUtilities.GetBlobClient(context.Global);

            // Delete catalog2dnx artifacts.
            await CleanUpUtilities.DeleteBlobsWithPrefix(
                blobClient,
                context.Global.FlatContainerContainerName,
                context.FlatContainerStoragePath,
                _logger);
        }
Ejemplo n.º 2
0
        public async Task CleanUpAsync(PerBatchContext context, IReadOnlyList <PerPackageContext> packageContexts)
        {
            var blobClient = BlobStorageUtilities.GetBlobClient(context.Global);

            // Delete catalog2lucene artifacts.
            await CleanUpUtilities.DeleteContainer(blobClient, context.LuceneContainerName, _logger);

            var luceneCachePath = Path.Combine(
                CleanUpUtilities.GetLuceneCacheDirectory(),
                context.LuceneContainerName);

            if (Directory.Exists(luceneCachePath))
            {
                Directory.Delete(luceneCachePath, recursive: true);
            }

            // Delete catalog2registration artifacts.
            await CleanUpUtilities.DeleteBlobsWithPrefix(
                blobClient,
                context.Global.RegistrationContainerName,
                context.Worker.Name,
                _logger);

            // Delete catalog2dnx artifacts.
            foreach (var packageContext in packageContexts)
            {
                await CleanUpUtilities.DeleteBlobsWithPrefix(
                    blobClient,
                    context.Global.FlatContainerContainerName,
                    $"{context.Process.FlatContainerStoragePath}/{packageContext.PackageId.ToLowerInvariant()}/{packageContext.PackageVersion.ToLowerInvariant()}",
                    _logger);
            }

            // Delete feed2catalog artifacts.
            await CleanUpUtilities.DeleteBlobsWithPrefix(
                blobClient,
                context.Global.CatalogContainerName,
                context.Worker.Name,
                _logger);
        }
        private static IServiceProvider InitializeAndGetServiceProvider()
        {
            ServicePointManager.DefaultConnectionLimit = 64;
            ServicePointManager.SecurityProtocol      &= ~SecurityProtocolType.Ssl3;
            ServicePointManager.SecurityProtocol      |= SecurityProtocolType.Tls12;

            var configurationRoot  = GetConfigurationRoot();
            var instrumentationKey = configurationRoot
                                     .GetSection(ConfigurationSectionName)
                                     .GetValue <string>(nameof(V3PerPackageConfiguration.InstrumentationKey));

            ApplicationInsights.Initialize(instrumentationKey);

            var loggerConfiguration = LoggingSetup.CreateDefaultLoggerConfiguration(withConsoleLogger: true);
            var loggerFactory       = LoggingSetup.CreateLoggerFactory(loggerConfiguration, LogEventLevel.Information);

            var serviceCollection = new ServiceCollection();

            serviceCollection.Configure <V3PerPackageConfiguration>(configurationRoot.GetSection(ConfigurationSectionName));
            serviceCollection.AddOptions();
            serviceCollection.Add(ServiceDescriptor.Scoped(typeof(IOptionsSnapshot <>), typeof(NonCachingOptionsSnapshot <>)));

            serviceCollection.AddLogging();
            serviceCollection.AddSingleton(loggerFactory);

            serviceCollection.AddSingleton(new ControlledDisposeHttpClientHandler());
            serviceCollection.AddTransient <HttpMessageHandler>(x => x.GetRequiredService <ControlledDisposeHttpClientHandler>());
            serviceCollection.AddSingleton(x => new HttpClient(x.GetRequiredService <HttpMessageHandler>()));
            serviceCollection.AddTransient <Func <HttpMessageHandler> >(x => () => x.GetRequiredService <HttpMessageHandler>());
            serviceCollection.AddTransient <PerBatchProcessor>();
            serviceCollection.AddTransient <ITelemetryService, TelemetryService>();
            serviceCollection.AddTransient <PerWorkerProcessor>();
            serviceCollection.AddTransient <PerProcessProcessor>();
            serviceCollection.AddSingleton <StringLocker>();
            serviceCollection.AddTransient <EnqueueCollector>();
            serviceCollection.AddTransient <EnqueueCommand>();
            serviceCollection.AddTransient <CleanUpCommand>();

            serviceCollection.AddSingleton(x =>
            {
                var globalContext = x.GetRequiredService <GlobalContext>();

                var perProcessContext = new PerProcessContext(
                    globalContext,
                    UniqueName.New("process"),
                    WorkerCount,
                    MessageCount,
                    BatchSize);

                var blobClient       = BlobStorageUtilities.GetBlobClient(globalContext);
                var flatContainerUrl = $"{blobClient.BaseUri.AbsoluteUri}/{globalContext.FlatContainerContainerName}/{perProcessContext.Name}";
                RegistrationMakerCatalogItem.PackagePathProvider = new FlatContainerPackagePathProvider(flatContainerUrl);

                return(perProcessContext);
            });

            serviceCollection.AddTransient(x =>
            {
                var settings = x.GetRequiredService <IOptionsSnapshot <V3PerPackageConfiguration> >();
                return(new GlobalContext(
                           settings.Value.StorageBaseAddress,
                           settings.Value.StorageAccountName,
                           settings.Value.StorageKeyValue,
                           settings.Value.ContentBaseAddress,
                           settings.Value.GalleryBaseAddress));
            });

            serviceCollection.AddSingleton(new TelemetryClient());

            serviceCollection.AddTransient <IStorageQueue <PackageMessage> >(x =>
            {
                var globalContext      = x.GetRequiredService <GlobalContext>();
                var storageCredentials = new StorageCredentials(globalContext.StorageAccountName, globalContext.StorageKeyValue);
                var storageAccount     = new CloudStorageAccount(storageCredentials, useHttps: true);

                return(new StorageQueue <PackageMessage>(
                           new AzureStorageQueue(storageAccount, "v3perpackage"),
                           new JsonMessageSerializer <PackageMessage>(JsonSerializerUtility.SerializerSettings),
                           PackageMessage.Version));
            });

            return(serviceCollection.BuildServiceProvider());
        }