Fix HeartMonitor db usage

This commit is contained in:
Jetsparrow 2024-04-27 14:54:58 +03:00
parent f08efc040e
commit b938e37fdd
2 changed files with 13 additions and 6 deletions

View File

@ -2,6 +2,7 @@
using System.Threading; using System.Threading;
using System.ComponentModel; using System.ComponentModel;
using MySql.Data.MySqlClient; using MySql.Data.MySqlClient;
using Dapper;
using Dapper.Transaction; using Dapper.Transaction;
using JetHerald.Options; using JetHerald.Options;
using JetHerald.Contracts; using JetHerald.Contracts;
@ -27,6 +28,12 @@ public class Db
var tran = await conn.BeginTransactionAsync(lvl, token); var tran = await conn.BeginTransactionAsync(lvl, token);
return new DbContext(tran); 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 public class DbContext : IDisposable
@ -220,9 +227,6 @@ public class DbContext : IDisposable
@"CALL report_heartbeat(@topicId, @heart, @timeoutSeconds);", @"CALL report_heartbeat(@topicId, @heart, @timeoutSeconds);",
new { topicId, heart, timeoutSeconds }); new { topicId, heart, timeoutSeconds });
public Task<IEnumerable<HeartEvent>> ProcessHearts()
=> Tran.QueryAsync<HeartEvent>("CALL process_hearts();");
public Task MarkHeartAttackReported(ulong id) public Task MarkHeartAttackReported(ulong id)
=> Tran.ExecuteAsync("UPDATE heartevent SET Status = 'reported' WHERE HeartEventId = @id", new { id }); => Tran.ExecuteAsync("UPDATE heartevent SET Status = 'reported' WHERE HeartEventId = @id", new { id });

View File

@ -24,15 +24,18 @@ public class HeartMonitor : BackgroundService
await Task.Delay(1000 * 10, token); await Task.Delay(1000 * 10, token);
try try
{ {
using var ctx = await Db.GetContext(); var attacks = await Db.ProcessHearts();
var attacks = await ctx.ProcessHearts();
foreach (var a in attacks) foreach (var a in attacks)
{ {
await Herald.BroadcastMessageRaw( await Herald.BroadcastMessageRaw(
a.TopicId, a.TopicId,
$"!{a.Description}!:\nHeart \"{a.Heart}\" stopped beating at {a.CreateTs:O}"); $"!{a.Description}!:\nHeart \"{a.Heart}\" stopped beating at {a.CreateTs:O}");
await ctx.MarkHeartAttackReported(a.HeartEventId); using (var ctx = await Db.GetContext())
{
await ctx.MarkHeartAttackReported(a.HeartEventId);
}
if (token.IsCancellationRequested) if (token.IsCancellationRequested)
return; return;