Permalink
Browse files

Fix some startup errors when on first run

  • Loading branch information...
da3dsoul committed Oct 2, 2018
1 parent 5d5d89d commit 23e0a088ecee441e636efced46455cf188f41575
Showing with 40 additions and 38 deletions.
  1. +3 −1 Shoko.Server/API/Bootstrapper.cs
  2. +7 −2 Shoko.Server/ShokoServer.cs
  3. +30 −35 Shoko.UI/MainWindow.xaml.cs
@@ -132,14 +132,16 @@ private Response onError(NancyContext ctx, Exception ex)

private Response BeforeProcessing(NancyContext ctx)
{
if (!(ServerState.Instance?.ServerOnline ?? false) && !ctx.Request.Path.StartsWith("/api/init/"))
if (!(ServerState.Instance?.ServerOnline ?? false) && ctx.Request.Path.StartsWith("/api") &&
!ctx.Request.Path.StartsWith("/api/init/"))
{
return new Response
{
StatusCode = HttpStatusCode.ServiceUnavailable,
ReasonPhrase = "Server is not running"
};
}

return null;
}

@@ -652,6 +652,12 @@ public void SetupNetHosts()
throw new Exception("Failed to start all of the network hosts");
}
}

public void RestartAniDBSocket()
{
AniDBDispose();
SetupAniDBProcessor();
}

void WorkerSetupDB_DoWork(object sender, DoWorkEventArgs e)
{
@@ -667,8 +673,7 @@ void WorkerSetupDB_DoWork(object sender, DoWorkEventArgs e)

StopWatchingFiles();

AniDBDispose();
SetupAniDBProcessor();
RestartAniDBSocket();

ShokoService.CmdProcessorGeneral.Stop();
ShokoService.CmdProcessorHasher.Stop();
@@ -160,7 +160,7 @@ public MainWindow()
InitCulture();
Instance = this;

if (!ServerSettings.FirstRun)
if (!ServerSettings.FirstRun && ShokoService.AnidbProcessor.ValidAniDBCredentials())
{
logger.Info("Already been set up... Initializing DB...");
ShokoServer.RunWorkSetupDB();
@@ -202,29 +202,9 @@ private void SubscribeEvents()
{
Application.Current.Dispatcher.Invoke(action);
};
AniDBHelper.LoginFailed += (a, e) => Application.Current.Dispatcher.Invoke(() =>
{
if (AniDBLoginOpen && LoginWindow != null)
{
LoginWindow.Focus();
return;
}

LoginWindow = new InitialSetupForm();
LoginWindow.Owner = this;
LoginWindow.ShowDialog();
});
ShokoServer.Instance.LoginFormNeeded += (a, e) => Application.Current.Dispatcher.Invoke(() =>
{
if (AniDBLoginOpen && LoginWindow != null)
{
LoginWindow.Focus();
return;
}
LoginWindow = new InitialSetupForm();
LoginWindow.Owner = this;
LoginWindow.ShowDialog();
});
AniDBHelper.LoginFailed += (a, e) => Application.Current.Dispatcher.Invoke(ShowAniDBLoginDialog);
ShokoServer.Instance.LoginFormNeeded +=
(a, e) => Application.Current.Dispatcher.Invoke(ShowAniDBLoginDialog);

ServerSettings.MigrationStarted += (a, e) =>
{
@@ -240,6 +220,19 @@ private void SubscribeEvents()
DBSetupCompleted += DBSetupCompleted;
ShokoServer.Instance.DatabaseSetup += (sender, args) => ShowDatabaseSetup();
}

public void ShowAniDBLoginDialog()
{
if (AniDBLoginOpen && LoginWindow != null)
{
LoginWindow.Focus();
return;
}

LoginWindow = new InitialSetupForm {Owner = this};
LoginWindow.ShowDialog();
}

public void DoEvents()
{
DispatcherFrame frame = new DispatcherFrame();
@@ -454,14 +447,7 @@ void btnJMMDisableStartWithWindows_Click(object sender, RoutedEventArgs e)

void btnUpdateAniDBLogin_Click(object sender, RoutedEventArgs e)
{
if (AniDBLoginOpen && LoginWindow != null)
{
LoginWindow.Focus();
return;
}
LoginWindow = new InitialSetupForm();
LoginWindow.Owner = this;
LoginWindow.ShowDialog();
ShowAniDBLoginDialog();
}

void cboLanguages_SelectionChanged(object sender, SelectionChangedEventArgs e)
@@ -851,10 +837,19 @@ void MainWindow_Loaded(object sender, RoutedEventArgs e)

Utils.ClearAutoUpdateCache();

if (ServerSettings.FirstRun)
if (ServerSettings.FirstRun || !ShokoService.AnidbProcessor.ValidAniDBCredentials())
{
logger.Info("Initializing DB...");
ShokoServer.RunWorkSetupDB();
ShokoServer.Instance.RestartAniDBSocket();

if (!ShokoService.AnidbProcessor.Login()) ShowAniDBLoginDialog();

while (AniDBLoginOpen) Thread.Sleep(100);

if (ShokoService.AnidbProcessor.ValidAniDBCredentials())
{
logger.Info("Initializing DB...");
ShokoServer.RunWorkSetupDB();
}
}

ShokoServer.Instance.CheckForUpdates();

0 comments on commit 23e0a08

Please sign in to comment.