Пример #1
0
        private async Task RefreshConfigurationForever(CancellationToken token)
        {
            await Task.Delay(ConfigurationRefreshPeriod);

            StatusConfiguration = await _configurationFactory.Get <StatusConfiguration>();
            await RefreshConfigurationForever(token);
        }
Пример #2
0
        private async Task RefreshConfigurationForever(CancellationToken token)
        {
            while (!token.IsCancellationRequested)
            {
                await Task.Delay(ConfigurationRefreshPeriod, token);

                StatusConfiguration = await _configurationFactory.Get <StatusConfiguration>();
            }
        }
Пример #3
0
        static MvcApplication()
        {
            var configurationDictionary = ConfigurationManager.AppSettings.AllKeys.ToDictionary(key => key, key => ConfigurationManager.AppSettings[key]);
            var secretReaderFactory     = new SecretReaderFactory(configurationDictionary);
            var secretReader            = secretReaderFactory.CreateSecretReader();
            var secretInjector          = secretReaderFactory.CreateSecretInjector(secretReader);

            var configurationProvider = new AppSettingsConfigurationProvider(secretInjector);

            _configurationFactory = new ConfigurationFactory(configurationProvider);

            StatusConfiguration = _configurationFactory.Get <StatusConfiguration>().Result;
        }
Пример #4
0
        private async void ReopenCallback(object state)
        {
            try
            {
                int val = Interlocked.Increment(ref _gate);
                if (val > 1)
                {
                    _logger.LogInformation(LogMessages.SearchIndexAlreadyReopened, Thread.CurrentThread.ManagedThreadId);
                    Interlocked.Decrement(ref _gate);
                    return;
                }

                _logger.LogInformation(LogMessages.SearchIndexReopenStarted, Thread.CurrentThread.ManagedThreadId);

                try
                {
                    var stopwatch = Stopwatch.StartNew();

                    var newConfig = await _configFactory.Get <BasicSearchConfiguration>();

                    _searcherManager.MaybeReopen(newConfig);

                    stopwatch.Stop();

                    _logger.LogInformation(LogMessages.SearchIndexReopenCompleted, stopwatch.Elapsed.TotalSeconds,
                                           Thread.CurrentThread.ManagedThreadId);

                    _searchTelemetryClient.TrackMetric(
                        SearchTelemetryClient.MetricName.SearchIndexReopenDuration, stopwatch.Elapsed.TotalSeconds);

                    TrackIndexMetrics(_searcherManager, _searchTelemetryClient);
                }
                finally
                {
                    Interlocked.Decrement(ref _gate);
                }
            }
            catch (Exception e)
            {
                _logger.LogError(LogMessages.SearchIndexReopenFailed, e);

                _searchTelemetryClient.TrackMetric(SearchTelemetryClient.MetricName.SearchIndexReopenFailed, 1);
            }
        }
Пример #5
0
        public void Configuration(IAppBuilder app, IConfigurationFactory configFactory, Directory directory,
                                  ILoader loader)
        {
            _configFactory = configFactory;
            var config = _configFactory.Get <BasicSearchConfiguration>().Result;

            // Configure
            if (!string.IsNullOrEmpty(config.ApplicationInsightsInstrumentationKey))
            {
                TelemetryConfiguration.Active.InstrumentationKey = config.ApplicationInsightsInstrumentationKey;
            }

            // Add telemetry initializers
            TelemetryConfiguration.Active.TelemetryInitializers.Add(new MachineNameTelemetryInitializer());
            TelemetryConfiguration.Active.TelemetryInitializers.Add(new DeploymentIdTelemetryInitializer());

            // Create telemetry sink
            _searchTelemetryClient = new SearchTelemetryClient();

            // Add telemetry processors
            var processorChain = TelemetryConfiguration.Active.TelemetryProcessorChainBuilder;

            processorChain.Use(next =>
            {
                var processor = new RequestTelemetryProcessor(next);

                processor.SuccessfulResponseCodes.Add(400);
                processor.SuccessfulResponseCodes.Add(404);

                return(processor);
            });

            processorChain.Use(next => new ExceptionTelemetryProcessor(next, _searchTelemetryClient.TelemetryClient));

            processorChain.Build();

            // Create an ILoggerFactory
            var loggerConfiguration = LoggingSetup.CreateDefaultLoggerConfiguration(withConsoleLogger: false)
                                      .Enrich.With <HttpRequestIdEnricher>()
                                      .Enrich.With <HttpRequestTraceIdEnricher>()
                                      .Enrich.With <HttpRequestTypeEnricher>()
                                      .Enrich.With <HttpRequestUrlReferrerEnricher>()
                                      .Enrich.With <HttpRequestUserAgentEnricher>()
                                      .Enrich.With <HttpRequestRawUrlEnricher>();

            var loggerFactory = LoggingSetup.CreateLoggerFactory(loggerConfiguration);

            // Create a logger that is scoped to this class (only)
            _logger = loggerFactory.CreateLogger <Startup>();

            _logger.LogInformation(LogMessages.AppStartup);

            // Overwrite the index's Azure Directory cache path if configured ot use an Azure Local Storage resource.
            if (!string.IsNullOrEmpty(config.AzureDirectoryCacheLocalResourceName))
            {
                if (SafeRoleEnvironment.TryGetLocalResourceRootPath(config.AzureDirectoryCacheLocalResourceName, out var path))
                {
                    config.AzureDirectoryCachePath = path;

                    _logger.LogInformation(
                        "Set Azure Directory cache path to Azure Local Resource = {LocalResourceName}, Path = {LocalResourcePath}",
                        config.AzureDirectoryCacheLocalResourceName,
                        config.AzureDirectoryCachePath);
                }
                else
                {
                    _logger.LogWarning(
                        "Cannot use Azure Local Resource {LocalResourceName} for caching when the RoleEnvironment is not available",
                        config.AzureDirectoryCacheLocalResourceName);
                }
            }

            // redirect all HTTP requests to HTTPS
            if (config.RequireSsl)
            {
                if (string.IsNullOrWhiteSpace(config.ForceSslExclusion))
                {
                    app.UseForceSsl(config.SslPort);
                }
                else
                {
                    app.UseForceSsl(config.SslPort, new[] { config.ForceSslExclusion });
                }
            }

            // Correlate requests
            app.Use(typeof(CorrelationIdMiddleware));

            // Add Application Insights
            app.Use(typeof(RequestTrackingMiddleware));

            // Set up exception logging
            app.Use(typeof(ExceptionTrackingMiddleware));

            // Enable HSTS
            app.Use(async(context, next) =>
            {
                context.Response.Headers.Add("Strict-Transport-Security", new string[] { "max-age=31536000; includeSubDomains" });
                await next.Invoke();
            });

            // Disable content type sniffing
            app.Use(async(context, next) =>
            {
                context.Response.Headers.Add("X-Content-Type-Options", new[] { "nosniff" });
                await next.Invoke();
            });

            // Enable CORS
            var corsPolicy = new CorsPolicy
            {
                Methods         = { "GET", "HEAD", "OPTIONS" },
                Headers         = { "Content-Type", "If-Match", "If-Modified-Since", "If-None-Match", "If-Unmodified-Since", "Accept-Encoding" },
                ExposedHeaders  = { "Content-Type", "Content-Length", "Last-Modified", "Transfer-Encoding", "ETag", "Date", "Vary", "Server", "X-Hit", "X-CorrelationId" },
                AllowAnyOrigin  = true,
                PreflightMaxAge = 3600
            };

            app.UseCors(new CorsOptions
            {
                PolicyProvider = new CorsPolicyProvider
                {
                    PolicyResolver = context => Task.FromResult(corsPolicy)
                }
            });

            // Search test console
            app.Use(typeof(SearchConsoleMiddleware));
            app.UseStaticFiles(new StaticFileOptions(new SharedOptions
            {
                RequestPath = new PathString("/console"),
                FileSystem  = new EmbeddedResourceFileSystem(typeof(Startup).Assembly, "NuGet.Services.BasicSearch.Console")
            }));

            // Start the service running - the Lucene index needs to be reopened regularly on a background thread
            var intervalSec = config.IndexRefreshSec;

            _logger.LogInformation(LogMessages.SearchIndexRefreshConfiguration, intervalSec);

            if (InitializeSearcherManager(config, directory, loader, loggerFactory))
            {
                var intervalMs = intervalSec * 1000;

                _gate             = 0;
                _indexReloadTimer = new Timer(ReopenCallback, 0, intervalMs, intervalMs);
            }

            _responseWriter = new ResponseWriter();

            app.Run(InvokeAsync);
        }
Пример #6
0
        /// <summary>
        /// Executes the process event.
        /// </summary>
        /// <param name="args">The arguments.</param>
        protected void DoProcess(DetermineChannelProcessorArgs args)
        {
            try
            {
                if (args.Interaction != null && !string.IsNullOrEmpty(args.Interaction.Referrer))
                {
                    InteractionChannelMappingsConfiguration interactionChannelMappingsConfiguration = _configurationFactory.Get <InteractionChannelMappingsConfiguration>();

                    if (interactionChannelMappingsConfiguration.IsEmpty)
                    {
                        _log.Warn("No InteractionChannelMappingsConfiguration found. Interaction-socialchannel-mapping only works properly if there is a <interactionChannelMappings> node present in the configuration.", this);
                    }

                    Uri             interactionUrlReferrer = new Uri(args.Interaction.Referrer);
                    ChannelSettings channelSettings2       = (from channelSettings in interactionChannelMappingsConfiguration.Channels
                                                              where !string.IsNullOrEmpty(channelSettings.ChannelId)
                                                              select channelSettings).FirstOrDefault((ChannelSettings channelSettings) => channelSettings.ChannelMappings.Any((ChannelMappingSettings channelMappingSettings) => string.Compare(channelMappingSettings.UrlReferrerHost, interactionUrlReferrer.Host, StringComparison.InvariantCultureIgnoreCase) == 0));
                    if (channelSettings2 != null)
                    {
                        args.ChannelId = ID.Parse(channelSettings2.ChannelId);
                        _log.Debug($"Interaction has been mapped to channel '{args.ChannelId}' for referrer '{args.Interaction.Referrer}'");
                    }
                }
            }
            catch (Exception exception)
            {
                _log.Error("Couldn't determine the social channels for the interaction.", exception, this);
            }
        }
 public T GetConfig <T>(IConfigurationFactory configFactory) where T : Configuration, new()
 {
     return(configFactory.Get <T>().Result);
 }