Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
I do need to cleanup this code, it was written during stressful times...
/** customize some global handling errors */
object Global extends WithFilters(LoggingFilter) {
// EMAIL BACKOFF stuff
val ERR_DELTA1 = 5 * 60 * 1000 // 5 min
val ERR_DELTA2 = 6 * 60 * 60 * 1000 // 6 hours
val ERR_EMAILS = 5 // per DELTA2
var errEmails = 0 // sent per DELTA2
var lastErrorTime = System.currentTimeMillis - ERR_DELTA1 // time last error email went out - just one every 5 min, eh
var firstErrorTime = System.currentTimeMillis - ERR_DELTA2 // time first error email went out - just one every 5 min, eh
var lastErrorCount = 0 // time last error email went out - just one every 5 min, eh
override def onError(request: RequestHeader, ex: Throwable) = {
clog << "ERR_onError - trying to log/audit in DB... " + "request:" + request.toString + "headers:" + request.headers + "ex:" + ex.toString
Audit.logdb("ERR_onError", "request:" + request.toString, "headers:" + request.headers, "ex:" + ex.toString)
val m = ("ERR_onError", "Current count: " + lastErrorCount + " Request:" + request.toString, "headers:" + request.headers, "ex:" + ex.toString).toString
if (System.currentTimeMillis - lastErrorTime >= ERR_DELTA1) {
if (errEmails <= ERR_EMAILS || System.currentTimeMillis - firstErrorTime >= ERR_DELTA2) {
admin.SendEmail.withSession { implicit mailSession =>
Emailer.tellRaz("ERR_onError",
currentUser.map(_.userName).mkString, m)
synchronized {
if (errEmails == ERR_EMAILS || System.currentTimeMillis - firstErrorTime >= ERR_DELTA2) {
errEmails = 0
firstErrorTime = lastErrorTime
}
errEmails = errEmails + 1
lastErrorTime = System.currentTimeMillis()
lastErrorCount = 0
}
}
} else {
lastErrorCount = 0
}
} else {
lastErrorCount = 0
}
super.onError(request, ex)
}
override def onHandlerNotFound(request: RequestHeader)= {
clog << "ERR_onHandlerNotFound " + "request:" + request.toString + "headers:" + request.headers
Audit.logdb("ERR_onHandlerNotFound", "request:" + request.toString, "headers:" + request.headers)
super.onHandlerNotFound(request)
}
override def onBadRequest(request: RequestHeader, error: String)= {
clog << ("ERR_onBadRequest " + "request:" + request.toString + "headers:" + request.headers + "error:" + error)
Audit.logdb("ERR_onBadRequest", "request:" + request.toString, "headers:" + request.headers, "error:" + error)
super.onBadRequest(request, error)
}
override def onRouteRequest(request: RequestHeader): Option[Handler] = {
if (! request.path.startsWith( "/assets/"))
cdebug << ("ROUTE_REQ.START: " + request.toString)
val res = super.onRouteRequest(request)
if (! request.path.startsWith( "/assets/"))
cdebug << ("ROUTE_REQ.STOP: " + request.toString)
res
}
override def onStart(app: Application) = {
// automated restart / patch / update handling
try { new File("../updating").delete() }
super.onStart(app)
}
}
object LoggingFilter extends Filter {
import ExecutionContext.Implicits.global
def apply(next: (RequestHeader) => scala.concurrent.Future[SimpleResult])(rh: RequestHeader) = {
val start = System.currentTimeMillis
if (! rh.uri.startsWith( "/assets/"))
cdebug << s"LF.START ${rh.method} ${rh.uri}"
GlobalData.synchronized {
GlobalData.serving = GlobalData.serving + 1
}
def served {
GlobalData.synchronized {
GlobalData.served = GlobalData.served + 1
GlobalData.serving = GlobalData.serving - 1
}
}
def servedPage {
GlobalData.synchronized {
GlobalData.servedPages = GlobalData.servedPages + 1
}
}
def logTime(what: String)(result: SimpleResult): Result = {
val time = System.currentTimeMillis - start
if (!isAsset) {
clog << s"LF.STOP $what ${rh.method} ${rh.uri} took ${time}ms and returned ${result.header.status}"
servedPage
}
served
result.withHeaders("Request-Time" -> time.toString)
}
def isAsset = rh.uri.startsWith( "/assets/") || rh.uri.startsWith("/favicon")
try {
next(rh) match {
case res @ _ => {
clog << s"LF.STOP.WHAT? ${rh.method} ${rh.uri} returned ${res}"
if (! isAsset) servedPage
served
res
}
}
} catch {
case t: Throwable => {
clog << s"LF.STOP.EXCEPTION ${rh.method} ${rh.uri} threw ${t.toString} \n ${com.razie.pub.base.log.Log.getStackTraceAsString(t)}"
served
throw t
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.