From b938e37fdd30ba6989e38ba626e3ef7502cdf246 Mon Sep 17 00:00:00 2001 From: Jetsparrow Date: Sat, 27 Apr 2024 14:54:58 +0300 Subject: [PATCH] Fix HeartMonitor db usage --- JetHerald/Services/DbContext.cs | 10 +++++++--- JetHerald/Services/HeartMonitor.cs | 9 ++++++--- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/JetHerald/Services/DbContext.cs b/JetHerald/Services/DbContext.cs index 03bdaed..7aa8b01 100644 --- a/JetHerald/Services/DbContext.cs +++ b/JetHerald/Services/DbContext.cs @@ -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> ProcessHearts() + { + using var conn = GetConnection(); + return await conn.QueryAsync("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> ProcessHearts() - => Tran.QueryAsync("CALL process_hearts();"); - public Task MarkHeartAttackReported(ulong id) => Tran.ExecuteAsync("UPDATE heartevent SET Status = 'reported' WHERE HeartEventId = @id", new { id }); diff --git a/JetHerald/Services/HeartMonitor.cs b/JetHerald/Services/HeartMonitor.cs index 4a45b9a..cf5de24 100644 --- a/JetHerald/Services/HeartMonitor.cs +++ b/JetHerald/Services/HeartMonitor.cs @@ -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}"); - await ctx.MarkHeartAttackReported(a.HeartEventId); + using (var ctx = await Db.GetContext()) + { + await ctx.MarkHeartAttackReported(a.HeartEventId); + } if (token.IsCancellationRequested) return;