feature(backups): job progress logs

pull/35/head
Houkime 2023-05-17 18:36:39 +00:00 committed by Inex Code
parent ca036b294a
commit 135fb0c42d
1 changed files with 34 additions and 3 deletions

View File

@ -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):