Permalink
Browse files

Some Proper Startup logging

  • Loading branch information...
da3dsoul committed Mar 11, 2018
1 parent c94dadb commit cf38bacc4ba1ee72dc052f73e9d23a112a43d775
@@ -18,7 +18,7 @@ namespace Shoko.Server.Databases
public abstract class BaseDatabase<T>
{
// ReSharper disable once StaticMemberInGenericType
protected static Logger Logger = LogManager.GetCurrentClassLogger();
protected static readonly Logger Logger = LogManager.GetCurrentClassLogger();

public string GetDatabaseBackupName(int version)
{
@@ -139,9 +139,11 @@ public void ExecuteDatabaseFixes()
string message = cmd.CommandName;
if (message.Length > 42)
message = message.Substring(0, 42) + "...";
ServerState.Instance.CurrentSetupStatus = ServerState.Instance.CurrentSetupStatus =
message = ServerState.Instance.CurrentSetupStatus =
Commons.Properties.Resources.Database_ApplySchema + cmd.Version + "." + cmd.Revision +
" - " + message;
Logger.Info($"Starting Server: {message}");
ServerState.Instance.CurrentSetupStatus = message;

cmd.DatabaseFix();
AddVersion(cmd.Version.ToString(), cmd.Revision.ToString(), cmd.CommandName);
@@ -170,9 +172,10 @@ public void AddFix(DatabaseCommand cmd)
string message = cmd.CommandName;
if (message.Length > 42)
message = message.Substring(0, 42) + "...";
ServerState.Instance.CurrentSetupStatus = ServerState.Instance.CurrentSetupStatus =
message = ServerState.Instance.CurrentSetupStatus =
Commons.Properties.Resources.Database_ApplySchema + cmd.Version + "." + cmd.Revision +
" - " + message;
ServerState.Instance.CurrentSetupStatus = message;

switch (cmd.Type)
{
@@ -204,20 +207,30 @@ public void PopulateInitialData()
{
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(ServerSettings.Culture);

ServerState.Instance.CurrentSetupStatus = Commons.Properties.Resources.Database_Users;
string message = Commons.Properties.Resources.Database_Users;

Logger.Info($"Starting Server: {message}");
ServerState.Instance.CurrentSetupStatus = message;
CreateInitialUsers();

ServerState.Instance.CurrentSetupStatus = Commons.Properties.Resources.Database_Filters;
message = Commons.Properties.Resources.Database_Filters;
Logger.Info($"Starting Server: {message}");
ServerState.Instance.CurrentSetupStatus = message;
CreateInitialGroupFilters();

ServerState.Instance.CurrentSetupStatus = Commons.Properties.Resources.Database_LockFilters;
message = Commons.Properties.Resources.Database_LockFilters;
Logger.Info($"Starting Server: {message}");
ServerState.Instance.CurrentSetupStatus = message;
CreateOrVerifyLockedFilters();


ServerState.Instance.CurrentSetupStatus = Commons.Properties.Resources.Database_RenameScripts;
message = Commons.Properties.Resources.Database_RenameScripts;
Logger.Info($"Starting Server: {message}");
ServerState.Instance.CurrentSetupStatus = message;
CreateInitialRenameScript();

ServerState.Instance.CurrentSetupStatus = Commons.Properties.Resources.Database_CustomTags;
message = Commons.Properties.Resources.Database_CustomTags;
Logger.Info($"Starting Server: {message}");
ServerState.Instance.CurrentSetupStatus = message;
CreateInitialCustomTags();
}

@@ -476,7 +489,7 @@ public void CreateInitialCustomTags()
{
// group filters

if (RepoFactory.CustomTag.GetAll().Count() > 0) return;
if (RepoFactory.CustomTag.GetAll().Any()) return;

Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(ServerSettings.Culture);

@@ -1,13 +1,9 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using NHibernate;
using NLog;
using Shoko.Commons.Properties;
using Shoko.Server.Repositories;

namespace Shoko.Server.Databases
@@ -19,7 +15,7 @@ public static class DatabaseFactory

private static readonly object sessionLock = new object();

private static ISessionFactory sessionFactory = null;
private static ISessionFactory sessionFactory;

public static ISessionFactory SessionFactory
{
@@ -77,29 +73,35 @@ public static bool InitDB(out string errorMessage)
}
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(ServerSettings.Culture);

DatabaseFactory.CloseSessionFactory();
ServerState.Instance.CurrentSetupStatus = Commons.Properties.Resources.Database_Initializing;
ISessionFactory temp = DatabaseFactory.SessionFactory;
CloseSessionFactory();

string message = Resources.Database_Initializing;
logger.Info($"Starting Server: {message}");
ServerState.Instance.CurrentSetupStatus = message;

Instance.Init();
int version = Instance.GetDatabaseVersion();
if (version > Instance.RequiredVersion)
{
ServerState.Instance.CurrentSetupStatus =
Commons.Properties.Resources.Database_NotSupportedVersion;
errorMessage = Commons.Properties.Resources.Database_NotSupportedVersion;
message = Resources.Database_NotSupportedVersion;
logger.Info($"Starting Server: {message}");
ServerState.Instance.CurrentSetupStatus = message;
errorMessage = Resources.Database_NotSupportedVersion;
return false;
}
if (version != 0 && version < Instance.RequiredVersion)
{
ServerState.Instance.CurrentSetupStatus = Commons.Properties.Resources.Database_Backup;
message = Resources.Database_Backup;
logger.Info($"Starting Server: {message}");
ServerState.Instance.CurrentSetupStatus = message;
Instance.BackupDatabase(Instance.GetDatabaseBackupName(version));
}
try
{
logger.Info($"{Instance.GetType()}Instance.CreateAndUpdateSchema()");
logger.Info($"Startomg Server: {Instance.GetType()} - CreateAndUpdateSchema()");
Instance.CreateAndUpdateSchema();

logger.Info($"RepoFactory.Init()");
logger.Info("Starting Server: RepoFactory.Init()");
RepoFactory.Init();
Instance.ExecuteDatabaseFixes();
Instance.PopulateInitialData();
@@ -112,18 +114,16 @@ public static bool InitDB(out string errorMessage)
logger.Error(ex, ex.ToString());
Utils.ShowErrorMessage("Database Error :\n\r " + ex +
"\n\rNotify developers about this error, it will be logged in your logs", "Database Error");
ServerState.Instance.CurrentSetupStatus =
Commons.Properties.Resources.Server_DatabaseFail;
ServerState.Instance.CurrentSetupStatus = Resources.Server_DatabaseFail;
errorMessage = "Database Error :\n\r " + ex +
"\n\rNotify developers about this error, it will be logged in your logs";
return false;
}
if (ex is TimeoutException)
{
logger.Error(ex, $"Database Timeout: {ex}");
ServerState.Instance.CurrentSetupStatus =
Commons.Properties.Resources.Server_DatabaseTimeOut;
errorMessage = Commons.Properties.Resources.Server_DatabaseTimeOut + "\n\r" + ex;
ServerState.Instance.CurrentSetupStatus = Resources.Server_DatabaseTimeOut;
errorMessage = Resources.Server_DatabaseTimeOut + "\n\r" + ex;
return false;
}
// throw to the outer try/catch
@@ -137,7 +137,7 @@ public static bool InitDB(out string errorMessage)
{
errorMessage = $"Could not init database: {ex}";
logger.Error(ex, errorMessage);
ServerState.Instance.CurrentSetupStatus = Commons.Properties.Resources.Server_DatabaseFail;
ServerState.Instance.CurrentSetupStatus = Resources.Server_DatabaseFail;
return false;
}
}
@@ -440,6 +440,14 @@ private static byte GetByte(this MediaInfoLib.MediaInfo mi, StreamKind kind, int
return 0;
}

private static long GetLong(this MediaInfoLib.MediaInfo mi, StreamKind kind, int number, string par)
{
string dta = mi.Get(kind, number, par);
if (long.TryParse(dta, out long val))
return val;
return 0;
}

private static float GetFloat(this MediaInfoLib.MediaInfo mi, StreamKind kind, int number, string par)
{
string dta = mi.Get(kind, number, par);
@@ -476,21 +484,26 @@ public static Media Convert(string filename, IFile file)
minstance = new MediaInfoLib.MediaInfo();
if (minstance.Open(filename) == 0) return; //it's a boolean response.
Stream VideoStream = null;

m.Duration = p.Duration = minstance.GetLong(StreamKind.General, 0, "Duration");
p.Size = minstance.GetLong(StreamKind.General, 0, "FileSize");

int brate = minstance.GetInt(StreamKind.General, 0, "BitRate");
if (brate != 0)
m.Bitrate = (int) Math.Round(brate / 1000F);

int chaptercount = minstance.GetInt(StreamKind.General, 0, "MenuCount");
m.Chaptered = chaptercount > 0;

int video_count = minstance.GetInt(StreamKind.General, 0, "VideoCount");
int audio_count = minstance.GetInt(StreamKind.General, 0, "AudioCount");
int text_count = minstance.GetInt(StreamKind.General, 0, "TextCount");
if (int.TryParse(minstance.Get(StreamKind.General, 0, "MenuCount"), out int chaptercount))
m.Chaptered = chaptercount > 0;
m.Duration = p.Duration = long.Parse(minstance.Get(StreamKind.General, 0, "Duration"));

m.Container = p.Container = TranslateContainer(minstance.Get(StreamKind.General, 0, "Format"));
string codid = minstance.Get(StreamKind.General, 0, "CodecID");
if (!string.IsNullOrEmpty(codid) && (codid.Trim().ToLower() == "qt"))
m.Container = p.Container = "mov";

int brate = minstance.GetInt(StreamKind.General, 0, "BitRate");
if (brate != 0)
m.Bitrate = (int) Math.Round(brate / 1000F);
p.Size = long.Parse(minstance.Get(StreamKind.General, 0, "FileSize"));
List<Stream> streams = new List<Stream>();
byte iidx = 0;
if (video_count > 0)
@@ -146,6 +146,7 @@ public static void PostInit()
// Update Contracts if necessary
try
{
logger.Info("Starting Server: RepoFactory.PostInit()");
CachedRepositories.ForEach(repo =>
{
ServerState.Instance.CurrentSetupStatus = string.Format(
@@ -60,7 +60,14 @@ public bool ServerStarting
public string CurrentSetupStatus
{
get => currentSetupStatus;
set => this.SetField(() => currentSetupStatus, value);
set
{
if (!value.Equals(currentSetupStatus))
{
logger.Trace($"Starting Server: {value}");
}
this.SetField(() => currentSetupStatus, value);
}
}

private bool databaseIsSQLite = false;
@@ -566,6 +566,7 @@ void WorkerSetupDB_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs

void WorkerSetupDB_ReportProgress()
{
logger.Info("Starting Server: Complete!");
ServerInfo.Instance.RefreshImportFolders();
ServerInfo.Instance.RefreshCloudAccounts();
ServerState.Instance.CurrentSetupStatus = Resources.Server_Complete;
@@ -641,7 +642,7 @@ private static void CloudWatchTimer_Elapsed(object sender, ElapsedEventArgs e)

public void SetupNetHosts()
{
logger.Info("Initializing Hosts...");
logger.Info("Initializing Web Hosts...");
ServerState.Instance.CurrentSetupStatus = Resources.Server_InitializingHosts;
bool started = true;
started &= NetPermissionWrapper(StartNancyHost);

0 comments on commit cf38bac

Please sign in to comment.