Skip to content

Instantly share code, notes, and snippets.

@abdullin
Created June 15, 2011 16:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abdullin/1027419 to your computer and use it in GitHub Desktop.
Save abdullin/1027419 to your computer and use it in GitHub Desktop.
Quarantine sample that reports recurring failures to email (sample for Lokad.CQRS v2.0)
// This quarantine sample uses in-memory quarantine to detect repetitive processing failures
// It records each failure into the streaming container (which could be file/Azure)
// When failure threshold is breached, we send an email to the hardcoded address.
// failure message contains all information about failures and message contents
public sealed class MailQuarantine : IEnvelopeQuarantine
{
readonly SmtpHandlerCore _core;
readonly IStreamingContainer _container;
readonly MemoryQuarantine _quarantine = new MemoryQuarantine();
public MailQuarantine(SmtpHandlerCore core, IStreamingRoot root)
{
_core = core;
_container = root.GetContainer("sample-errors").Create();
}
public bool TryToQuarantine(EnvelopeTransportContext context, Exception ex)
{
var quarantined = _quarantine.TryToQuarantine(context, ex);
try
{
var item = GetStreamingItem(context);
var data = "";
try
{
data = item.ReadText();
}
catch (StreamingItemNotFoundException)
{
}
var builder = new StringBuilder(data);
if (builder.Length == 0)
{
DescribeMessage(builder, context);
}
builder.AppendLine("[Exception]");
builder.AppendLine(DateTime.UtcNow.ToString());
builder.AppendLine(ex.ToString());
builder.AppendLine();
var text = builder.ToString();
item.WriteText(text);
if (quarantined)
ReportFailure(text, context.Unpacked);
}
catch (Exception x)
{
Trace.WriteLine(x.ToString());
}
return quarantined;
}
IStreamingItem GetStreamingItem(EnvelopeTransportContext context)
{
var createdOnUtc = context.Unpacked.CreatedOnUtc;
var file = string.Format("{0:yyyy-MM-dd-HH-mm}-{1}-engine.txt",
createdOnUtc,
context.Unpacked.EnvelopeId.ToLowerInvariant());
return _container.GetItem(file);
}
public void TryRelease(EnvelopeTransportContext context)
{
_quarantine.TryRelease(context);
}
static void DescribeMessage(StringBuilder builder, EnvelopeTransportContext context)
{
builder.AppendLine(string.Format("{0,12}: {1}", "Queue", context.QueueName));
builder.AppendLine(context.Unpacked.PrintToString(o => JsonConvert.SerializeObject(o, Formatting.Indented)));
}
void ReportFailure(string text, ImmutableEnvelope envelope)
{
var name = envelope.Items
.Select(e => e.MappedType.Name)
.Select(e => StringUtil.MemberNameToCaption(e.Replace("Command", "")))
.FirstOrDefault() ?? "N/A";
var subject = string.Format("[Error]: Sample fails to '{0}'", name);
var builder = new StringBuilder();
builder.AppendFormat(
@"<p>Dear Lokad.CQRS Developer,</p><p>Something just went horribly wrong - there is a problem that I can't resolve. Please check the error log <strong>ASAP</strong>.</p>
<p>Here are a few details to help you out:</p><pre>");
builder.AppendLine(EncoderUtil.EncodeForPre(text));
builder.AppendFormat(
"</pre><p>Sincerely,<br /> Lokad.CQRS AI</p>");
var mail = new MailMessage()
{
Body = builder.ToString(),
Subject = subject,
IsBodyHtml = true,
};
mail.To.Add(new MailAddress("contact@company.com", "Sample Support"));
_core.Send(mail);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment