private void write(ConcurrentQueue <Message> queue, int count) //-1 ==all { const int ABORT_TIMEOUT_MS = 10000; var processed = 0; Message msg; var started = App.TimeSource.UTCNow; while ((count < 0 || processed < count) && queue.TryDequeue(out msg)) { if (!Running && (App.TimeSource.UTCNow - started).TotalMilliseconds > ABORT_TIMEOUT_MS) { log(MessageType.Error, "{0}.Write(msg) aborted on svc shutdown: timed-out after {1} ms.".Args(m_Sink.GetType().FullName, ABORT_TIMEOUT_MS), null); break; } try { statSend(); m_Sink.SendMsg(msg); } catch (Exception error) { statSendError(); var et = error.ToMessageWithType(); log(MessageType.Error, "{0}.Write(msg) leaked {1}".Args(m_Sink.GetType().FullName, et), et); writeFallback(msg); } processed++; } }
private void writeFallback(Message msg) { try { statFallback(); m_FallbackSink.SendMsg(msg); } catch (Exception error) { statFallbackError(); var et = error.ToMessageWithType(); log(MessageType.Error, "{0}.Write(msg) leaked {1}".Args(m_FallbackSink.GetType().FullName, et), et); } }
/// <summary> /// MessageSink DoSendMsg implementation /// </summary> /// <param name="msg">Message</param> protected override void DoSendMsg(Message msg) { if (msg == null || msg.TOAddress.IsNullOrWhiteSpace()) { return; } var request = new WebClient.RequestParams() { Caller = this, Method = HTTPRequestMethod.POST, Uri = new Uri(ServiceUrl), Headers = new Dictionary <string, string>(), BodyParameters = new Dictionary <string, string>(), UName = "api", UPwd = AuthorizationKey }; var fromAddress = "{0} <{1}>".Args(DefaultFromName, DefaultFromAddress); if (msg.FROMAddress.IsNotNullOrWhiteSpace()) { fromAddress = "{0} <{1}>".Args(msg.FROMName, msg.FROMAddress); } addParameter(request.BodyParameters, MAIL_PARAM_FROM, fromAddress); addParameter(request.BodyParameters, MAIL_PARAM_TO, "{0} <{1}>".Args(msg.TOName, msg.TOAddress)); addParameter(request.BodyParameters, MAIL_PARAM_CC, msg.CC); addParameter(request.BodyParameters, MAIL_PARAM_BCC, msg.BCC); addParameter(request.BodyParameters, MAIL_PARAM_SUBJECT, msg.Subject); addParameter(request.BodyParameters, MAIL_PARAM_TEXT, msg.Body); addParameter(request.BodyParameters, MAIL_PARAM_HTML, msg.HTMLBody); if (TestMode) { request.BodyParameters.Add(API_PARAM_TESTMODE, "Yes"); } try { var result = WebClient.GetJsonAsDynamic(request); } catch (Exception e) { if (m_FallbackSink != null) { m_FallbackSink.SendMsg(msg); } } }