From 3b994431eb20c55a3f83b6afc6fceee4e0b948c4 Mon Sep 17 00:00:00 2001 From: Joseph Schorr Date: Mon, 20 Jun 2016 11:51:30 -0400 Subject: [PATCH] Auto expire the build status and logs in redis --- data/buildlogs.py | 12 ++++++++++-- workers/buildlogsarchiver.py | 1 + 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/data/buildlogs.py b/data/buildlogs.py index 027093f66..90a5fd2e7 100644 --- a/data/buildlogs.py +++ b/data/buildlogs.py @@ -7,6 +7,7 @@ from datetime import timedelta ONE_DAY = timedelta(days=1) +SEVEN_DAYS = timedelta(days=7) class BuildStatusRetrievalError(Exception): @@ -33,6 +34,7 @@ class RedisBuildLogs(object): Appends the serialized form of log_obj to the end of the log entry list and returns the new length of the list. """ + self._redis.expire(self._logs_key(build_id), SEVEN_DAYS) return self._redis.rpush(self._logs_key(build_id), json.dumps(log_obj)) def append_log_message(self, build_id, log_message, log_type=None, log_data=None): @@ -50,7 +52,7 @@ class RedisBuildLogs(object): if log_data: log_obj['data'] = log_data - return self._redis.rpush(self._logs_key(build_id), json.dumps(log_obj)) - 1 + return self.append_log_entry(build_id, log_obj) - 1 def get_log_entries(self, build_id, start_index): """ @@ -64,6 +66,12 @@ class RedisBuildLogs(object): except redis.RedisError as re: raise BuildStatusRetrievalError('Cannot retrieve build logs: %s' % re) + def expire_status(self, build_id): + """ + Sets the status entry to expire in 1 day. + """ + self._redis.expire(self._status_key(build_id), ONE_DAY) + def expire_log_entries(self, build_id): """ Sets the log entry to expire in 1 day. @@ -79,7 +87,7 @@ class RedisBuildLogs(object): Sets the status key for this build to json serialized form of the supplied obj. """ - self._redis.set(self._status_key(build_id), json.dumps(status_obj)) + self._redis.set(self._status_key(build_id), json.dumps(status_obj), ex=SEVEN_DAYS) def get_status(self, build_id): """ diff --git a/workers/buildlogsarchiver.py b/workers/buildlogsarchiver.py index e29417c92..c2ca6b586 100644 --- a/workers/buildlogsarchiver.py +++ b/workers/buildlogsarchiver.py @@ -52,6 +52,7 @@ class ArchiveBuildLogsWorker(Worker): to_update.logs_archived = True to_update.save() + build_logs.expire_status(to_update.uuid) build_logs.expire_log_entries(to_update.uuid)