2019-04-21 17:27:21 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Concurrent;
|
2019-12-24 15:26:25 +00:00
|
|
|
|
using System.Diagnostics;
|
2020-03-21 10:16:03 +00:00
|
|
|
|
using System.Linq;
|
2019-04-21 17:27:21 +00:00
|
|
|
|
using System.Threading.Tasks;
|
2019-04-21 17:38:20 +00:00
|
|
|
|
using CompatBot.Commands;
|
2020-03-21 10:16:03 +00:00
|
|
|
|
using CompatBot.Database.Providers;
|
2020-09-01 10:05:27 +00:00
|
|
|
|
using CompatBot.EventHandlers;
|
2019-04-21 17:27:21 +00:00
|
|
|
|
using DSharpPlus;
|
2021-04-23 15:37:58 +00:00
|
|
|
|
using DSharpPlus.EventArgs;
|
2020-07-03 08:57:27 +00:00
|
|
|
|
using Microsoft.ApplicationInsights;
|
2020-09-01 10:05:27 +00:00
|
|
|
|
using NLog;
|
2019-04-21 17:27:21 +00:00
|
|
|
|
|
2022-06-29 19:59:46 +00:00
|
|
|
|
namespace CompatBot;
|
|
|
|
|
|
|
|
|
|
internal static class Watchdog
|
2019-04-21 17:27:21 +00:00
|
|
|
|
{
|
2022-06-29 19:59:46 +00:00
|
|
|
|
public static readonly ConcurrentQueue<DateTime> DisconnectTimestamps = new();
|
|
|
|
|
public static readonly Stopwatch TimeSinceLastIncomingMessage = Stopwatch.StartNew();
|
|
|
|
|
private static bool IsOk => DisconnectTimestamps.IsEmpty && TimeSinceLastIncomingMessage.Elapsed < Config.IncomingMessageCheckIntervalInMin;
|
|
|
|
|
private static DiscordClient? discordClient;
|
2019-04-21 17:27:21 +00:00
|
|
|
|
|
2022-06-29 19:59:46 +00:00
|
|
|
|
public static async Task Watch(DiscordClient client)
|
|
|
|
|
{
|
|
|
|
|
discordClient = client;
|
|
|
|
|
do
|
2019-04-21 17:27:21 +00:00
|
|
|
|
{
|
2022-06-29 19:59:46 +00:00
|
|
|
|
await Task.Delay(Config.SocketDisconnectCheckIntervalInSec, Config.Cts.Token).ConfigureAwait(false);
|
|
|
|
|
foreach (var sudoer in ModProvider.Mods.Values.Where(m => m.Sudoer))
|
2019-04-21 17:27:21 +00:00
|
|
|
|
{
|
2022-06-29 19:59:46 +00:00
|
|
|
|
var user = await client.GetUserAsync(sudoer.DiscordId).ConfigureAwait(false);
|
|
|
|
|
if (user?.Presence?.Activity?.CustomStatus?.Name is string cmd && cmd.StartsWith("restart"))
|
2020-03-21 10:16:03 +00:00
|
|
|
|
{
|
2022-06-29 19:59:46 +00:00
|
|
|
|
var instance = cmd.Split(' ', StringSplitOptions.RemoveEmptyEntries).LastOrDefault();
|
|
|
|
|
if (ulong.TryParse(instance, out var botId) && botId == client.CurrentUser.Id)
|
2020-03-21 10:16:03 +00:00
|
|
|
|
{
|
2022-06-29 19:59:46 +00:00
|
|
|
|
Config.Log.Warn($"Found request to restart on {user.Username}#{user.Discriminator}'s custom status");
|
|
|
|
|
Sudo.Bot.Restart(Program.InvalidChannelId, $"Restarted by request from {user.Username}#{user.Discriminator}'s custom status");
|
2020-03-21 10:16:03 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-06-29 19:59:46 +00:00
|
|
|
|
}
|
2020-03-21 10:16:03 +00:00
|
|
|
|
|
2022-06-29 19:59:46 +00:00
|
|
|
|
if (IsOk)
|
|
|
|
|
continue;
|
2020-01-03 10:15:19 +00:00
|
|
|
|
|
2022-06-29 19:59:46 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Config.TelemetryClient?.TrackEvent("socket-deadlock-potential");
|
|
|
|
|
Config.Log.Warn("Potential socket deadlock detected, reconnecting...");
|
|
|
|
|
await client.ReconnectAsync(true).ConfigureAwait(false);
|
|
|
|
|
await Task.Delay(Config.SocketDisconnectCheckIntervalInSec, Config.Cts.Token).ConfigureAwait(false);
|
|
|
|
|
if (IsOk)
|
2019-04-21 17:27:21 +00:00
|
|
|
|
{
|
2022-06-29 19:59:46 +00:00
|
|
|
|
Config.Log.Info("Looks like we're back in business");
|
|
|
|
|
continue;
|
2019-04-21 17:27:21 +00:00
|
|
|
|
}
|
2020-07-03 08:57:27 +00:00
|
|
|
|
|
2022-06-29 19:59:46 +00:00
|
|
|
|
Config.TelemetryClient?.TrackEvent("socket-deadlock-for-sure");
|
|
|
|
|
Config.Log.Error("Hard reconnect failed, restarting...");
|
|
|
|
|
Sudo.Bot.Restart(Program.InvalidChannelId, $@"Restarted to reset potential socket deadlock (last incoming message event: {TimeSinceLastIncomingMessage.Elapsed:h\:mm\:ss} ago)");
|
2020-11-23 15:42:17 +00:00
|
|
|
|
}
|
2022-06-29 19:59:46 +00:00
|
|
|
|
catch (Exception e)
|
2020-09-01 10:05:27 +00:00
|
|
|
|
{
|
2022-06-29 19:59:46 +00:00
|
|
|
|
Config.Log.Error(e);
|
2020-09-01 10:05:27 +00:00
|
|
|
|
}
|
2022-06-29 19:59:46 +00:00
|
|
|
|
} while (!Config.Cts.IsCancellationRequested);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void OnLogHandler(string level, string message)
|
|
|
|
|
{
|
|
|
|
|
if (level == nameof(LogLevel.Info))
|
|
|
|
|
{
|
|
|
|
|
if (message.Contains("Session resumed"))
|
|
|
|
|
DisconnectTimestamps.Clear();
|
|
|
|
|
}
|
|
|
|
|
else if (level == nameof(LogLevel.Warn))
|
|
|
|
|
{
|
|
|
|
|
if (message.Contains("Dispatch:PRESENCES_REPLACE")
|
|
|
|
|
&& discordClient != null)
|
|
|
|
|
BotStatusMonitor.RefreshAsync(discordClient).ConfigureAwait(false).GetAwaiter().GetResult();
|
|
|
|
|
else if (message.Contains("Pre-emptive ratelimit triggered"))
|
|
|
|
|
Config.TelemetryClient?.TrackEvent("preemptive-rate-limit");
|
2020-09-01 10:05:27 +00:00
|
|
|
|
}
|
2022-06-29 19:59:46 +00:00
|
|
|
|
else if (level == nameof(LogLevel.Error))
|
|
|
|
|
{
|
|
|
|
|
if (message.Contains("System.Threading.Tasks.TaskSchedulerException")
|
|
|
|
|
|| message.Contains("System.OutOfMemoryException"))
|
|
|
|
|
Sudo.Bot.RestartNoSaving();
|
|
|
|
|
}
|
|
|
|
|
else if (level == nameof(LogLevel.Fatal))
|
|
|
|
|
{
|
|
|
|
|
if (message.Contains("Socket connection terminated")
|
|
|
|
|
|| message.Contains("heartbeats were skipped. Issuing reconnect."))
|
|
|
|
|
DisconnectTimestamps.Enqueue(DateTime.UtcNow);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-09-01 10:05:27 +00:00
|
|
|
|
|
2022-06-29 19:59:46 +00:00
|
|
|
|
public static Task OnMessageCreated(DiscordClient c, MessageCreateEventArgs args)
|
|
|
|
|
{
|
|
|
|
|
if (Config.TelemetryClient is TelemetryClient tc)
|
2021-04-23 15:37:58 +00:00
|
|
|
|
{
|
2022-06-29 19:59:46 +00:00
|
|
|
|
var userToBotDelay = (DateTime.UtcNow - args.Message.Timestamp.UtcDateTime).TotalMilliseconds;
|
|
|
|
|
tc.TrackMetric("gw-latency", c.Ping);
|
|
|
|
|
tc.TrackMetric("user-to-bot-latency", userToBotDelay);
|
|
|
|
|
tc.TrackMetric("time-since-last-incoming-message", TimeSinceLastIncomingMessage.ElapsedMilliseconds);
|
2021-04-23 15:37:58 +00:00
|
|
|
|
}
|
2022-06-29 19:59:46 +00:00
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
2021-04-23 15:37:58 +00:00
|
|
|
|
|
2022-06-29 19:59:46 +00:00
|
|
|
|
public static async Task SendMetrics(DiscordClient client)
|
|
|
|
|
{
|
|
|
|
|
do
|
2020-07-03 08:57:27 +00:00
|
|
|
|
{
|
2022-06-29 19:59:46 +00:00
|
|
|
|
await Task.Delay(Config.MetricsIntervalInSec).ConfigureAwait(false);
|
|
|
|
|
var gcMemInfo = GC.GetGCMemoryInfo();
|
|
|
|
|
using var process = Process.GetCurrentProcess();
|
|
|
|
|
if (Config.TelemetryClient is not TelemetryClient tc)
|
|
|
|
|
continue;
|
2020-11-14 10:25:20 +00:00
|
|
|
|
|
2022-06-29 19:59:46 +00:00
|
|
|
|
tc.TrackMetric("gw-latency", client.Ping);
|
|
|
|
|
tc.TrackMetric("memory-gc-total", gcMemInfo.HeapSizeBytes);
|
|
|
|
|
tc.TrackMetric("memory-gc-load", gcMemInfo.MemoryLoadBytes);
|
|
|
|
|
tc.TrackMetric("memory-gc-committed", gcMemInfo.TotalCommittedBytes);
|
|
|
|
|
tc.TrackMetric("memory-process-private", process.PrivateMemorySize64);
|
|
|
|
|
tc.TrackMetric("memory-process-ws", process.WorkingSet64);
|
|
|
|
|
tc.TrackMetric("github-limit-remaining", GithubClient.Client.RateLimitRemaining);
|
|
|
|
|
tc.Flush();
|
2021-05-30 10:05:38 +00:00
|
|
|
|
|
2022-06-29 19:59:46 +00:00
|
|
|
|
if (gcMemInfo.TotalCommittedBytes > 3_000_000_000)
|
|
|
|
|
Sudo.Bot.Restart(Program.InvalidChannelId, "GC Memory overcommitment");
|
|
|
|
|
} while (!Config.Cts.IsCancellationRequested);
|
|
|
|
|
}
|
2020-09-09 17:17:54 +00:00
|
|
|
|
|
2022-06-29 19:59:46 +00:00
|
|
|
|
public static async Task CheckGCStats()
|
|
|
|
|
{
|
|
|
|
|
do
|
2020-09-09 17:17:54 +00:00
|
|
|
|
{
|
2022-06-29 19:59:46 +00:00
|
|
|
|
var gcMemInfo = GC.GetGCMemoryInfo();
|
|
|
|
|
using var process = Process.GetCurrentProcess();
|
|
|
|
|
Config.Log.Info($"Process memory stats:\n" +
|
|
|
|
|
$"GC Heap: {gcMemInfo.HeapSizeBytes}\n" +
|
|
|
|
|
$"Private: {process.PrivateMemorySize64}\n" +
|
|
|
|
|
$"Working set: {process.WorkingSet64}\n" +
|
|
|
|
|
$"Virtual: {process.VirtualMemorySize64}\n" +
|
|
|
|
|
$"Paged: {process.PagedMemorySize64}\n" +
|
|
|
|
|
$"Paged system: {process.PagedSystemMemorySize64}\n" +
|
|
|
|
|
$"Non-paged system: {process.NonpagedSystemMemorySize64}");
|
|
|
|
|
await Task.Delay(TimeSpan.FromHours(1)).ConfigureAwait(false);
|
|
|
|
|
} while (!Config.Cts.IsCancellationRequested);
|
2019-04-21 17:27:21 +00:00
|
|
|
|
}
|
2022-06-29 19:59:46 +00:00
|
|
|
|
}
|