From 9c7bbbdd0ac7bcfbca04f55854d9bdf7b37224cd Mon Sep 17 00:00:00 2001
From: Julian Rother <julianr@fsmpi.rwth-aachen.de>
Date: Sun, 24 Dec 2017 02:51:08 +0100
Subject: [PATCH] Send notifications for exceptions in scheduled functions

---
 scheduler.py                                                  | 2 ++
 server.py                                                     | 4 ++--
 templates/mails/{exception.body => endpoint_exception.body}   | 0
 .../mails/{exception.subject => endpoint_exception.subject}   | 0
 templates/mails/scheduler_exception.body                      | 4 ++++
 templates/mails/scheduler_exception.subject                   | 1 +
 6 files changed, 9 insertions(+), 2 deletions(-)
 rename templates/mails/{exception.body => endpoint_exception.body} (100%)
 rename templates/mails/{exception.subject => endpoint_exception.subject} (100%)
 create mode 100644 templates/mails/scheduler_exception.body
 create mode 100644 templates/mails/scheduler_exception.subject

diff --git a/scheduler.py b/scheduler.py
index 9227505..af7a6b5 100644
--- a/scheduler.py
+++ b/scheduler.py
@@ -22,6 +22,8 @@ def sched_func(delay, priority=0, firstdelay=None, args=[], kargs={}):
 					func(*args, **kargs)
 				except Exception:
 					traceback.print_exc()
+					notify_admins('scheduler_exception', name=func.__name__,
+							traceback=traceback.format_exc())
 			scheduler.enter(delay, priority, sched_wrapper)
 		scheduler.enter(firstdelay, priority, sched_wrapper)
 		print("Scheduler: registered {} (frequency 1/{}s, start delay: {}s)".format(func.__name__, delay, firstdelay))
diff --git a/server.py b/server.py
index 60740ee..7898f58 100644
--- a/server.py
+++ b/server.py
@@ -43,10 +43,10 @@ if not config.get('SECRET_KEY', None):
 	config['SECRET_KEY'] = os.urandom(24)
 
 from db import query, modify, show, searchquery
+from mail import notify_mods, notify_admins
 from ldap import ldapauth
 from legacy import legacy_index
 from scheduler import sched_func
-from mail import notify_mods, notify_admins
 
 mod_endpoints = []
 
@@ -124,7 +124,7 @@ def handle_not_found(e=None):
 @app.errorhandler(Exception)
 def handle_internal_error(e):
 	traceback.print_exc()
-	notify_admins('exception', traceback=traceback.format_exc())
+	notify_admins('endpoint_exception', traceback=traceback.format_exc())
 	return render_template('500.html'), 500
 
 @sched_func(5*60, firstdelay=0)
diff --git a/templates/mails/exception.body b/templates/mails/endpoint_exception.body
similarity index 100%
rename from templates/mails/exception.body
rename to templates/mails/endpoint_exception.body
diff --git a/templates/mails/exception.subject b/templates/mails/endpoint_exception.subject
similarity index 100%
rename from templates/mails/exception.subject
rename to templates/mails/endpoint_exception.subject
diff --git a/templates/mails/scheduler_exception.body b/templates/mails/scheduler_exception.body
new file mode 100644
index 0000000..7bf228c
--- /dev/null
+++ b/templates/mails/scheduler_exception.body
@@ -0,0 +1,4 @@
+Function: {{ name }}
+Hostname: {{ gethostname() }}
+
+{{ traceback }}
diff --git a/templates/mails/scheduler_exception.subject b/templates/mails/scheduler_exception.subject
new file mode 100644
index 0000000..ff9fc4f
--- /dev/null
+++ b/templates/mails/scheduler_exception.subject
@@ -0,0 +1 @@
+Exception in scheduler function "{{ name }}"
-- 
GitLab