feat: store shard status in Redis
This commit is contained in:
@@ -9,30 +9,6 @@ namespace PluralKit.API;
|
||||
|
||||
public static class APIJsonExt
|
||||
{
|
||||
public static JArray ToJSON(this IEnumerable<PKShardInfo> shards)
|
||||
{
|
||||
var o = new JArray();
|
||||
|
||||
foreach (var shard in shards)
|
||||
{
|
||||
var s = new JObject();
|
||||
s.Add("id", shard.Id);
|
||||
|
||||
if (shard.Status == PKShardInfo.ShardStatus.Down)
|
||||
s.Add("status", "down");
|
||||
else
|
||||
s.Add("status", "up");
|
||||
|
||||
s.Add("ping", shard.Ping);
|
||||
s.Add("last_heartbeat", shard.LastHeartbeat.ToString());
|
||||
s.Add("last_connection", shard.LastConnection.ToString());
|
||||
|
||||
o.Add(s);
|
||||
}
|
||||
|
||||
return o;
|
||||
}
|
||||
|
||||
public static JObject ToJson(this ModelRepository.Counts counts)
|
||||
{
|
||||
var o = new JObject();
|
||||
|
||||
@@ -17,19 +17,53 @@ namespace PluralKit.API;
|
||||
[Route("private")]
|
||||
public class PrivateController: PKControllerBase
|
||||
{
|
||||
public PrivateController(IServiceProvider svc) : base(svc) { }
|
||||
private readonly RedisService _redis;
|
||||
public PrivateController(IServiceProvider svc) : base(svc)
|
||||
{
|
||||
_redis = svc.GetRequiredService<RedisService>();
|
||||
}
|
||||
|
||||
[HttpGet("meta")]
|
||||
public async Task<ActionResult<JObject>> Meta()
|
||||
{
|
||||
var shards = await _repo.GetShards();
|
||||
var db = _redis.Connection.GetDatabase();
|
||||
var redisInfo = await db.HashGetAllAsync("pluralkit:shardstatus");
|
||||
var shards = redisInfo.Select(x => Proto.Unmarshal<ShardState>(x.Value));
|
||||
|
||||
var stats = await _repo.GetStats();
|
||||
|
||||
var o = new JObject();
|
||||
o.Add("shards", shards.ToJSON());
|
||||
o.Add("shards", shards.ToJson());
|
||||
o.Add("stats", stats.ToJson());
|
||||
o.Add("version", BuildInfoService.Version);
|
||||
|
||||
return Ok(o);
|
||||
}
|
||||
}
|
||||
|
||||
public static class PrivateJsonExt
|
||||
{
|
||||
public static JArray ToJson(this IEnumerable<ShardState> shards)
|
||||
{
|
||||
var o = new JArray();
|
||||
|
||||
foreach (var shard in shards)
|
||||
{
|
||||
var s = new JObject();
|
||||
s.Add("id", shard.ShardId);
|
||||
|
||||
if (!shard.Up)
|
||||
s.Add("status", "down");
|
||||
else
|
||||
s.Add("status", "up");
|
||||
|
||||
s.Add("ping", shard.Latency);
|
||||
s.Add("last_heartbeat", shard.LastHeartbeat.ToString());
|
||||
s.Add("last_connection", shard.LastConnection.ToString());
|
||||
|
||||
o.Add(s);
|
||||
}
|
||||
|
||||
return o;
|
||||
}
|
||||
}
|
||||
@@ -28,6 +28,8 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Google.Protobuf" Version="3.13.0"/>
|
||||
<PackageReference Include="Grpc.Tools" Version="2.37.0" PrivateAssets="All"/>
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.1.0"/>
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="4.2.0"/>
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer" Version="4.2.0"/>
|
||||
@@ -40,4 +42,7 @@
|
||||
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="5.6.3"/>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Protobuf Include="../proto/discord.proto" GrpcServices="Client" Link="Protos/discord.proto"/>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
|
||||
@@ -12,7 +12,10 @@ public class Program
|
||||
{
|
||||
InitUtils.InitStatic();
|
||||
await BuildInfoService.LoadVersion();
|
||||
await CreateHostBuilder(args).Build().RunAsync();
|
||||
var host = CreateHostBuilder(args).Build();
|
||||
var config = host.Services.GetRequiredService<CoreConfig>();
|
||||
await host.Services.GetRequiredService<RedisService>().InitAsync(config);
|
||||
await host.RunAsync();
|
||||
}
|
||||
|
||||
public static IHostBuilder CreateHostBuilder(string[] args) =>
|
||||
|
||||
@@ -2,6 +2,22 @@
|
||||
"version": 1,
|
||||
"dependencies": {
|
||||
"net6.0": {
|
||||
"Google.Protobuf": {
|
||||
"type": "Direct",
|
||||
"requested": "[3.13.0, )",
|
||||
"resolved": "3.13.0",
|
||||
"contentHash": "/6VgKCh0P59x/rYsBkCvkUanF0TeUYzwV9hzLIWgt23QRBaKHoxaaMkidEWhKibLR88c3PVCXyyrx9Xlb+Ne6w==",
|
||||
"dependencies": {
|
||||
"System.Memory": "4.5.2",
|
||||
"System.Runtime.CompilerServices.Unsafe": "4.5.2"
|
||||
}
|
||||
},
|
||||
"Grpc.Tools": {
|
||||
"type": "Direct",
|
||||
"requested": "[2.37.0, )",
|
||||
"resolved": "2.37.0",
|
||||
"contentHash": "cud/urkbw3QoQ8+kNeCy2YI0sHrh7td/1cZkVbH6hDLIXX7zzmJbV/KjYSiqiYtflQf+S5mJPLzDQWScN/QdDg=="
|
||||
},
|
||||
"Microsoft.AspNetCore.Mvc.NewtonsoftJson": {
|
||||
"type": "Direct",
|
||||
"requested": "[3.1.0, )",
|
||||
@@ -1167,6 +1183,11 @@
|
||||
"System.Threading": "4.3.0"
|
||||
}
|
||||
},
|
||||
"System.Memory": {
|
||||
"type": "Transitive",
|
||||
"resolved": "4.5.2",
|
||||
"contentHash": "fvq1GNmUFwbKv+aLVYYdgu/+gc8Nu9oFujOxIjPrsf+meis9JBzTPDL6aP/eeGOz9yPj6rRLUbOjKMpsMEWpNg=="
|
||||
},
|
||||
"System.Net.Http": {
|
||||
"type": "Transitive",
|
||||
"resolved": "4.3.0",
|
||||
@@ -1685,6 +1706,7 @@
|
||||
"Autofac.Extensions.DependencyInjection": "7.1.0",
|
||||
"Dapper": "2.0.35",
|
||||
"Dapper.Contrib": "2.0.35",
|
||||
"Google.Protobuf": "3.13.0",
|
||||
"Microsoft.Extensions.Caching.Memory": "3.1.10",
|
||||
"Microsoft.Extensions.Configuration": "3.1.10",
|
||||
"Microsoft.Extensions.Configuration.Binder": "3.1.10",
|
||||
|
||||
Reference in New Issue
Block a user