feat: store shard status in Redis

This commit is contained in:
spiral
2022-01-22 03:52:52 -05:00
parent 22faa47d00
commit 0419ced0d2
18 changed files with 602 additions and 183 deletions

View File

@@ -21,17 +21,16 @@ public class Misc
private readonly IDiscordCache _cache;
private readonly CpuStatService _cpu;
private readonly IMetrics _metrics;
private readonly ModelRepository _repo;
private readonly ShardInfoService _shards;
private readonly ModelRepository _repo;
public Misc(BotConfig botConfig, IMetrics metrics, CpuStatService cpu, ShardInfoService shards,
ModelRepository repo, IDiscordCache cache)
public Misc(BotConfig botConfig, IMetrics metrics, CpuStatService cpu, ModelRepository repo, ShardInfoService shards, IDiscordCache cache)
{
_botConfig = botConfig;
_metrics = metrics;
_cpu = cpu;
_shards = shards;
_repo = repo;
_shards = shards;
_cache = cache;
}
@@ -64,6 +63,8 @@ public class Misc
var embed = new EmbedBuilder();
// todo: these will be inaccurate when the bot is actually multi-process
var messagesReceived = _metrics.Snapshot.GetForContext("Bot").Meters
.FirstOrDefault(m => m.MultidimensionalName == BotMetrics.MessagesReceived.Name)?.Value;
if (messagesReceived != null)
@@ -85,38 +86,52 @@ public class Misc
$"{commandsRun.OneMinuteRate * 60:F1}/m ({commandsRun.FifteenMinuteRate * 60:F1}/m over 15m)",
true));
var isCluster = _botConfig.Cluster != null && _botConfig.Cluster.TotalShards != ctx.Cluster.Shards.Count;
var counts = await _repo.GetStats();
var shards = await _shards.GetShards();
var shardId = ctx.ShardId;
var shardTotal = ctx.Cluster.Shards.Count;
var shardUpTotal = _shards.Shards.Where(x => x.Connected).Count();
var shardInfo = _shards.GetShardInfo(ctx.ShardId);
var shardInfo = shards.Where(s => s.ShardId == ctx.ShardId).First();
// todo: if we're running multiple processes, it is not useful to get the CPU/RAM usage of just the current one
var process = Process.GetCurrentProcess();
var memoryUsage = process.WorkingSet64;
var now = SystemClock.Instance.GetCurrentInstant();
var shardUptime = now - shardInfo.LastConnectionTime;
var now = SystemClock.Instance.GetCurrentInstant().ToUnixTimeSeconds();
var shardUptime = Duration.FromSeconds(now - shardInfo.LastConnection);
var shardTotal = shards.Count();
int shardClusterTotal = ctx.Cluster.Shards.Count;
var shardUpTotal = shards.Where(x => x.Up && now - x.LastConnection > 60).Count();
embed
.Field(new Embed.Field("Current shard",
$"Shard #{shardId} (of {shardTotal} total, {shardUpTotal} are up)", true))
$"Shard #{ctx.ShardId} (of {shardTotal} total,"
+ (isCluster ? $" {shardClusterTotal} in this cluster," : "") + $" {shardUpTotal} are up)"
, true))
.Field(new Embed.Field("Shard uptime",
$"{shardUptime.FormatDuration()} ({shardInfo.DisconnectionCount} disconnections)", true))
.Field(new Embed.Field("CPU usage", $"{_cpu.LastCpuMeasure:P1}", true))
.Field(new Embed.Field("Memory usage", $"{memoryUsage / 1024 / 1024} MiB", true))
.Field(new Embed.Field("Latency",
$"API: {apiLatency.TotalMilliseconds:F0} ms, shard: {shardInfo.ShardLatency.Milliseconds} ms",
true))
.Field(new Embed.Field("Total numbers", $" {counts.SystemCount:N0} systems,"
+ $" {counts.MemberCount:N0} members,"
+ $" {counts.GroupCount:N0} groups,"
+ $" {counts.SwitchCount:N0} switches,"
+ $" {counts.MessageCount:N0} messages"))
.Timestamp(process.StartTime.ToString("O"))
.Footer(new Embed.EmbedFooter(
$"PluralKit {BuildInfoService.Version} • https://github.com/xSke/PluralKit • Last restarted: "));
;
$"API: {apiLatency.TotalMilliseconds:F0} ms, shard: {shardInfo.Latency} ms",
true));
embed.Field(new("Total numbers", $" {counts.SystemCount:N0} systems,"
+ $" {counts.MemberCount:N0} members,"
+ $" {counts.GroupCount:N0} groups,"
+ $" {counts.SwitchCount:N0} switches,"
+ $" {counts.MessageCount:N0} messages"));
embed
.Footer(new(String.Join(" \u2022 ", new[] {
$"PluralKit {BuildInfoService.Version}",
(isCluster ? $"Cluster {_botConfig.Cluster.NodeIndex}" : ""),
"https://github.com/xSke/PluralKit",
"Last restarted:",
})))
.Timestamp(process.StartTime.ToString("O"));
await ctx.Rest.EditMessage(msg.ChannelId, msg.Id,
new MessageEditRequest { Content = "", Embed = embed.Build() });
}