From e070665f3d89ba75ed3dbbc6378e5b395c2bf06e Mon Sep 17 00:00:00 2001 From: Klaus-Uwe Mitterer Date: Fri, 19 Oct 2018 19:36:59 +0200 Subject: [PATCH] Improve error handling --- handler.py | 10 +++++++--- server/daemon.py | 22 +++++++++++++++++----- server/hallmonitor.py | 1 - 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/handler.py b/handler.py index 4f4b970..f0efc4e 100755 --- a/handler.py +++ b/handler.py @@ -35,7 +35,11 @@ if __name__ == "__main__": args = parser.parse_args() - handler = parse_url(args.url) - image = handler(args.url, args.rotation or [0,0,0], args.resolution or [3840, 1920]) + try: + handler = parse_url(args.url) + image = handler(args.url, args.rotation or [0,0,0], args.resolution or [3840, 1920]) - image.save(args.output + "/" + args.title + ".png") + image.save(args.output + "/" + args.title + ".png") + except Exception as e: + with open(args.output + "/" + args.title + ".err", "w") as errorfile: + errorfile.write("") diff --git a/server/daemon.py b/server/daemon.py index c9771d9..7d77121 100644 --- a/server/daemon.py +++ b/server/daemon.py @@ -6,6 +6,8 @@ import uuid import configparser import os import glob +import hashlib +import time HTTP200 = "200 OK" HTTP202 = "202 Accepted" @@ -90,12 +92,22 @@ def getjob(req): jobid = req.path[-1] content_disposition = None - if glob.glob("/tmp/panosteal/%s---*" % jobid): - content = open(glob.glob("/tmp/panosteal/%s---*" % jobid)[0], "rb").read() + found = glob.glob("/tmp/panosteal/%s---*" % jobid) + + if found: + md5 = "Not happening." + while True: + content = open(found[0], "rb").read() + newmd5 = hashlib.md5(content).hexdigest() + if newmd5 == md5: + break + md5 = newmd5 + time.sleep(0.5) + code = HTTP200 ctype = mimetypes.guess_type("any.png")[0] - content_disposition = glob.glob("/tmp/panosteal/%s---*" % jobid)[0].split("---")[-1] - elif os.path.isfile("/tmp/panosteal/%s.err" % jobid): + content_disposition = found[0].split("---")[-1] + elif glob.glob("/tmp/panosteal/%s*err" % jobid): content = "

500 Internal Server Error

".encode() code = HTTP500 ctype = HTML @@ -106,7 +118,7 @@ def getjob(req): else: content = "".encode() code = HTTP202 - ctype = HTML + ctype = TEXT res = Response(code, ctype, content) diff --git a/server/hallmonitor.py b/server/hallmonitor.py index a7e6525..0ba222e 100644 --- a/server/hallmonitor.py +++ b/server/hallmonitor.py @@ -27,7 +27,6 @@ class createHandler(pyinotify.ProcessEvent): try: handleIncoming(event.pathname) except Exception as e: - raise with open(event.pathname + ".err", "w") as errorfile: errorfile.write("") print(e)