From 4feeaef2fdb27426d36a8bc3478437c8e0fe6bfd Mon Sep 17 00:00:00 2001 From: Brandon Davis Date: Sun, 21 Aug 2016 11:20:00 -0700 Subject: [PATCH 1/6] Add exception handling for when redis is down and or not running. --- snappass/main.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/snappass/main.py b/snappass/main.py index ee9b77e..f074a24 100644 --- a/snappass/main.py +++ b/snappass/main.py @@ -1,7 +1,10 @@ import os +import sys import uuid +import traceback import redis +from redis.exceptions import ConnectionError from flask import abort, Flask, render_template, request @@ -21,8 +24,19 @@ time_conversion = { 'hour': 3600 } +def check_redis_alive(force_exit = False): + try: + redis_client.ping() + except (ConnectionError, TimeoutError) as e: + if force_exit: + print("{}".format(e)) + sys.exit(0) + else: + abort(500) + def set_password(password, ttl): + check_redis_alive() key = uuid.uuid4().hex redis_client.set(key, password) redis_client.expire(key, ttl) @@ -30,6 +44,7 @@ def set_password(password, ttl): def get_password(key): + check_redis_alive() password = redis_client.get(key) if password is not None: password = password.decode('utf-8') @@ -83,6 +98,7 @@ def show_password(password_key): def main(): + check_redis_alive(force_exit=True) app.run(host='0.0.0.0', debug=True) From 2d6aec17cd653ab0dc23f4b0ea2cbd0539807250 Mon Sep 17 00:00:00 2001 From: Brandon Davis Date: Sun, 21 Aug 2016 11:24:20 -0700 Subject: [PATCH 2/6] Remove unused traceback and code formatting cleanup --- snappass/main.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/snappass/main.py b/snappass/main.py index f074a24..d28baee 100644 --- a/snappass/main.py +++ b/snappass/main.py @@ -1,7 +1,6 @@ import os import sys import uuid -import traceback import redis from redis.exceptions import ConnectionError @@ -29,8 +28,8 @@ def check_redis_alive(force_exit = False): redis_client.ping() except (ConnectionError, TimeoutError) as e: if force_exit: - print("{}".format(e)) - sys.exit(0) + print("{}".format(e)) + sys.exit(0) else: abort(500) From c9db4914851d1ffe24a7317728daeac5711e6a82 Mon Sep 17 00:00:00 2001 From: Brandon Davis Date: Sun, 21 Aug 2016 11:50:06 -0700 Subject: [PATCH 3/6] Minor cleanup --- snappass/main.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/snappass/main.py b/snappass/main.py index d28baee..9db002d 100644 --- a/snappass/main.py +++ b/snappass/main.py @@ -23,12 +23,12 @@ time_conversion = { 'hour': 3600 } -def check_redis_alive(force_exit = False): +def check_redis_alive(force_exit=False): try: redis_client.ping() except (ConnectionError, TimeoutError) as e: if force_exit: - print("{}".format(e)) + print(e) sys.exit(0) else: abort(500) From 9694d7da7f411d94331a71349edf0f9a7660a2b3 Mon Sep 17 00:00:00 2001 From: Brandon Davis Date: Sun, 21 Aug 2016 21:27:00 -0700 Subject: [PATCH 4/6] Switch to use decorator for checking if redis server is up. - setup.py removed empty line flake8 was complaining. --- setup.py | 1 - snappass/main.py | 28 ++++++++++++++++------------ 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/setup.py b/setup.py index 562b643..0bf1581 100644 --- a/setup.py +++ b/setup.py @@ -36,4 +36,3 @@ setup( ], zip_safe=False, ) - diff --git a/snappass/main.py b/snappass/main.py index 9db002d..a7bee3e 100644 --- a/snappass/main.py +++ b/snappass/main.py @@ -3,7 +3,7 @@ import sys import uuid import redis -from redis.exceptions import ConnectionError +from redis.exceptions import ConnectionError, TimeoutError from flask import abort, Flask, render_template, request @@ -23,27 +23,31 @@ time_conversion = { 'hour': 3600 } -def check_redis_alive(force_exit=False): - try: - redis_client.ping() - except (ConnectionError, TimeoutError) as e: - if force_exit: + +def check_redis_alive(fn): + def inner(*args, **kwargs): + try: + redis_client.ping() + except (ConnectionError, TimeoutError) as e: print(e) - sys.exit(0) - else: - abort(500) + if fn.__name__ == "main": + sys.exit(0) + else: + return abort(500) + return fn(*args, **kwargs) + return inner +@check_redis_alive def set_password(password, ttl): - check_redis_alive() key = uuid.uuid4().hex redis_client.set(key, password) redis_client.expire(key, ttl) return key +@check_redis_alive def get_password(key): - check_redis_alive() password = redis_client.get(key) if password is not None: password = password.decode('utf-8') @@ -96,8 +100,8 @@ def show_password(password_key): return render_template('password.html', password=password) +@check_redis_alive def main(): - check_redis_alive(force_exit=True) app.run(host='0.0.0.0', debug=True) From e0ae801b244cf8971daa9cd8c124fdae866f4ba1 Mon Sep 17 00:00:00 2001 From: Brandon Davis Date: Sun, 21 Aug 2016 21:33:12 -0700 Subject: [PATCH 5/6] Remove TimeoutError import --- snappass/main.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/snappass/main.py b/snappass/main.py index a7bee3e..463d8f5 100644 --- a/snappass/main.py +++ b/snappass/main.py @@ -3,7 +3,7 @@ import sys import uuid import redis -from redis.exceptions import ConnectionError, TimeoutError +from redis.exceptions import ConnectionError from flask import abort, Flask, render_template, request @@ -28,7 +28,7 @@ def check_redis_alive(fn): def inner(*args, **kwargs): try: redis_client.ping() - except (ConnectionError, TimeoutError) as e: + except ConnectionError as e: print(e) if fn.__name__ == "main": sys.exit(0) From fadd2854e507ff1108a55a818ce5412dea45c530 Mon Sep 17 00:00:00 2001 From: Brandon Davis Date: Mon, 22 Aug 2016 21:59:08 -0700 Subject: [PATCH 6/6] Fix for remaining comments. - Call function within try/catch - Syntax clean up --- snappass/main.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/snappass/main.py b/snappass/main.py index 463d8f5..089a364 100644 --- a/snappass/main.py +++ b/snappass/main.py @@ -27,14 +27,15 @@ time_conversion = { def check_redis_alive(fn): def inner(*args, **kwargs): try: - redis_client.ping() + if fn.__name__ == 'main': + redis_client.ping() + return fn(*args, **kwargs) except ConnectionError as e: - print(e) - if fn.__name__ == "main": + print('Failed to connect to redis! %s' % e.message) + if fn.__name__ == 'main': sys.exit(0) else: return abort(500) - return fn(*args, **kwargs) return inner