mirror of
https://github.com/Jetsparrow/jetherald.git
synced 2026-01-20 23:56:08 +03:00
Fix HeartMonitor db usage
This commit is contained in:
parent
f08efc040e
commit
b938e37fdd
@ -2,6 +2,7 @@
|
||||
using System.Threading;
|
||||
using System.ComponentModel;
|
||||
using MySql.Data.MySqlClient;
|
||||
using Dapper;
|
||||
using Dapper.Transaction;
|
||||
using JetHerald.Options;
|
||||
using JetHerald.Contracts;
|
||||
@ -27,6 +28,12 @@ public class Db
|
||||
var tran = await conn.BeginTransactionAsync(lvl, token);
|
||||
return new DbContext(tran);
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<HeartEvent>> ProcessHearts()
|
||||
{
|
||||
using var conn = GetConnection();
|
||||
return await conn.QueryAsync<HeartEvent>("CALL process_hearts();");
|
||||
}
|
||||
}
|
||||
|
||||
public class DbContext : IDisposable
|
||||
@ -220,9 +227,6 @@ public class DbContext : IDisposable
|
||||
@"CALL report_heartbeat(@topicId, @heart, @timeoutSeconds);",
|
||||
new { topicId, heart, timeoutSeconds });
|
||||
|
||||
public Task<IEnumerable<HeartEvent>> ProcessHearts()
|
||||
=> Tran.QueryAsync<HeartEvent>("CALL process_hearts();");
|
||||
|
||||
public Task MarkHeartAttackReported(ulong id)
|
||||
=> Tran.ExecuteAsync("UPDATE heartevent SET Status = 'reported' WHERE HeartEventId = @id", new { id });
|
||||
|
||||
|
||||
@ -24,15 +24,18 @@ public class HeartMonitor : BackgroundService
|
||||
await Task.Delay(1000 * 10, token);
|
||||
try
|
||||
{
|
||||
using var ctx = await Db.GetContext();
|
||||
var attacks = await ctx.ProcessHearts();
|
||||
var attacks = await Db.ProcessHearts();
|
||||
|
||||
foreach (var a in attacks)
|
||||
{
|
||||
await Herald.BroadcastMessageRaw(
|
||||
a.TopicId,
|
||||
$"!{a.Description}!:\nHeart \"{a.Heart}\" stopped beating at {a.CreateTs:O}");
|
||||
|
||||
using (var ctx = await Db.GetContext())
|
||||
{
|
||||
await ctx.MarkHeartAttackReported(a.HeartEventId);
|
||||
}
|
||||
|
||||
if (token.IsCancellationRequested)
|
||||
return;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user