Ejegg has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/343365 )

Change subject: Send failmail
......................................................................

Send failmail

Change-Id: I987df527c07e95b5bde31c89118d72681673d94a
TODO: check the send args in tests
---
M job_wrapper.py
M tests/test_job_wrapper.py
2 files changed, 34 insertions(+), 8 deletions(-)


  git pull 
ssh://gerrit.wikimedia.org:29418/wikimedia/fundraising/process-control 
refs/changes/65/343365/1

diff --git a/job_wrapper.py b/job_wrapper.py
index 4813b3a..4a07833 100644
--- a/job_wrapper.py
+++ b/job_wrapper.py
@@ -1,6 +1,8 @@
 from __future__ import print_function
 import datetime
+from email.mime.text import MIMEText
 import shlex
+import smtplib
 import subprocess
 import sys
 import threading
@@ -61,18 +63,39 @@
             self.fail_exitcode(return_code)
 
     def fail_exitcode(self, return_code):
-        print("Job {name} failed with code {code}".format(name=self.name, 
code=return_code), file=sys.stderr)
+        message = "Job {name} failed with code {code}".format(name=self.name, 
code=return_code)
+        print(message, file=sys.stderr)
         # TODO: Prevent future jobs according to config.
+        self.fail_mail(message)
 
     def fail_has_stderr(self, stderr_data):
-        print("Job {name} printed things to stderr:".format(name=self.name), 
file=sys.stderr)
-        print(stderr_data.decode("utf-8"), file=sys.stderr)
+        message = "Job {name} printed things to stderr:".format(name=self.name)
+        print(message, file=sys.stderr)
+        data = stderr_data.decode("utf-8")
+        print(data, file=sys.stderr)
+        self.fail_mail(message, data)
 
     def fail_timeout(self):
         self.process.kill()
-        print("Job {name} timed out after {timeout} 
seconds".format(name=self.name, timeout=self.timeout), file=sys.stderr)
+        message = "Job {name} timed out after {timeout} 
seconds".format(name=self.name, timeout=self.timeout)
+        print(message, file=sys.stderr)
+        self.fail_mail(message)
         # FIXME: Job will return SIGKILL now, fail_exitcode should ignore that 
signal now?
 
+    def fail_mail(self, subject, body="Hope your wits are freshly sharpened!"):
+        msg = MIMEText(body)
+        # TODO: get these from our global config
+        from_address = "Fail Mail <fr-t...@wikimedia.org>"
+        to_address = "fr-t...@wikimedia.org"
+
+        msg["Subject"] = "Fail Mail : " + subject
+        msg["From"] = from_address
+        msg["To"] = to_address
+
+        mailer = smtplib.SMTP("localhost")
+        mailer.sendmail(from_address, to_address, msg.as_string())
+        mailer.quit()
+
     def store_job_output(self, stdout_data):
         if "stdout_destination" not in self.config:
             return
diff --git a/tests/test_job_wrapper.py b/tests/test_job_wrapper.py
index 730ca2c..d4ef6dd 100644
--- a/tests/test_job_wrapper.py
+++ b/tests/test_job_wrapper.py
@@ -1,3 +1,4 @@
+from mock import patch
 import iocapture
 import nose
 import os
@@ -22,8 +23,8 @@
         assert captured.stdout == ""
         assert captured.stderr == ""
 
-
-def test_return_code():
+@patch("smtplib.SMTP")
+def test_return_code(MockSmtp):
     with iocapture.capture() as captured:
         run_job("return_code.yaml")
 
@@ -33,7 +34,8 @@
 
 # Must finish in less than two seconds, i.e. must have timed out.
 @nose.tools.timed(2)
-def test_timeout():
+@patch("smtplib.SMTP")
+def test_timeout(MockSmtp):
     with iocapture.capture() as captured:
         run_job("timeout.yaml")
 
@@ -44,7 +46,8 @@
         )
 
 
-def test_stderr():
+@patch("smtplib.SMTP")
+def test_stderr(MockSmtp):
     with iocapture.capture() as captured:
         run_job("errors.yaml")
 

-- 
To view, visit https://gerrit.wikimedia.org/r/343365
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I987df527c07e95b5bde31c89118d72681673d94a
Gerrit-PatchSet: 1
Gerrit-Project: wikimedia/fundraising/process-control
Gerrit-Branch: master
Gerrit-Owner: Ejegg <eeggles...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to