fix: don't require Redis for startup

This commit is contained in:
spiral 2022-02-04 14:53:56 -05:00
parent 8964d7f623
commit fc8f1b7cd3
No known key found for this signature in database
GPG Key ID: A6059F0CA0E1BD31
4 changed files with 22 additions and 11 deletions

View File

@ -91,14 +91,14 @@ public class Misc
var counts = await _repo.GetStats(); var counts = await _repo.GetStats();
var shards = await _shards.GetShards(); var shards = await _shards.GetShards();
var shardInfo = shards.Where(s => s.ShardId == ctx.ShardId).First(); var shardInfo = shards.Where(s => s.ShardId == ctx.ShardId).FirstOrDefault();
// todo: if we're running multiple processes, it is not useful to get the CPU/RAM usage of just the current one // 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 process = Process.GetCurrentProcess();
var memoryUsage = process.WorkingSet64; var memoryUsage = process.WorkingSet64;
var now = SystemClock.Instance.GetCurrentInstant().ToUnixTimeSeconds(); var now = SystemClock.Instance.GetCurrentInstant().ToUnixTimeSeconds();
var shardUptime = Duration.FromSeconds(now - shardInfo.LastConnection); var shardUptime = Duration.FromSeconds(now - shardInfo?.LastConnection ?? 0);
var shardTotal = shards.Count(); var shardTotal = shards.Count();
int shardClusterTotal = ctx.Cluster.Shards.Count; int shardClusterTotal = ctx.Cluster.Shards.Count;
@ -110,11 +110,11 @@ public class Misc
+ (isCluster ? $" {shardClusterTotal} in this cluster," : "") + $" {shardUpTotal} are up)" + (isCluster ? $" {shardClusterTotal} in this cluster," : "") + $" {shardUpTotal} are up)"
, true)) , true))
.Field(new Embed.Field("Shard uptime", .Field(new Embed.Field("Shard uptime",
$"{shardUptime.FormatDuration()} ({shardInfo.DisconnectionCount} disconnections)", true)) $"{shardUptime.FormatDuration()} ({shardInfo?.DisconnectionCount} disconnections)", true))
.Field(new Embed.Field("CPU usage", $"{_cpu.LastCpuMeasure:P1}", 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("Memory usage", $"{memoryUsage / 1024 / 1024} MiB", true))
.Field(new Embed.Field("Latency", .Field(new Embed.Field("Latency",
$"API: {apiLatency.TotalMilliseconds:F0} ms, shard: {shardInfo.Latency} ms", $"API: {apiLatency.TotalMilliseconds:F0} ms, shard: {shardInfo?.Latency} ms",
true)); true));
embed.Field(new("Total numbers", $" {counts.SystemCount:N0} systems," embed.Field(new("Total numbers", $" {counts.SystemCount:N0} systems,"

View File

@ -42,12 +42,14 @@ public class Init
opts.DisableTaskUnobservedTaskExceptionCapture(); opts.DisableTaskUnobservedTaskExceptionCapture();
}); });
// initialize Redis var config = services.Resolve<BotConfig>();
var coreConfig = services.Resolve<CoreConfig>(); var coreConfig = services.Resolve<CoreConfig>();
// initialize Redis
var redis = services.Resolve<RedisService>(); var redis = services.Resolve<RedisService>();
if (config.UseRedisRatelimiter)
await redis.InitAsync(coreConfig); await redis.InitAsync(coreConfig);
var config = services.Resolve<BotConfig>();
if (config.Cluster == null) if (config.Cluster == null)
{ {
// "Connect to the database" (ie. set off database migrations and ensure state) // "Connect to the database" (ie. set off database migrations and ensure state)
@ -55,6 +57,7 @@ public class Init
await services.Resolve<IDatabase>().ApplyMigrations(); await services.Resolve<IDatabase>().ApplyMigrations();
// Clear shard status from Redis // Clear shard status from Redis
if (redis.Connection != null)
await redis.Connection.GetDatabase().KeyDeleteAsync("pluralkit:shardstatus"); await redis.Connection.GetDatabase().KeyDeleteAsync("pluralkit:shardstatus");
} }

View File

@ -37,6 +37,8 @@ public class ShardInfoService
public async Task<IEnumerable<ShardState>> GetShards() public async Task<IEnumerable<ShardState>> GetShards()
{ {
if (_redis.Connection == null)
return new ShardState[] { };
var db = _redis.Connection.GetDatabase(); var db = _redis.Connection.GetDatabase();
var redisInfo = await db.HashGetAllAsync("pluralkit:shardstatus"); var redisInfo = await db.HashGetAllAsync("pluralkit:shardstatus");
return redisInfo.Select(x => Proto.Unmarshal<ShardState>(x.Value)); return redisInfo.Select(x => Proto.Unmarshal<ShardState>(x.Value));
@ -48,6 +50,12 @@ public class ShardInfoService
async Task Inner() async Task Inner()
{ {
if (_redis.Connection == null)
{
_logger.Warning("Redis is disabled, shard connection status will be unavailable.");
return;
}
var db = _redis.Connection.GetDatabase(); var db = _redis.Connection.GetDatabase();
var redisInfo = await db.HashGetAsync("pluralkit::shardstatus", shard.ShardId); var redisInfo = await db.HashGetAsync("pluralkit::shardstatus", shard.ShardId);

View File

@ -4,7 +4,7 @@ namespace PluralKit.Core;
public class RedisService public class RedisService
{ {
public ConnectionMultiplexer Connection { get; set; } public ConnectionMultiplexer? Connection { get; set; }
public async Task InitAsync(CoreConfig config) public async Task InitAsync(CoreConfig config)
{ {