Example #1
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IConfiguration configuration, ExilenceContext exilenceContext, ILogger <Startup> logger)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseRouting();

            app.UseAuthentication();
            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
                endpoints.MapHub <BaseHub>("/hub");
            });

            var instanceName = configuration.GetSection("Settings")["InstanceName"];


            logger.LogInformation("Removing dead connections.");
            //Remove faulty connections to this node on startup if node crasched
            exilenceContext.Database.ExecuteSqlRaw($"DELETE FROM Connections WHERE InstanceName = '{instanceName}'");

            logger.LogInformation("Removing dead groups.");
            //Remove groups with no connections after connection cleanup
            exilenceContext.Database.ExecuteSqlRaw($"DELETE FROM Groups WHERE Id IN (SELECT g.Id FROM Groups g WHERE (SELECT COUNT(*) FROM Connections WHERE GroupId = g.Id) = 0)");

            //Apply mongo migrations on start if neeeded
            var migrationResult = MongoMigrationHandler.Run(configuration.GetSection("ConnectionStrings")["Mongo"], configuration.GetSection("Mongo")["Database"]);

            foreach (var migration in migrationResult.InterimSteps)
            {
                logger.LogInformation($"Applied migration version: {migration.TargetVersion} and name: {migration.MigrationName} to database: {migration.DatabaseName} on host: {migration.ServerAdress}");
            }
            if (migrationResult.InterimSteps.Count() == 0)
            {
                logger.LogInformation($"No pending migrations found. Using MongoDB {migrationResult.DatabaseName} on {migrationResult.ServerAdress} version: {migrationResult.CurrentVersion}.");
            }
        }
 public MongoMigrationController(MongoMigrationHandler handler)
 {
     _handler = handler;
 }