Add: process startup, save logs.

Fix: control exceptions when connecting to mysql.
This commit is contained in:
Javier Pastor 2019-11-20 14:32:00 +01:00
commit 64d68f0d92
5 changed files with 160 additions and 55 deletions

View file

@ -10,7 +10,16 @@ namespace Ombi.Store.Context.MySql
if (_created) return;
_created = true;
Database.Migrate();
try
{
Database.Migrate();
}
catch (System.InvalidOperationException)
{
}
catch (System.Exception)
{
}
}
}
}

View file

@ -10,7 +10,16 @@ namespace Ombi.Store.Context.MySql
if (_created) return;
_created = true;
Database.Migrate();
try
{
Database.Migrate();
}
catch (System.InvalidOperationException)
{
}
catch (System.Exception)
{
}
}
public override void Dispose()

View file

@ -10,7 +10,17 @@ namespace Ombi.Store.Context.MySql
if (_created) return;
_created = true;
Database.Migrate();
try
{
Database.Migrate();
}
catch (System.InvalidOperationException)
{
}
catch (System.Exception)
{
}
}
}
}

View file

@ -20,6 +20,7 @@ namespace Ombi
public class Program
{
private static string UrlArgs { get; set; }
private static StartupLog _log = new StartupLog();
public static void Main(string[] args)
{
@ -57,77 +58,104 @@ namespace Ombi
DeleteSchedules();
//CheckAndMigrate();
_log.Config();
var services = new ServiceCollection();
services.ConfigureDatabases();
using (var provider = services.BuildServiceProvider())
{
var settingsDb = provider.GetRequiredService<SettingsContext>();
var config = settingsDb.ApplicationConfigurations.ToList();
var url = config.FirstOrDefault(x => x.Type == ConfigurationTypes.Url);
var dbBaseUrl = config.FirstOrDefault(x => x.Type == ConfigurationTypes.BaseUrl);
if (url == null)
try
{
url = new ApplicationConfiguration
var settingsDb = provider.GetRequiredService<SettingsContext>();
var config = settingsDb.ApplicationConfigurations.ToList();
var url = config.FirstOrDefault(x => x.Type == ConfigurationTypes.Url);
var dbBaseUrl = config.FirstOrDefault(x => x.Type == ConfigurationTypes.BaseUrl);
if (url == null)
{
Type = ConfigurationTypes.Url,
Value = "http://*:5000"
};
using (var tran = settingsDb.Database.BeginTransaction())
{
settingsDb.ApplicationConfigurations.Add(url);
settingsDb.SaveChanges();
tran.Commit();
}
urlValue = url.Value;
}
if (!url.Value.Equals(host))
{
url.Value = UrlArgs;
using (var tran = settingsDb.Database.BeginTransaction())
{
settingsDb.SaveChanges();
tran.Commit();
}
urlValue = url.Value;
}
if (dbBaseUrl == null)
{
if (baseUrl.HasValue() && baseUrl.StartsWith("/"))
{
dbBaseUrl = new ApplicationConfiguration
url = new ApplicationConfiguration
{
Type = ConfigurationTypes.BaseUrl,
Value = baseUrl
Type = ConfigurationTypes.Url,
Value = "http://*:5000"
};
using (var tran = settingsDb.Database.BeginTransaction())
{
settingsDb.ApplicationConfigurations.Add(url);
settingsDb.SaveChanges();
tran.Commit();
}
urlValue = url.Value;
}
if (!url.Value.Equals(host))
{
url.Value = UrlArgs;
using (var tran = settingsDb.Database.BeginTransaction())
{
settingsDb.SaveChanges();
tran.Commit();
}
urlValue = url.Value;
}
if (dbBaseUrl == null)
{
if (baseUrl.HasValue() && baseUrl.StartsWith("/"))
{
dbBaseUrl = new ApplicationConfiguration
{
Type = ConfigurationTypes.BaseUrl,
Value = baseUrl
};
using (var tran = settingsDb.Database.BeginTransaction())
{
settingsDb.ApplicationConfigurations.Add(dbBaseUrl);
settingsDb.SaveChanges();
tran.Commit();
}
}
}
else if (baseUrl.HasValue() && !baseUrl.Equals(dbBaseUrl.Value))
{
dbBaseUrl.Value = baseUrl;
using (var tran = settingsDb.Database.BeginTransaction())
{
settingsDb.ApplicationConfigurations.Add(dbBaseUrl);
settingsDb.SaveChanges();
tran.Commit();
}
}
Console.WriteLine($"We are running on {urlValue}");
CreateWebHostBuilder(args).Build().Run();
}
else if (baseUrl.HasValue() && !baseUrl.Equals(dbBaseUrl.Value))
catch (MySql.Data.MySqlClient.MySqlException e) when (e.SqlState.Equals("28000") || e.SqlState.Equals("42000"))
{
dbBaseUrl.Value = baseUrl;
using (var tran = settingsDb.Database.BeginTransaction())
{
settingsDb.SaveChanges();
tran.Commit();
}
// numbre = 1045, sqlstate = 28000, "Access denied for user 'ombi_dev'@'x.x.x.x' (using password: NO)"
// number = 1044, sqlstate = 42000, "Access denied for user 'ombi_dev'@'x.x.x.x' to database 'OmbiDev'"
_log.LogError("MySQL > " + e.Message);
return;
}
catch (MySql.Data.MySqlClient.MySqlException e)
{
_log.LogError(e.ToString());
throw;
}
catch (System.InvalidOperationException e) when (e.InnerException is MySql.Data.MySqlClient.MySqlException && e.InnerException.Message.Equals("Unable to connect to any of the specified MySQL hosts."))
{
_log.LogError("MySQL > " + e.InnerException.Message);
return;
}
catch (System.Exception e)
{
_log.LogError(e.ToString());
throw;
}
Console.WriteLine($"We are running on {urlValue}");
CreateWebHostBuilder(args).Build().Run();
}
}

49
src/Ombi/StartupLog.cs Normal file
View file

@ -0,0 +1,49 @@
using System;
using System.IO;
using System.Reflection;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Serilog;
using ILogger = Serilog.ILogger;
using Ombi.Helpers;
namespace Ombi
{
public class StartupLog
{
public static StoragePathSingleton StoragePath => StoragePathSingleton.Instance;
private string ContentRootPath
{
get { return Path.GetDirectoryName(Assembly.GetEntryAssembly().Location.Substring(0, Assembly.GetEntryAssembly().Location.IndexOf("bin\\"))); }
}
public StartupLog()
{
//this.config();
}
public void Config()
{
ILogger log_config = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.RollingFile(Path.Combine(StoragePath.StoragePath.IsNullOrEmpty() ? this.ContentRootPath : StoragePath.StoragePath, "Logs", "startup-{Date}.txt"))
.CreateLogger();
Log.Logger = log_config;
}
public void LogError(string str)
{
Console.WriteLine(str);
Log.Error(str);
}
public void LogInformation(string str)
{
Console.WriteLine(str);
Log.Information(str);
}
}
}