Fixup instance watchdog handling

This commit is contained in:
Jordan Brown
2018-09-08 16:06:49 -04:00
parent 372cb75c28
commit 2f1a52d931
3 changed files with 24 additions and 6 deletions
@@ -79,11 +79,21 @@ namespace Tgstation.Server.Host.Components
{
Models.WatchdogReattachInformation result = null;
await databaseContextFactory.UseContext(async (db) =>
result = await db.Instances.Where(x => x.Id == metadata.Id).Select(x => x.WatchdogReattachInformation).FirstOrDefaultAsync(cancellationToken).ConfigureAwait(false)
).ConfigureAwait(false);
{
var instance = await db.Instances.Where(x => x.Id == metadata.Id)
.Include(x => x.WatchdogReattachInformation).ThenInclude(x => x.Alpha)
.Include(x => x.WatchdogReattachInformation).ThenInclude(x => x.Alpha)
.FirstOrDefaultAsync(cancellationToken).ConfigureAwait(false);
result = instance.WatchdogReattachInformation;
if (result == default)
return;
instance.WatchdogReattachInformation = null;
db.WatchdogReattachInformations.Remove(result);
await db.Save(cancellationToken).ConfigureAwait(false);
}).ConfigureAwait(false);
if (result == default)
throw new JobException("Unable to load reattach information!");
return null;
var bravoDmbTask = dmbFactory.FromCompileJob(result.Bravo.CompileJob, cancellationToken);
return new WatchdogReattachInformation(result, await dmbFactory.FromCompileJob(result.Alpha.CompileJob, cancellationToken).ConfigureAwait(false), await bravoDmbTask.ConfigureAwait(false));
@@ -361,7 +361,7 @@ namespace Tgstation.Server.Host.Components.Watchdog
reattachInformation.Dmb = null;
released = true;
Dispose();
Dmb.KeepAlive();
tmpProvider.KeepAlive();
reattachInformation.Dmb = tmpProvider;
return reattachInformation;
}
@@ -625,12 +625,16 @@ namespace Tgstation.Server.Host.Components.Watchdog
return null;
}
var reattachInfoTask = doReattach ? reattachInfoHandler.Load(cancellationToken) : Task.FromResult<WatchdogReattachInformation>(null);
Task chatTask;
//this is necessary, the monitor could be in it's sleep loop trying to restart
if (startMonitor && await StopMonitor().ConfigureAwait(false))
chatTask = chat.SendWatchdogMessage("Automatic retry sequence cancelled by manual launch. Restarting...", cancellationToken);
else if (announce)
chatTask = chat.SendWatchdogMessage("Starting...", cancellationToken);
{
var info = await reattachInfoTask.ConfigureAwait(false);
chatTask = chat.SendWatchdogMessage(info == null ? "Starting..." : "Reattaching...", cancellationToken);
}
else
chatTask = Task.CompletedTask;
//start both servers
@@ -641,7 +645,7 @@ namespace Tgstation.Server.Host.Components.Watchdog
if (alphaServer != null || bravoServer != null)
throw new InvalidOperationException("Entered LaunchNoLock with one or more of the servers not being null!");
var reattachInfo = doReattach ? await reattachInfoHandler.Load(cancellationToken).ConfigureAwait(false) : null;
var reattachInfo = await reattachInfoTask.ConfigureAwait(false);
var doesntNeedNewDmb = doReattach && reattachInfo.Alpha != null && reattachInfo.Bravo != null;
var dmbToUse = doesntNeedNewDmb ? null : dmbFactory.LockNextDmb(2);
@@ -798,6 +802,9 @@ namespace Tgstation.Server.Host.Components.Watchdog
{
if (releaseServers && Running)
{
var chatTask = chat.SendWatchdogMessage("Detaching...", cancellationToken);
await StopMonitor().ConfigureAwait(false);
var reattachInformation = new WatchdogReattachInformation
{
AlphaIsActive = AlphaIsActive
@@ -805,6 +812,7 @@ namespace Tgstation.Server.Host.Components.Watchdog
reattachInformation.Alpha = alphaServer?.Release();
reattachInformation.Bravo = bravoServer?.Release();
await reattachInfoHandler.Save(reattachInformation, cancellationToken).ConfigureAwait(false);
await chatTask.ConfigureAwait(false);
}
await Terminate(false, cancellationToken).ConfigureAwait(false);
}