public BackLogItem Notify(BackLogItem bli) { try { _emailClient.Send(bli.To, bli.From, bli.Subject, bli.Message); bli.AttemptMessage = "Sent."; bli.AttempNumber = bli.AttempNumber + 1; bli.WasSent = true; bli.SentDate = DateTime.Now; } catch(Exception ex) { bli.AttemptMessage = "Failed with message: '" + ex.Message + "'"; bli.AttempNumber = bli.AttempNumber + 1; } // If this doesn't exist then add it. If it does then update it. if (!BackLog.Exists(x => x.BackLogItemUniqueId.Equals(bli.BackLogItemUniqueId))) { BackLog.Add(bli); } else { // update the item in our backlog for (var i = 0; i < BackLog.Count; i++) { if (BackLog[i].BackLogItemUniqueId != bli.BackLogItemUniqueId) continue; BackLog[i] = bli; break; } } return bli; }
/// <summary> /// Whenever the dispatcher publishes an event we create a message for it and stick it on the queue. Then we see if anyone is requesting notification and we create a backlog entry for them to be processed at a later date. Likely by a scheduled task or explict request. /// </summary> /// <param name="response"></param> public void ProcessEvent(Response response) { if (!Subscribers.ContainsKey(response.EventName.Name) || !Subscribers[response.EventName.Name].ContainsKey(response.Type)) return; // Bail early if there are no subscribers. // Run through all of the subscribers for this publisher and generate a backlog item for them. foreach (var s in Subscribers[response.EventName.Name][response.Type]) { string subject; string body; if (EmailTemplateLookup.ContainsKey(response.EventName.ToString()) && EmailTemplateLookup[response.EventName.ToString()].ContainsKey(response.Request.Entity.GetType())) { var temp = EmailTemplateLookup[response.EventName.ToString()][response.Request.Entity.GetType()]; subject = temp.GetSubject(response.Entity); body = temp.GetBody(response.Entity); } else { subject = "Automated Notification: You were selected to be notified when the following event occurced: " + response.EventName.Name; body = response.Message; } // Add the backlog item var bli = new BackLogItem { AttempNumber = 0, AttemptMessage = "Created.", BackLogItemUniqueId = Guid.NewGuid(), NextAttempTime = null, RequestId = response.RequestId, RequestUniqueKey = response.RequestUniqueKey, ResponseId = response.ResponseId, ResponseUniqueKey = response.ResponseUniqueKey, SentDate = null, UserId = s, WasSent = false, Subject = subject, Message = body }; BackLog.Add(bli); } }