Skip to content

Commit

Permalink
Fix startup caching. Clean up. Throw exceptions to actually halt startup
Browse files Browse the repository at this point in the history
  • Loading branch information
da3dsoul committed Jul 30, 2017
1 parent 1053c75 commit 30c912b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 21 deletions.
16 changes: 8 additions & 8 deletions Shoko.Server/Databases/DatabaseFactory.cs
Expand Up @@ -111,17 +111,17 @@ public static bool InitDB()
MessageBoxButton.OK, MessageBoxImage.Error);
ServerState.Instance.CurrentSetupStatus =
Commons.Properties.Resources.Server_DatabaseFail;
return false;
}
else
if (ex is TimeoutException)
{
if (ex is TimeoutException)
{
logger.Error(ex, "Database TimeOut: " + ex.ToString());
ServerState.Instance.CurrentSetupStatus =
Commons.Properties.Resources.Server_DatabaseTimeOut;
}
logger.Error(ex, "Database TimeOut: " + ex.ToString());
ServerState.Instance.CurrentSetupStatus =
Commons.Properties.Resources.Server_DatabaseTimeOut;
return false;
}
return false;
// throw to the outer try/catch
throw;
}

return true;
Expand Down
34 changes: 21 additions & 13 deletions Shoko.Server/Repositories/RepoFactory.cs
Expand Up @@ -147,19 +147,7 @@ public static ICachedRepository CreateCachedRepository(ICachedRepository repo)

public static void Init()
{
try
{
foreach (var repo in CachedRepositories)
{
Task task = new Task(() => repo.Populate());
// don't wait longer than 3 minutes
if (Task.WhenAny(task, Task.Delay(180000)).Result != task) throw new TimeoutException($"{repo.GetType()} took too long to cache.");
}
}
catch (Exception exception)
{
logger.Error($"There was an error starting the Database Factory - Caching: {exception}");
}
InitCaches();

// Update Contracts if necessary
try
Expand All @@ -176,10 +164,30 @@ public static void Init()
catch (Exception e)
{
logger.Error($"There was an error starting the Database Factory - Regenerating: {e}");
throw;
}
CleanUpMemory();
}

public static void InitCaches()
{
try
{
foreach (var repo in CachedRepositories)
{
Task task = Task.Run(() => repo.Populate());

// don't wait longer than 3 minutes
if (!task.Wait(180000)) throw new TimeoutException($"{repo.GetType()} took too long to cache.");
}
}
catch (Exception exception)
{
logger.Error($"There was an error starting the Database Factory - Caching: {exception}");
throw;
}
}

public static void CleanUpMemory()
{
AniDB_Anime.GetAll().ForEach(a => a.CollectContractMemory());
Expand Down

0 comments on commit 30c912b

Please sign in to comment.