From 6e0ca735a57deb31e1ac21821a9814796c7d9350 Mon Sep 17 00:00:00 2001 From: Joseph Schorr Date: Wed, 30 Sep 2015 15:23:19 -0400 Subject: [PATCH] Add a better redis health check that reads and writes This will hopefully catch issues earlier with Redis --- data/buildlogs.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/data/buildlogs.py b/data/buildlogs.py index 17e5b397f..e9ea4a78f 100644 --- a/data/buildlogs.py +++ b/data/buildlogs.py @@ -1,5 +1,6 @@ import redis import json +import time from util.dynamic import import_class from datetime import timedelta @@ -65,7 +66,6 @@ class RedisBuildLogs(object): """ self._redis.expire(self._logs_key(build_id), ONE_DAY) - @staticmethod def _status_key(build_id): return 'builds/%s/status' % build_id @@ -88,9 +88,20 @@ class RedisBuildLogs(object): return json.loads(fetched) if fetched else None + @staticmethod + def _health_key(): + return '_health' + def check_health(self): try: - return self._redis.ping() == True + if not self._redis.ping() == True: + return False + + # Ensure we can write and read a key. + self._redis.set(self._health_key(), time.time()) + self._redis.get(self._health_key()) + + return True except redis.ConnectionError: return False