Last active
October 8, 2015 10:18
-
-
Save danielrichman/3317671 to your computer and use it in GitHub Desktop.
Check that habhub.org/predict is working
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# Copyright 2012 Daniel Richman. GNU GPL 3 | |
# Logging setup borrowed from habitat.utils.startup | |
import time | |
import urllib | |
import json | |
import os | |
import logging | |
from logging.handlers import SMTPHandler | |
logger = logging.getLogger("check_predictor") | |
def start_prediction(): | |
url = "http://habhub.org/predict/ajax.php?action=submitForm" | |
now = time.localtime(time.time() + 3600 * 24) | |
post = { | |
"launchsite": "Churchill", | |
"lat": "52.2135", | |
"lon": "0.0964", | |
"initial_alt": "0", | |
"ascent": 5, | |
"burst": 30000, | |
"drag": 5, | |
"software": "gfs", | |
"delta_lat": 3, | |
"delta_lon": 3, | |
"delta_time": 5, | |
"submit": "Run Prediction", | |
"hour": now.tm_hour, | |
"min": now.tm_min, | |
"second": now.tm_sec, | |
"day": now.tm_mday, | |
"month": now.tm_mon, | |
"year": now.tm_year | |
} | |
req = urllib.urlopen(url, data=urllib.urlencode(post)) | |
resp = json.load(req) | |
req.close() | |
logger.debug("ajax submit responded {0!r}".format(resp)) | |
assert resp["valid"] and resp["uuid"] | |
return resp["uuid"] | |
def get_progress(uuid): | |
url = "http://habhub.org/predict/preds/{0}/progress.json".format(uuid) | |
req = urllib.urlopen(url) | |
resp = json.load(req) | |
req.close() | |
logger.debug("get progress responded {0!r}".format(resp)) | |
return resp | |
def do_check(): | |
start_time = time.time() | |
uuid = start_prediction() | |
while True: | |
time.sleep(10) | |
resp = get_progress(uuid) | |
if resp["error"]: | |
raise Exception(resp["error"]) | |
if resp["pred_complete"]: | |
return # success | |
if time.time() - start_time > 720: | |
raise Exception("Timeout") | |
def setup_logging(): | |
# Lifted from habitat | |
formatstring = "[%(asctime)s] %(levelname)s %(name)s %(threadName)s: " + \ | |
"%(message)s" | |
file_name = "/tmp/check_predictor.log" | |
file_level = logging.INFO | |
mail_targets = ['root@kraken.habhub.org'] | |
root_logger = logging.getLogger() | |
root_logger.setLevel(logging.DEBUG) | |
file_handler = logging.handlers.WatchedFileHandler(file_name) | |
file_handler.setFormatter(logging.Formatter(formatstring)) | |
file_handler.setLevel(file_level) | |
root_logger.addHandler(file_handler) | |
mail_handler = SMTPHandler('127.0.0.1', 'www-data@kraken.habhub.org', | |
mail_targets, 'check predictor failed') | |
mail_handler.setLevel(logging.ERROR) | |
root_logger.addHandler(mail_handler) | |
def main(): | |
setup_logging() | |
while True: | |
try: | |
logger.info("starting check") | |
do_check() | |
logger.info("check ok") | |
except KeyboardInterrupt: | |
raise | |
except SystemExit: | |
raise | |
except: | |
logger.exception("error in check") | |
time.sleep(600) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment