feat: set shard status to offline when starting up

This commit is contained in:
spiral 2021-11-28 20:50:13 -05:00
parent d2f29bbb51
commit d195c80d92
No known key found for this signature in database
GPG Key ID: A6059F0CA0E1BD31
2 changed files with 8 additions and 0 deletions

View File

@ -51,6 +51,12 @@ public class Init
var bot = services.Resolve<Bot>(); var bot = services.Resolve<Bot>();
bot.Init(); bot.Init();
// if we're running single-process, clear any existing shard status from the database
var config = services.Resolve<BotConfig>();
var repo = services.Resolve<ModelRepository>();
if (config.Cluster == null)
await repo.ClearShardStatus();
// Start the Discord shards themselves (handlers already set up) // Start the Discord shards themselves (handlers already set up)
logger.Information("Connecting to Discord"); logger.Information("Connecting to Discord");
await StartCluster(services); await StartCluster(services);

View File

@ -14,6 +14,8 @@ public partial class ModelRepository
"insert into shards (id, status) values (@Id, @Status) on conflict (id) do update set status = @Status", "insert into shards (id, status) values (@Id, @Status) on conflict (id) do update set status = @Status",
new { Id = shard, Status = status }); new { Id = shard, Status = status });
public Task ClearShardStatus() => _db.Execute(conn => conn.ExecuteAsync("update shards set status = 0"));
public Task RegisterShardHeartbeat(IPKConnection conn, int shard, Duration ping) => public Task RegisterShardHeartbeat(IPKConnection conn, int shard, Duration ping) =>
conn.ExecuteAsync( conn.ExecuteAsync(
"insert into shards (id, last_heartbeat, ping) values (@Id, now(), @Ping) on conflict (id) do update set last_heartbeat = now(), ping = @Ping", "insert into shards (id, last_heartbeat, ping) values (@Id, now(), @Ping) on conflict (id) do update set last_heartbeat = now(), ping = @Ping",