Files
raven/server/AyaNova/biz/NotifyEventProcessor.cs
2020-07-16 17:58:23 +00:00

82 lines
2.6 KiB
C#

using System;
using System.Linq;
using System.Globalization;
using System.Text;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.EntityFrameworkCore;
using AyaNova.Util;
using AyaNova.Models;
//using System.Diagnostics;
namespace AyaNova.Biz
{
//This class handles word breaking, processing keywords and searching for results
public static class NotifyEventProcessor
{
private static ILogger log = AyaNova.Util.ApplicationLogging.CreateLogger("NotifyEventProcessor");
//Add operations message
public static async Task AddOpsProblemEvent(string message, Exception ex = null)
{
if (string.IsNullOrWhiteSpace(message) && ex == null)
return;
//Log as a backup in case there is no one to notify and also for the record and support
if (ex != null)
{
log.LogError(ex, $"Ops problem notification: \"{message}\"");
message += $"\nException error: {ExceptionUtil.ExtractAllExceptionMessages(ex)}";
}
else
log.LogWarning($"Ops problem notification: \"{message}\"");
await AddEvent(new NotifyEvent() { EventType = NotifyEventType.ServerOperationsProblem, Message = message });
}
//Add event if there are subscribers
public static async Task AddEvent(NotifyEvent ev, List<string> inTags = null)
{
log.LogTrace($"AddEvent processing: [{ev.ToString()}]");
try
{
List<NotifySubscription> subs = new List<NotifySubscription>();
using (AyContext ct = AyaNova.Util.ServiceProviderProvider.DBContext)
{
//iterate subs, figure out who gets this event
//add to table for any that do
if (inTags == null || inTags.Count==0)
{
//No tags
subs = await ct.NotifySubscription.AsNoTracking().Where(z => z.EventType == ev.EventType && z.AyaType == ev.AyaType).ToListAsync();
}
else
{
//In tags
}
}
}
catch (Exception ex)
{
log.LogError(ex, $"Error adding notification event: [{ev.ToString()}]");
}
finally
{
log.LogTrace($"Notify event processed: [{ev.ToString()}]");
}
}
}//eoc
}//eons