Example #1
0
        protected void Application_Start()
        {
            Log.Trace();

            // Set the maximum number of concurrent connections
            HttpUtilities.SetServicePointDefaultConnectionLimit();

            var shipHubConfig = new ShipHubCloudConfiguration();

            ApplicationInsightsConfig.Register(shipHubConfig.ApplicationInsightsKey);
            GlobalConfiguration.Configure((config) => WebApiConfig.Register(config, shipHubConfig.RaygunApiKey));
            SimpleInjectorConfig.Register(shipHubConfig);
        }
Example #2
0
        static void Main()
        {
            Log.Trace();
            // Set the maximum number of concurrent connections
            HttpUtilities.SetServicePointDefaultConnectionLimit();

            var shipHubConfig         = new ShipHubCloudConfiguration();
            var azureWebJobsDashboard = shipHubConfig.AzureWebJobsDashboard;
            var azureWebJobsStorage   = shipHubConfig.AzureWebJobsStorage;

            // Raygun Client
            var          raygunApiKey = shipHubConfig.RaygunApiKey;
            RaygunClient raygunClient = null;

            if (!raygunApiKey.IsNullOrWhiteSpace())
            {
                raygunClient = new RaygunClient(raygunApiKey);
                raygunClient.AddWrapperExceptions(typeof(AggregateException));
            }

            // App Insights Client
            var             applicationInsightsKey = shipHubConfig.ApplicationInsightsKey;
            TelemetryClient telemetryClient        = null;

            if (!applicationInsightsKey.IsNullOrWhiteSpace())
            {
                TelemetryConfiguration.Active.InstrumentationKey = applicationInsightsKey;
                telemetryClient = new TelemetryClient();
            }

            var detailedLogger = new DetailedExceptionLogger(telemetryClient, raygunClient);

            var container = CreateContainer(detailedLogger);

            // Hack to address https://github.com/realartists/shiphub-server/issues/277
            // I suspect that when DI occurs, the AzureBlobTraceListener is registered as a TraceListener
            // but not initialized. To get around this, force Orleans to initialize now, before any
            // TraceListeners get added.
            var timer = new Stopwatch();

            timer.Restart();
            Log.Info("[Orleans Client]: Initializing");
            container.GetInstance <IAsyncGrainFactory>();
            timer.Stop();
            Log.Info($"[Orleans Client]: Initialized in {timer.Elapsed}");

            // Job Host Configuration
            var config = new JobHostConfiguration()
            {
                DashboardConnectionString = azureWebJobsDashboard,
                StorageConnectionString   = azureWebJobsStorage,
                JobActivator = new SimpleInjectorJobActivator(container),
            };

            config.Queues.MaxDequeueCount = 2; // Only try twice

            // Gross manual DI
            ConfigureGlobalLogging(config, telemetryClient, raygunClient);

            var azureWebJobsServiceBus = shipHubConfig.AzureWebJobsServiceBus;
            var sbConfig = new ServiceBusConfiguration()
            {
                ConnectionString = azureWebJobsServiceBus,
            };

            sbConfig.MessageOptions.MaxConcurrentCalls = 128;

#if DEBUG
            config.UseDevelopmentSettings();
            config.DashboardConnectionString           = null;
            sbConfig.MessageOptions.AutoRenewTimeout   = TimeSpan.FromSeconds(10); // Abandon locks quickly
            sbConfig.MessageOptions.MaxConcurrentCalls = 1;
            config.Queues.MaxDequeueCount = 1;
#endif

            // https://azure.microsoft.com/en-us/documentation/articles/service-bus-performance-improvements/ recommends
            // 20x the processing rate/sec
            var ratePerSecond = 1;
            sbConfig.PrefetchCount = sbConfig.MessageOptions.MaxConcurrentCalls * 20 * ratePerSecond;

            Log.Info($"[Service Bus]: Initializing");
            timer.Restart();
            var sbFactory = container.GetInstance <IServiceBusFactory>();
            timer.Stop();
            Log.Info($"[Service Bus]: Initialized in {timer.Elapsed}");

            // Override default messaging provider to use pairing.
            sbConfig.MessagingProvider = new PairedMessagingProvider(sbConfig, sbFactory);

            config.UseServiceBus(sbConfig);
            config.UseTimers();
            config.UseCore(); // For ExecutionContext

            Log.Info("[Job Host]: Starting");
            using (var host = new JobHost(config)) {
#if DEBUG
                host.Start();
                Console.WriteLine("Press Any Key to Exit.");
                Console.ReadLine();
                Console.WriteLine("Stopping job host...");
                host.Stop();
#else
                host.RunAndBlock();
#endif
                Log.Info("[Job Host]: Stopped");
            }
        }