jetherald/JetHerald/Services/HeartMonitor.cs
2022-01-26 01:05:05 +03:00

47 lines
1.3 KiB
C#

using System.Threading;
using Microsoft.Extensions.Hosting;
namespace JetHerald.Services;
public class HeartMonitor : BackgroundService
{
public HeartMonitor(
Db db,
JetHeraldBot herald,
ILogger<HeartMonitor> log)
{
Db = db;
Herald = herald;
Log = log;
}
Db Db { get; }
JetHeraldBot Herald { get; }
ILogger<HeartMonitor> Log { get; }
protected override async Task ExecuteAsync(CancellationToken token)
{
while (!token.IsCancellationRequested)
{
await Task.Delay(1000 * 10, token);
try
{
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}");
await Db.MarkHeartAttackReported(a.HeartEventId);
if (token.IsCancellationRequested)
return;
}
}
catch (Exception e)
{
Log.LogError(e, "Exception while checking heartbeats");
}
}
}
}