Stop command countdown (#343)

Hopefully this works
This commit is contained in:
N1Ran
2019-09-10 17:58:50 -04:00
committed by Brant Martin
parent 5630eb14c2
commit ca99404b42

View File

@@ -29,6 +29,8 @@ namespace Torch.Commands
{ {
private static bool _restartPending = false; private static bool _restartPending = false;
private static bool _cancelRestart = false; private static bool _cancelRestart = false;
private bool _stopPending = false;
private bool _cancelStop = false;
private static readonly Logger Log = LogManager.GetCurrentClassLogger(); private static readonly Logger Log = LogManager.GetCurrentClassLogger();
[Command("whatsmyip")] [Command("whatsmyip")]
@@ -156,7 +158,23 @@ namespace Torch.Commands
[Command("stop", "Stops the server.")] [Command("stop", "Stops the server.")]
public void Stop(bool save = true) public void Stop(bool save = true)
{ {
Context.Respond("Stopping server."); if (_stopPending)
{
Context.Respond("A stop is already pending.");
return;
}
_stopPending = true;
Task.Run(() =>
{
var countdown = StopCountdown(countdownSeconds, save).GetEnumerator();
while (countdown.MoveNext())
{
Thread.Sleep(1000);
}
});
/*Context.Respond("Stopping server.");
if (save) if (save)
DoSave()?.ContinueWith((a, mod) => DoSave()?.ContinueWith((a, mod) =>
{ {
@@ -165,7 +183,7 @@ namespace Torch.Commands
torch.Stop(); torch.Stop();
}, this, TaskContinuationOptions.RunContinuationsAsynchronously); }, this, TaskContinuationOptions.RunContinuationsAsynchronously);
else else
Context.Torch.Stop(); Context.Torch.Stop();*/
} }
[Command("restart", "Restarts the server.")] [Command("restart", "Restarts the server.")]
@@ -204,6 +222,69 @@ namespace Torch.Commands
else else
Context.Respond("A restart is not pending."); Context.Respond("A restart is not pending.");
} }
[Command("stop cancel", "Cancel a pending stop.")]
public void CancelStop()
{
if (_restartPending)
_cancelStop = true;
else
Context.Respond("Server Stop is not pending.");
}
private IEnumerable StopCountdown(int countdown, bool save)
{
for (var i = countdown; i >= 0; i--)
{
if (_cancelStop)
{
Context.Torch.CurrentSession.Managers.GetManager<IChatManagerClient>()
.SendMessageAsSelf($"Stop cancelled.");
_stopPending = false;
_cancelStop = false;
yield break;
}
if (i >= 60 && i % 60 == 0)
{
Context.Torch.CurrentSession.Managers.GetManager<IChatManagerClient>()
.SendMessageAsSelf($"Stopping server in {i / 60} minute{Pluralize(i / 60)}.");
yield return null;
}
else if (i > 0)
{
if (i < 11)
Context.Torch.CurrentSession.Managers.GetManager<IChatManagerClient>()
.SendMessageAsSelf($"Stopping server in {i} second{Pluralize(i)}.");
yield return null;
}
else
{
if (save)
{
Log.Info("Saving game before stop.");
Context.Torch.CurrentSession.Managers.GetManager<IChatManagerClient>()
.SendMessageAsSelf($"Saving game before stop.");
DoSave()?.ContinueWith((a, mod) =>
{
ITorchBase torch = (mod as CommandModule)?.Context?.Torch;
Debug.Assert(torch != null);
torch.Stop();
}, this, TaskContinuationOptions.RunContinuationsAsynchronously);
}
else
{
Log.Info("Stopping server.");
Context.Torch.Invoke(() => Context.Torch.Stop());
}
yield break;
}
}
}
private IEnumerable RestartCountdown(int countdown, bool save) private IEnumerable RestartCountdown(int countdown, bool save)
{ {