/// <summary> /// Creates a MantaEvent object and Fills it with the values from the data record. /// </summary> /// <param name="record">Record to get the data from.</param> /// <returns>MantaAubseEvent or MantaBounceEvent</returns> private static MantaEvent CreateAndFillMantaEventFromRecord(IDataRecord record) { MantaEventType type = (MantaEventType)record.GetInt32("evn_type_id"); MantaEvent thisEvent = null; switch (type) { case MantaEventType.Abuse: thisEvent = new MantaAbuseEvent(); break; case MantaEventType.Bounce: thisEvent = new MantaBounceEvent(); FillMantaBounceEvent((thisEvent as MantaBounceEvent), record); break; case MantaEventType.TimedOutInQueue: thisEvent = new MantaTimedOutInQueueEvent(); break; default: throw new NotImplementedException("Unknown Event Type (" + type + ")"); } thisEvent.EmailAddress = record.GetString("evn_event_emailAddress"); thisEvent.EventTime = record.GetDateTime("evn_event_timestamp"); thisEvent.EventType = type; thisEvent.ID = record.GetInt32("evn_event_id"); thisEvent.SendID = record.GetString("snd_send_id"); thisEvent.Forwarded = record.GetBoolean("evn_event_forwarded"); return(thisEvent); }
/// <summary> /// Saves the MantaEvent to the database. /// </summary> /// <param name="evn">The Manta Event to save.</param> /// <returns>ID of the MantaEvent.</returns> public static async Task <int> SaveAsync(MantaEvent evn) { using (SqlConnection conn = MantaDB.GetSqlConnection()) { SqlCommand cmd = conn.CreateCommand(); cmd.CommandText = @" IF EXISTS (SELECT 1 FROM man_evn_event WHERE evn_event_id = @eventID) BEGIN UPDATE man_evn_event SET evn_type_id = @eventType, evn_event_timestamp = @timestamp, evn_event_emailAddress = @emailAddress, snd_send_id = @sendId, evn_event_forwarded = @forwarded WHERE evn_event_id = @eventID END ELSE BEGIN INSERT INTO man_evn_event(evn_type_id, evn_event_timestamp, evn_event_emailAddress, snd_send_id, evn_event_forwarded) VALUES(@eventType, @timestamp, @emailAddress, @sendId, @forwarded) SET @eventID = @@IDENTITY END "; cmd.Parameters.AddWithValue("@eventID", evn.ID); cmd.Parameters.AddWithValue("@eventType", (int)evn.EventType); cmd.Parameters.AddWithValue("@timestamp", evn.EventTime); cmd.Parameters.AddWithValue("@emailAddress", evn.EmailAddress); cmd.Parameters.AddWithValue("@sendId", evn.SendID); cmd.Parameters.AddWithValue("@forwarded", evn.Forwarded); if (evn is MantaBounceEvent) { cmd.CommandText += @" IF EXISTS (SELECT 1 FROM man_evn_bounceEvent WHERE evn_event_id = @eventId) UPDATE man_evn_bounceEvent SET evn_bounceCode_id = @bounceCode, evn_bounceEvent_message = @message, evn_bounceType_id = @bounceType WHERE evn_event_id = @eventId ELSE INSERT INTO man_evn_bounceEvent(evn_event_id, evn_bounceCode_id, evn_bounceEvent_message, evn_bounceType_id) VALUES(@eventId, @bounceCode, @message, @bounceType) "; cmd.Parameters.AddWithValue("@bounceCode", (int)(evn as MantaBounceEvent).BounceInfo.BounceCode); cmd.Parameters.AddWithValue("@message", (evn as MantaBounceEvent).Message); cmd.Parameters.AddWithValue("@bounceType", (int)(evn as MantaBounceEvent).BounceInfo.BounceType); } cmd.CommandText += @"SELECT @eventID "; await conn.OpenAsync(); return(Convert.ToInt32(await cmd.ExecuteScalarAsync())); } }
public void TimedOutInQueue() { using (System.Transactions.TransactionScope ts = CreateTransactionScopeObject()) { MantaEventCollection events = EventsManager.Instance.GetEvents(); int initialMaxEventID = events.Max(e => e.ID); bool result = false; EmailProcessingDetails processingDetails; result = EventsManager.Instance.ProcessSmtpResponseMessage(MtaParameters.TIMED_OUT_IN_QUEUE_MESSAGE, "*****@*****.**", 1, out processingDetails); Assert.IsTrue(result); Assert.IsNull(processingDetails); // Check an Event has been created. events = EventsManager.Instance.GetEvents(); int newMaxEventID = events.Max(e => e.ID); Assert.AreNotEqual(initialMaxEventID, newMaxEventID); MantaEvent ev = events.First(e => e.ID == newMaxEventID); // Check the new Event record. Assert.IsTrue(ev is MantaTimedOutInQueueEvent); Assert.AreEqual(ev.ID, newMaxEventID); Assert.AreEqual(ev.EventType, MantaEventType.TimedOutInQueue); Assert.AreEqual(ev.EmailAddress, "*****@*****.**"); Assert.AreEqual(ev.SendID, SendDB.GetSend(1).ID); Assert.That(ev.EventTime, Is.EqualTo(DateTime.UtcNow).Within(TimeSpan.FromSeconds(1))); // Depends on how long it takes to get the Events out of the DB. Assert.IsFalse(ev.Forwarded); } }
private async Task ForwardEventAsync(MantaEvent evt) { try { if (_IsStopping) { return; } // Create the HTTP POST request to the remove endpoint. var httpRequest = (HttpWebRequest)WebRequest.Create(MtaParameters.EventForwardingHttpPostUrl); httpRequest.Method = "POST"; httpRequest.ContentType = "text/json"; // Convert the Event to JSON. string eventJson = string.Empty; switch (evt.EventType) { case MantaEventType.Abuse: eventJson = JsonConvert.SerializeObject((MantaAbuseEvent)evt); break; case MantaEventType.Bounce: eventJson = JsonConvert.SerializeObject((MantaBounceEvent)evt); break; case MantaEventType.TimedOutInQueue: eventJson = JsonConvert.SerializeObject((MantaTimedOutInQueueEvent)evt); break; default: eventJson = JsonConvert.SerializeObject(evt); break; } // Remove the forwarded property as it is internal only. eventJson = Regex.Replace(eventJson, ",\"Forwarded\":(false|true)", string.Empty); // Write the event json to the POST body. using (StreamWriter writer = new StreamWriter(await httpRequest.GetRequestStreamAsync())) { await writer.WriteAsync(eventJson); } // Send the POST and get the response. HttpWebResponse httpResponse = (HttpWebResponse)await httpRequest.GetResponseAsync(); // Get the response body. string responseBody = string.Empty; using (StreamReader reader = new StreamReader(httpResponse.GetResponseStream())) { responseBody = await reader.ReadToEndAsync(); } // If response body is just a "." then event was received successfully. if (responseBody.Trim().StartsWith(".")) { // Log that the event forwared. evt.Forwarded = true; await EventsManager.Instance.SaveAsync(evt); } } catch (Exception ex) { // We failed to forward the event. Most likly because the remote server didn't respond. Logging.Error("Failed to forward event " + evt.ID, ex); } }
internal async Task <int> SaveAsync(MantaEvent evt) { return(await EventDB.SaveAsync(evt)); }
/// <summary> /// Saves a Manta Event. /// </summary> /// <param name="evt">Event to save.</param> /// <returns>The Events ID</returns> internal int Save(MantaEvent evt) { return(SaveAsync(evt).Result); }