From 60806cd5362af92035a3cc66e9fd73f2780d4cd6 Mon Sep 17 00:00:00 2001 From: Houkime <> Date: Wed, 17 May 2023 18:36:39 +0000 Subject: [PATCH] feature(backups): job progress logs --- selfprivacy_api/jobs/__init__.py | 37 +++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/selfprivacy_api/jobs/__init__.py b/selfprivacy_api/jobs/__init__.py index d7e4f31..5c3ea62 100644 --- a/selfprivacy_api/jobs/__init__.py +++ b/selfprivacy_api/jobs/__init__.py @@ -133,18 +133,27 @@ class Jobs: @staticmethod def log_status_update(job: Job, status: JobStatus): redis = RedisPool().get_connection() - key = _redis_log_key_from_uuid(job.uid) + key = _status_log_key_from_uuid(job.uid) if redis.exists(key): assert redis.type(key) == "list" redis.lpush(key, status.value) redis.expire(key, 10) + @staticmethod + def log_progress_update(job: Job, progress: int): + redis = RedisPool().get_connection() + key = _progress_log_key_from_uuid(job.uid) + if redis.exists(key): + assert redis.type(key) == "list" + redis.lpush(key, progress) + redis.expire(key, 10) + @staticmethod def status_updates(job: Job) -> typing.List[JobStatus]: result = [] redis = RedisPool().get_connection() - key = _redis_log_key_from_uuid(job.uid) + key = _status_log_key_from_uuid(job.uid) if not redis.exists(key): return [] @@ -156,6 +165,23 @@ class Jobs: raise ValueError("impossible job status: " + status) from e return result + @staticmethod + def progress_updates(job: Job) -> typing.List[int]: + result = [] + + redis = RedisPool().get_connection() + key = _progress_log_key_from_uuid(job.uid) + if not redis.exists(key): + return [] + + progress_strings = redis.lrange(key, 0, -1) + for progress in progress_strings: + try: + result.append(int(progress)) + except KeyError as e: + raise ValueError("impossible job progress: " + progress) from e + return result + @staticmethod def update( job: Job, @@ -178,6 +204,7 @@ class Jobs: job.status_text = status_text if progress is not None: job.progress = progress + Jobs.log_progress_update(job, progress) job.status = status Jobs.log_status_update(job, status) job.updated_at = datetime.datetime.now() @@ -235,10 +262,14 @@ def _redis_key_from_uuid(uuid_string): return "jobs:" + str(uuid_string) -def _redis_log_key_from_uuid(uuid_string): +def _status_log_key_from_uuid(uuid_string): return STATUS_LOGS_PREFIX + str(uuid_string) +def _progress_log_key_from_uuid(uuid_string): + return PROGRESS_LOGS_PREFIX + str(uuid_string) + + def _store_job_as_hash(redis, redis_key, model): for key, value in model.dict().items(): if isinstance(value, uuid.UUID):