Ejemplo n.º 1
0
        private static void InternalTestConnection(IServiceProvider services)
        {
            ILoggerFactory loggerF = services.GetService <ILoggerFactory>();

            ILogger logger = loggerF.CreateLogger("Program");

            IServiceScopeFactory serviceScopeFactory = services.GetService <IServiceScopeFactory>();

            using (var scope = serviceScopeFactory.CreateScope())
            {
                MonitorDBContext db = scope.ServiceProvider.GetRequiredService <MonitorDBContext>();

                try
                {
                    if (!db.Database.CanConnect())
                    {
                        logger.LogError("The database connection settings is invalid, or the server can't connect to the database.");
                        Environment.Exit(-1);
                    }
                }
                catch (Exception err)
                {
                    if (err is MySqlException || err is InvalidOperationException)
                    {
                        string errorText = "The database connection settings is invalid, or the server can't connect to the database.\nError: {0}\nStack trace: {1}";
                        logger.LogError(err, string.Format(errorText, err.Message, err.StackTrace));

                        Environment.Exit(-1);
                        return;
                    }
                    throw;
                }
            }
        }
Ejemplo n.º 2
0
        private static IDictionary <string, object> CreateAndSaveDefaultValues(
            MonitorDBContext dbContext)
        {
            // TODO: Set Settings
            var configValues = MonitorConfig.DefaultConfig;

            foreach (
                var config in
                configValues
                .Select(kvp => new Setting
            {
                Key = kvp.Key,
                Value = kvp.Value
            })
                .ToArray()
                .Where(conf => !dbContext.Settings.Any(con => con.Key == conf.Key))
                )
            {
                dbContext.Settings.Add(config);
            }

            dbContext.SaveChanges();

            return(configValues);
        }
Ejemplo n.º 3
0
        // Load config data from EF DB.
        public override void Load()
        {
            var builder = new DbContextOptionsBuilder <MonitorDBContext>();

            OptionsAction(builder);

            using (var dbContext = new MonitorDBContext(builder.Options, Options.Create(new OperationalStoreOptions())))
            {
                //dbContext.Database.EnsureCreated();
                try
                {
                    CreateAndSaveDefaultValues(dbContext);

                    Data = dbContext.Settings.ToDictionary(c => c.Key, c => c.Value)
                           .Select(pair => new KeyValuePair <string, string>(
                                       $"MonitorDBConfig:{pair.Key}",
                                       //System.Text.Json.JsonSerializer.Serialize(pair.Value, null)
                                       pair.Value.ToString()
                                       ))
                           .ToDictionary(x => x.Key, x => x.Value);
                }
                catch (Exception err)
                {
                    // Cannot connect to DB for EF Config
                }
            }
        }
Ejemplo n.º 4
0
        private static void InternalCheckForUpdate(IServiceProvider services)
        {
            IServiceScopeFactory serviceScopeFactory = services.GetService <IServiceScopeFactory>();

            using (var scope = serviceScopeFactory.CreateScope())
            {
                MonitorDBContext db = scope.ServiceProvider.GetRequiredService <MonitorDBContext>();

                bool shouldMigrateDatabase = db.Database.GetPendingMigrations().Any();

                ILoggerFactory loggerF = services.GetService <ILoggerFactory>();
                ILogger        logger  = loggerF.CreateLogger("Program");


                if (shouldMigrateDatabase)
                {
                    logger.LogInformation("Version out of date or old, performing updating!");

                    try
                    {
                        db.Database.Migrate();
                    }
                    catch (Exception err)
                    {
                        logger.LogError(err, "There has been an error while performing the Database upgrade! Err: {0}", err.Message);
                        Environment.Exit(-1);
                        return;
                    }
                }

                // Write the version file
                string[] data = new string[]
                {
                    MonitorConfig.Version.ToString(CultureInfo.InvariantCulture)
                };

                File.WriteAllLines("application.dat", data);


                if (shouldMigrateDatabase)
                {
                    // After the upgrade, restart
                    logger.LogInformation("Application upgraded succesfully!");
                    //host.Services.GetService<Microsoft.AspNetCore.Hosting.IApplicationLifetime>().StopApplication();
                    ReloadInternalConfigAfterUpgrade(services);
                }
            }
        }