From 384e322f974534c1c734db144633e3c3e002b1f8 Mon Sep 17 00:00:00 2001 From: Jonathan Herman Date: Wed, 10 Apr 2013 15:32:39 -0400 Subject: Added option to send email when experiments complete. --- run/emailer.py | 28 ++++++++++++++++++++++++++++ run/experiment.py | 10 ++++++++-- run/litmus_util.py | 4 +--- run_exps.py | 40 +++++++++++++++++++++++++++++----------- 4 files changed, 66 insertions(+), 16 deletions(-) create mode 100644 run/emailer.py diff --git a/run/emailer.py b/run/emailer.py new file mode 100644 index 0000000..6d66da2 --- /dev/null +++ b/run/emailer.py @@ -0,0 +1,28 @@ +import os +import pwd +import smtplib +import socket + +class Emailer(object): + def __init__(self, target): + user = pwd.getpwuid(os.getuid())[0] + host = socket.gethostname() + + self.sender = "%s@%s" % (user, host) + self.target = target + + self.body = "\r\n".join(["From: %s" % self.sender, + "To: %s" % target, + "Subject: Test Completed!", "", "{}"]) + + self.mail = smtplib.SMTP("localhost") + + # Hopefully crash if the server is not running + self.mail.ehlo() + + def close(self): + self.mail.quit() + + def send(self, text): + self.mail.sendmail(self.sender, [self.target], + self.body.format(text)) diff --git a/run/experiment.py b/run/experiment.py index 3dd4866..e5811f8 100644 --- a/run/experiment.py +++ b/run/experiment.py @@ -87,6 +87,10 @@ class Experiment(object): def __run_tasks(self): already_waiting = lu.waiting_tasks() + if already_waiting: + self.log("Already %d tasks waiting for release!") + self.log("Experiment will fail if any of these tasks are released.") + self.log("Starting the programs") for e in self.executables: try: @@ -115,7 +119,6 @@ class Experiment(object): # Need to re-release non-released tasks before we can kill them though self.log("Failed to release {} tasks! Re-releasing and killing".format( len(self.executables) - released, len(self.executables))) - time.sleep(5) released = lu.release_tasks() @@ -160,7 +163,10 @@ class Experiment(object): self.teardown() finally: self.log("Switching to Linux scheduler") - lu.switch_scheduler("Linux") + try: + lu.switch_scheduler("Linux") + except: + self.log("Failed to switch back to Linux.") if succ: self.__save_results() diff --git a/run/litmus_util.py b/run/litmus_util.py index b9080c1..4709b66 100644 --- a/run/litmus_util.py +++ b/run/litmus_util.py @@ -8,7 +8,6 @@ def switch_scheduler(switch_to_in): This methods sleeps for two seconds to give Linux the chance to execute schedule switching code. Raises an exception if the switch does not work. - ''' switch_to = str(switch_to_in).strip() @@ -23,7 +22,7 @@ def switch_scheduler(switch_to_in): cur_plugin = active_plugin.read().strip() if switch_to != cur_plugin: - raise Exception("Could not switch to plugin: %s" % switch_to) + raise Exception("Could not switch to '%s' (check dmesg)" % switch_to) def waiting_tasks(): reg = re.compile(r'^ready.*?(?P\d+)$', re.M) @@ -37,7 +36,6 @@ def waiting_tasks(): return 0 if not ready else int(ready) def release_tasks(): - try: data = subprocess.check_output([conf.BINS['release']]) except subprocess.CalledProcessError: diff --git a/run_exps.py b/run_exps.py index 77c9631..b9cf88e 100755 --- a/run_exps.py +++ b/run_exps.py @@ -68,6 +68,9 @@ def parse_args(): parser.add_option('-j', '--jabber', metavar='username@domain', dest='jabber', default=None, help='send a jabber message when an experiment completes') + parser.add_option('-e', '--email', metavar='username@server', + dest='email', default=None, + help='send an email when all experiments complete') return parser.parse_args() @@ -295,10 +298,22 @@ def setup_jabber(target): return Jabber(target) except ImportError: - sys.stderr.write("Failed to import jabber, disabling messages. " + - "Is python-xmpp installed?") + sys.stderr.write("Failed to import jabber. Is python-xmpp installed? " + + "Disabling instant messages.\n") return None +def setup_email(target): + try: + from run.emailer import Emailer + + return Emailer(target) + except ImportError: + message = "Failed to import email. Is smtplib installed?" + except IOError: + message = "Failed to create email. Is an smtp server active?" + + sys.stderr.write(message + " Disabling email message.\n") + return None def main(): opts, args = parse_args() @@ -320,10 +335,8 @@ def main(): failed = 0 invalid = 0 - if opts.jabber: - jabber = setup_jabber(opts.jabber) - else: - jabber = None + jabber = setup_jabber(opts.jabber) if opts.jabber else None + email = setup_email(opts.email) if opts.email else None for exp in args: path = "%s/%s" % (os.getcwd(), exp) @@ -357,12 +370,17 @@ def main(): if not os.listdir(out_base) and created and not succ: os.rmdir(out_base) - print("Experiments run:\t%d" % len(args)) - print(" Successful:\t\t%d" % succ) - print(" Failed:\t\t%d" % failed) - print(" Already Done:\t\t%d" % done) - print(" Invalid Environment:\t%d" % invalid) + message = "Experiments run:\t%d" % len(args) +\ + "\n Successful:\t\t%d" % succ +\ + "\n Failed:\t\t%d" % failed +\ + "\n Already Done:\t\t%d" % done +\ + "\n Invalid Environment:\t%d" % invalid + + print(message) + if email: + email.send(message) + email.close() if __name__ == '__main__': main() -- cgit v1.2.2