internal static async Task FlushLogs(List <MessagePayload> messagePayloads) { try { List <LogEvent> logEvents = new List <LogEvent>(); messagePayloads.ForEach(x => logEvents.AddRange(x.LogEvents.Where(y => IsLogInsertible(y.LogMessage)))); if (logEvents.Count > 0) { await Resources.GetInstance().LogEventCollection.InsertManyAsync(logEvents); } var filterBuilder = Builders <LogEvent> .Filter; var updateBuilder = Builders <LogEvent> .Update; var writeBuilder = new List <WriteModel <LogEvent> >(); foreach (MessagePayload messagePayload in messagePayloads) { if (messagePayload.InvitationLogEvents.Count > 0) { var filter = filterBuilder.Eq(x => x.Id, messagePayload.Invitation.Id); var update = updateBuilder.PushEach(x => x.Events, messagePayload.InvitationLogEvents).Set(x => x.Updated, DateTime.UtcNow); writeBuilder.Add(new UpdateOneModel <LogEvent>(filter, update)); } } if (writeBuilder.Count > 0) { await Resources.GetInstance().LogEventCollection.BulkWriteAsync(writeBuilder); } } catch (Exception ex) { await FlushLogs(new List <LogEvent> { CreateLogEvent(null, IRDLM.InternalException(ex)) }); } }
internal static async Task DeleteBulkMessagePayloads(List <DB_MessagePayload> dB_MessagePayloads) { try { List <string> docIds = dB_MessagePayloads.Select(x => x.Id).ToList(); await Resources.GetInstance().BulkMessagePayloadCollection.DeleteManyAsync(x => docIds.Contains(x.Id)); } catch (Exception ex) { await FlushLogs(new List <LogEvent> { CreateLogEvent(null, IRDLM.InternalException(ex)) }); } }
internal static async Task InsertBulkMessagePayload(MessagePayload messagePayload) { try { DB_MessagePayload dB_MessagePayload = new DB_MessagePayload(messagePayload); await Resources.GetInstance().BulkMessagePayloadCollection.InsertOneAsync(dB_MessagePayload); } catch (Exception ex) { await FlushLogs(new List <LogEvent> { CreateLogEvent(null, IRDLM.InternalException(ex)) }); } }
internal static async Task UpdateBulkMessagePayloads(List <DB_MessagePayload> dB_MessagePayloads) { try { List <string> docIds = dB_MessagePayloads.Select(x => x.Id).ToList(); var filter = Builders <DB_MessagePayload> .Filter.In(x => x.Id, docIds); var update = Builders <DB_MessagePayload> .Update.Set(x => x.Status, "Processing"); await Resources.GetInstance().BulkMessagePayloadCollection.UpdateManyAsync(filter, update); } catch (Exception ex) { await FlushLogs(new List <LogEvent> { CreateLogEvent(null, IRDLM.InternalException(ex)) }); } }
internal static async Task <List <DB_MessagePayload> > ReadBulkMessagePayloads() { try { return(await Resources.GetInstance().BulkMessagePayloadCollection .Find(x => x.BulkVendorName == Resources.GetInstance().BulkVendorName.ToLower() && x.Status == "Ready") .Limit(Resources.GetInstance().BulkReadSize) .ToListAsync()); } catch (Exception ex) { await FlushLogs(new List <LogEvent> { CreateLogEvent(null, IRDLM.InternalException(ex)) }); return(new List <DB_MessagePayload>()); } }
public static Resources CreateSingleton(string mongoDbConnectionString, string databaseName, int logLevel = 5, Dictionary <string, Func <IDispatchVendor> > additionalDispatchCreatorStrategies = default, string bulkVendorName = "sparkpost", int bulkReadSize = 10000, string surveyBaseDomain = "nps.bz", string unsubscribeUrl = "https://cx.getcloudcherry.com/l/unsub/?token=") { try { _instance = new Resources(mongoDbConnectionString, databaseName, logLevel, additionalDispatchCreatorStrategies, bulkVendorName, bulkReadSize, surveyBaseDomain, unsubscribeUrl); } catch (Exception ex) { Console.WriteLine(ex.ToString()); Utils.FlushLogs(new List <LogEvent> { Utils.CreateLogEvent(null, IRDLM.InternalException(ex)) }).GetAwaiter().GetResult(); throw ex; } return(_instance); }
/// <summary> /// Performs Hash-Look-Ups for PII Data if the received /// QueueData-Object's Subject/Text-Body/Html-Body /// utilizes WXM Tag-Substitution. /// </summary> /// <param name="queueData"></param> public static void PerformLookUps(QueueData queueData) { try { string matchString = @"\$(\w+)\*\|(.*?)\|\*"; //Subject if (!string.IsNullOrWhiteSpace(queueData.Subject)) { StringBuilder newSubject = new StringBuilder(queueData.Subject); foreach (Match m in Regex.Matches(queueData.Subject, matchString, RegexOptions.Multiline)) { string qid = m.Groups[1].Value; string defaultValue = m.Groups[2].Value; if (queueData.MappedValue.ContainsKey(qid)) { newSubject.Replace(m.Groups[0].Value, queueData.MappedValue[qid]); } else { newSubject.Replace(m.Groups[0].Value, defaultValue); } } queueData.Subject = newSubject.ToString(); } //HTML Body if (!string.IsNullOrWhiteSpace(queueData.HTMLBody)) { StringBuilder newHtmlBody = new StringBuilder(queueData.HTMLBody); foreach (Match m in Regex.Matches(queueData.HTMLBody, matchString, RegexOptions.Multiline)) { string qid = m.Groups[1].Value; string defaultValue = m.Groups[2].Value; if (queueData.MappedValue.ContainsKey(qid)) { newHtmlBody.Replace(m.Groups[0].Value, queueData.MappedValue[qid]); } else { newHtmlBody.Replace(m.Groups[0].Value, defaultValue); } } queueData.HTMLBody = newHtmlBody.ToString(); } //Text Body if (!string.IsNullOrWhiteSpace(queueData.TextBody)) { StringBuilder newTextBody = new StringBuilder(queueData.TextBody); foreach (Match m in Regex.Matches(queueData.TextBody, matchString, RegexOptions.Multiline)) { string qid = m.Groups[1].Value; string defaultValue = m.Groups[2].Value; if (queueData.MappedValue.ContainsKey(qid)) { newTextBody.Replace(m.Groups[0].Value, queueData.MappedValue[qid]); } else { newTextBody.Replace(m.Groups[0].Value, defaultValue); } } queueData.TextBody = newTextBody.ToString(); } } catch (Exception ex) { FlushLogs(new List <LogEvent> { CreateLogEvent(null, IRDLM.InternalException(ex)) }).GetAwaiter().GetResult(); } }
public async Task ProcessSingleMessage(QueueData queueData) { MessagePayload messagePayload = new MessagePayload(queueData); try { messagePayload.Validate(); if (!messagePayload.IsProcessable) { return; } messagePayload.ConfigureChannel(); if (messagePayload.IsEmailDelivery == null) { return; } await messagePayload.ConfigureUserData(); if (!messagePayload.IsUserDataLogEventConfigured) { return; } try { messagePayload.PrepareForHashLookUps(); messagePayload.ConfigureVendor(); if (!messagePayload.IsVendorConfigured) { return; } messagePayload.ConfigureVendorFlag(); if (messagePayload.Vendor.IsBulkVendor) { await Utils.InsertBulkMessagePayload(messagePayload); return; } SingleDispatch singleDispatch = new SingleDispatch(); singleDispatch = new SingleDispatch { MessagePayload = messagePayload }; singleDispatch.ConfigureDispatchVendor(); if (!singleDispatch.IsDispatcConfigured) { return; } await singleDispatch.DispatchReadyVendor.RunAsync(singleDispatch.MessagePayload); } catch (Exception ex) { messagePayload.LogEvents.Add(Utils.CreateLogEvent(messagePayload.QueueData, IRDLM.InternalException(ex))); messagePayload.InvitationLogEvents.Add(Utils.CreateInvitationLogEvent(EventAction.DispatchUnsuccessful, messagePayload.IsEmailDelivery.Value ? EventChannel.Email : EventChannel.SMS, messagePayload.QueueData, IRDLM.InternalException(ex))); } } catch (Exception ex) { messagePayload.LogEvents.Add(Utils.CreateLogEvent(queueData, IRDLM.InternalException(ex))); } finally { await Utils.FlushLogs(new List <MessagePayload> { messagePayload }); } }
public async Task ProcessMultipleMessage(bool isLate) { List <LogEvent> logEvents = new List <LogEvent>(); logEvents.Add(Utils.CreateLogEvent(null, IRDLM.TimeTriggerStart)); if (isLate) { logEvents.Add(Utils.CreateLogEvent(null, IRDLM.TimeTriggerRunningLate)); } List <MessagePayload> messagePayloads = new List <MessagePayload>(); List <DB_MessagePayload> dB_MessagePayloads = await Utils.ReadBulkMessagePayloads(); if (dB_MessagePayloads.Count > 0) { try { await Utils.UpdateBulkMessagePayloads(dB_MessagePayloads); Dictionary <string, List <MessagePayload> > ListOfMessagePayloadsByTemplateId = new Dictionary <string, List <MessagePayload> >(); foreach (DB_MessagePayload dB_MessagePayload in dB_MessagePayloads) { MessagePayload messagePayload = JsonConvert.DeserializeObject <MessagePayload>(dB_MessagePayload.MessagePayload); messagePayloads.Add(messagePayload); messagePayload.LogEvents.Add(Utils.CreateLogEvent(messagePayload.QueueData, IRDLM.ReadFromDB)); if (!ListOfMessagePayloadsByTemplateId.ContainsKey(messagePayload.QueueData.TemplateId)) { ListOfMessagePayloadsByTemplateId.Add(messagePayload.QueueData.TemplateId, new List <MessagePayload>()); } ListOfMessagePayloadsByTemplateId[messagePayload.QueueData.TemplateId].Add(messagePayload); } foreach (KeyValuePair <string, List <MessagePayload> > messagePayloadsByTemplateId in ListOfMessagePayloadsByTemplateId) { BulkDispatch bulkDispatch = null; try { bulkDispatch = new BulkDispatch { MessagePayloads = messagePayloadsByTemplateId.Value }; bulkDispatch.ConfigureDispatchVendor(); if (!bulkDispatch.IsDispatchConfigured) { continue; } await bulkDispatch.DispatchReadyVendor.RunAsync(bulkDispatch.MessagePayloads); } catch (Exception ex) { bulkDispatch.MessagePayloads.ForEach(x => x.LogEvents.Add(Utils.CreateLogEvent(x.QueueData, IRDLM.InternalException(ex)))); bulkDispatch.MessagePayloads.ForEach(x => x.InvitationLogEvents.Add(Utils.CreateInvitationLogEvent(EventAction.DispatchUnsuccessful, x.IsEmailDelivery.Value ? EventChannel.Email : EventChannel.SMS, x.QueueData, IRDLM.InternalException(ex)))); } } } catch (Exception ex) { logEvents.Add(Utils.CreateLogEvent(null, IRDLM.InternalException(ex))); } finally { await Utils.DeleteBulkMessagePayloads(dB_MessagePayloads); await Utils.FlushLogs(messagePayloads); } } logEvents.Add(Utils.CreateLogEvent(null, IRDLM.TimeTriggerEnd(dB_MessagePayloads.Count))); await Utils.FlushLogs(logEvents); }