help
Root Question Message
private Thread? _workerThread;
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
Logger.LogInformation("Monitoring Event Triggering Start");
_workerThread = new Thread(() =>
{
while (!stoppingToken.IsCancellationRequested)
{
if (EventQueue.Count == 0) continue;
var monitoringEvent = EventQueue.Dequeue();
using (var scope = ServiceProvider.CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
db.MonitoringEvents.Add(monitoringEvent);
db.SaveChanges();
}
// TODO Trigger events, notifications
Logger.LogInformation($"{monitoringEvent.DateTime} Rule {monitoringEvent.Message} triggered");
}
});
_workerThread.IsBackground = true;
_workerThread.Start();
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
if (LongRunningTask is not null) throw new InvalidOperationException("Already running");
Logger.LogInformation("Monitoring Event Triggering Start");
LongRunningTask = Task.Run(async () =>
{
while (!stoppingToken.IsCancellationRequested)
{
if (EventQueue.Count == 0) continue;
var monitoringEvent = EventQueue.Dequeue();
using (var scope = ServiceProvider.CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
await db.MonitoringEvents.AddAsync(monitoringEvent, stoppingToken);
await db.SaveChangesAsync(stoppingToken);
}
// TODO Trigger events, notifications
Logger.LogInformation($"{monitoringEvent.DateTime} Rule {monitoringEvent.Message} triggered");
}
},
stoppingToken
);
Logger.LogInformation("Task is running");
}
StartAsync
on the MonitoringEventTriggeringService
, I'm not sure if Host.cs has any conditions for production vs development but I doubt thatif (hostedService is BackgroundService backgroundService)
in _hostedService
and then next step is nowherehostedService is BackgroundService backgroundServic
fails?is T
just checks if the instance is of that type, if so it would run the code inside the bracesdotnet --version
6.0.402
Linux 5.15.0-52-generic #58~20.04.1-Ubuntu SMP Thu Oct 13 13:09:46 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux