pass restart info through database to workaround docker lifecycle management

This commit is contained in:
13xforever 2019-11-07 21:48:30 +05:00
parent 34439d1ee2
commit eb9a34b9e9
2 changed files with 38 additions and 3 deletions

View File

@ -1,5 +1,6 @@
using System;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
@ -175,6 +176,22 @@ namespace CompatBot.Commands
internal static void Restart(ulong channelId)
{
if (SandboxDetector.Detect() == SandboxType.Docker)
{
Config.Log.Info($"Saving channelId {channelId} into settings...");
using (var db = new BotDb())
{
var ch = db.BotState.FirstOrDefault(k => k.Key == "bot-restart-channel");
if (ch is null)
{
ch = new BotState {Key = "bot-restart-channel", Value = channelId.ToString()};
db.BotState.Add(ch);
}
else
ch.Value = channelId.ToString();
db.SaveChanges();
}
}
Config.Log.Info("Restarting...");
using (var self = new Process
{

View File

@ -253,9 +253,27 @@ namespace CompatBot
throw;
}
if (args.LastOrDefault() is string strCh && ulong.TryParse(strCh, out var channelId))
ulong? channelId = null;
if (SandboxDetector.Detect() == SandboxType.Docker)
{
Config.Log.Info($"Found channelId: {strCh} (parsed as {channelId})");
using (var db = new BotDb())
{
var chState = db.BotState.FirstOrDefault(k => k.Key == "bot-restart-channel");
if (chState != null)
{
if (ulong.TryParse(chState.Value, out var ch))
channelId = ch;
db.BotState.Remove(chState);
db.SaveChanges();
}
}
}
if (args.LastOrDefault() is string strCh && ulong.TryParse(strCh, out var chId))
channelId = chId;
if (channelId.HasValue)
{
Config.Log.Info($"Found channelId {channelId}");
DiscordChannel channel;
if (channelId == InvalidChannelId)
{
@ -264,7 +282,7 @@ namespace CompatBot
}
else
{
channel = await client.GetChannelAsync(channelId).ConfigureAwait(false);
channel = await client.GetChannelAsync(channelId.Value).ConfigureAwait(false);
await channel.SendMessageAsync("Bot is up and running").ConfigureAwait(false);
}
}