Store stard status in the database
This commit is contained in:
@@ -19,7 +19,7 @@ namespace PluralKit.Core
|
||||
internal class Database: IDatabase
|
||||
{
|
||||
private const string RootPath = "PluralKit.Core.Database"; // "resource path" root for SQL files
|
||||
private const int TargetSchemaVersion = 13;
|
||||
private const int TargetSchemaVersion = 14;
|
||||
|
||||
private readonly CoreConfig _config;
|
||||
private readonly ILogger _logger;
|
||||
|
15
PluralKit.Core/Database/Migrations/14.sql
Normal file
15
PluralKit.Core/Database/Migrations/14.sql
Normal file
@@ -0,0 +1,15 @@
|
||||
-- SCHEMA VERSION 14: 2021-06-10 --
|
||||
-- Add shard status table --
|
||||
|
||||
create table shards (
|
||||
id int not null primary key,
|
||||
|
||||
-- 0 = down, 1 = up
|
||||
status smallint not null default 0,
|
||||
|
||||
ping float,
|
||||
last_heartbeat timestamptz,
|
||||
last_connection timestamptz
|
||||
);
|
||||
|
||||
update info set schema_version = 14;
|
30
PluralKit.Core/Database/Repository/ModelRepository.Shards.cs
Normal file
30
PluralKit.Core/Database/Repository/ModelRepository.Shards.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using Dapper;
|
||||
|
||||
using NodaTime;
|
||||
|
||||
namespace PluralKit.Core
|
||||
{
|
||||
public partial class ModelRepository
|
||||
{
|
||||
public Task<IEnumerable<PKShardInfo>> GetShards(IPKConnection conn) =>
|
||||
conn.QueryAsync<PKShardInfo>("select * from shards");
|
||||
|
||||
public Task SetShardStatus(IPKConnection conn, int shard, PKShardInfo.ShardStatus status) =>
|
||||
conn.ExecuteAsync(
|
||||
"insert into shards (id, status) values (@Id, @Status) on conflict (id) do update set status = @Status",
|
||||
new {Id = shard, Status = status});
|
||||
|
||||
public Task RegisterShardHeartbeat(IPKConnection conn, int shard, Duration ping) =>
|
||||
conn.ExecuteAsync(
|
||||
"insert into shards (id, last_heartbeat, ping) values (@Id, now(), @Ping) on conflict (id) do update set last_heartbeat = now(), ping = @Ping",
|
||||
new {Id = shard, Ping = ping.TotalSeconds});
|
||||
|
||||
public Task RegisterShardConnection(IPKConnection conn, int shard) =>
|
||||
conn.ExecuteAsync(
|
||||
"insert into shards (id, last_connection) values (@Id, now()) on conflict (id) do update set last_connection = now()",
|
||||
new {Id = shard});
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user