attempt to restart the bot on catastrophic failures

This commit is contained in:
13xforever 2018-11-12 13:28:52 +05:00
parent ed1ad561e4
commit dd7a5bcba2
2 changed files with 33 additions and 16 deletions

View File

@ -67,19 +67,7 @@ namespace CompatBot.Commands
await ctx.SendAutosplitMessageAsync("```" + stdout + "```").ConfigureAwait(false);
}
await ctx.RespondAsync("Restarting...").ConfigureAwait(false);
using (var self = new Process
{
#if DEBUG
StartInfo = new ProcessStartInfo("dotnet", $"run -- {Config.Token} {ctx.Channel.Id}")
#else
StartInfo = new ProcessStartInfo("dotnet", $"run -c Release -- {Config.Token} {ctx.Channel.Id}")
#endif
})
{
self.Start();
Config.Cts.Cancel();
return;
}
Restart(ctx.Channel.Id);
}
catch (Exception e)
{
@ -105,6 +93,23 @@ namespace CompatBot.Commands
Config.Cts.Cancel();
}
internal static void Restart(ulong channelId)
{
using (var self = new Process
{
#if DEBUG
StartInfo = new ProcessStartInfo("dotnet", $"run -- {Config.Token} {channelId}")
#else
StartInfo = new ProcessStartInfo("dotnet", $"run -c Release -- {Config.Token} {channelId}")
#endif
})
{
self.Start();
Config.Cts.Cancel();
return;
}
}
}
}
}

View File

@ -10,6 +10,7 @@ using CompatBot.EventHandlers;
using CompatBot.ThumbScrapper;
using DSharpPlus;
using DSharpPlus.CommandsNext;
using DSharpPlus.Entities;
using Microsoft.Extensions.DependencyInjection;
namespace CompatBot
@ -18,6 +19,7 @@ namespace CompatBot
{
private static readonly SemaphoreSlim InstanceCheck = new SemaphoreSlim(0, 1);
private static readonly SemaphoreSlim ShutdownCheck = new SemaphoreSlim(0, 1);
private const ulong InvalidChannelId = 13;
internal static async Task Main(string[] args)
{
@ -208,8 +210,17 @@ namespace CompatBot
if (args.Length > 1 && ulong.TryParse(args[1], out var channelId))
{
Config.Log.Info($"Found channelId: {args[1]}");
var channel = await client.GetChannelAsync(channelId).ConfigureAwait(false);
await channel.SendMessageAsync("Bot is up and running").ConfigureAwait(false);
DiscordChannel channel;
if (channelId == InvalidChannelId)
{
channel = await client.GetChannelAsync(Config.ThumbnailSpamId).ConfigureAwait(false);
await channel.SendMessageAsync("Bot has suffered some catastrophic failure and was restarted").ConfigureAwait(false);
}
else
{
channel = await client.GetChannelAsync(channelId).ConfigureAwait(false);
await channel.SendMessageAsync("Bot is up and running").ConfigureAwait(false);
}
}
Config.Log.Debug("Running RPC3 update check thread");
@ -232,7 +243,8 @@ namespace CompatBot
}
catch(Exception e)
{
Config.Log.Fatal(e);
Config.Log.Fatal(e, $"Experienced catastrofic failure, attempting to restart...");
Sudo.Bot.Restart(InvalidChannelId);
}
finally
{