001/* 002 * Stallion Core: A Modern Web Framework 003 * 004 * Copyright (C) 2015 - 2016 Stallion Software LLC. 005 * 006 * This program is free software: you can redistribute it and/or modify it under the terms of the 007 * GNU General Public License as published by the Free Software Foundation, either version 2 of 008 * the License, or (at your option) any later version. This program is distributed in the hope that 009 * it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of 010 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public 011 * License for more details. You should have received a copy of the GNU General Public License 012 * along with this program. If not, see <http://www.gnu.org/licenses/gpl-2.0.html>. 013 * 014 * 015 * 016 */ 017 018package io.stallion.monitoring; 019 020import io.stallion.email.ContactableEmailer; 021import io.stallion.users.User; 022import io.stallion.utils.DateUtils; 023import io.stallion.utils.GeneralUtils; 024 025import java.net.MalformedURLException; 026import java.time.format.DateTimeFormatter; 027import java.util.Map; 028 029import static io.stallion.utils.Literals.*; 030import static io.stallion.Context.*; 031 032 033public class ExceptionEmailRunnable implements Runnable { 034 private ExceptionInfo exceptionInfo; 035 036 public ExceptionEmailRunnable(ExceptionInfo info) { 037 this.exceptionInfo = info; 038 } 039 040 @Override 041 public void run() { 042 for (String email: settings().getEmail().getAdminEmails()) { 043 User user = new User().setEmail(email); 044 ExceptionEmailer emailer = new ExceptionEmailer(user, exceptionInfo); 045 emailer.sendEmail(); 046 } 047 } 048 049 public static class ExceptionEmailer extends ContactableEmailer<User> { 050 051 public ExceptionEmailer(User user, ExceptionInfo ex) { 052 super(user); 053 put("siteUrl", getSettings().getSiteUrl()); 054 put("siteName", getSettings().getSiteName()); 055 put("exceptionInfo", ex); 056 } 057 058 @Override 059 public String getUniqueKey() { 060 ExceptionInfo ex = (ExceptionInfo)getContext().get("exceptionInfo"); 061 String hash = GeneralUtils.md5Hash(ex.getStackTrace()); 062 String minute = utcNow().format(DateUtils.MINUTE_FORMAT); 063 return "exception-" + hash + "-" + GeneralUtils.slugify(minute); 064 } 065 066 @Override 067 public boolean shouldLog() { 068 return false; 069 } 070 071 @Override 072 public boolean isTransactional() { 073 return true; 074 } 075 076 @Override 077 public String getTemplate() { 078 return getClass().getResource("/templates/exception-email.jinja").toString(); 079 } 080 081 @Override 082 public String getSubject() { 083 return "[ERROR] {exceptionInfo.className} on {exceptionInfo.requestUrl}"; 084 } 085 } 086}