From 0912ac183198f7882d60dafd1d3d1333043752d9 Mon Sep 17 00:00:00 2001 From: Houkime <> Date: Fri, 22 Sep 2023 17:56:04 +0000 Subject: [PATCH 1/3] feature(jobs): set ttl via method --- selfprivacy_api/jobs/__init__.py | 8 ++++++++ tests/test_jobs.py | 6 ++++++ 2 files changed, 14 insertions(+) diff --git a/selfprivacy_api/jobs/__init__.py b/selfprivacy_api/jobs/__init__.py index 3fe452b..05b5ab8 100644 --- a/selfprivacy_api/jobs/__init__.py +++ b/selfprivacy_api/jobs/__init__.py @@ -224,6 +224,14 @@ class Jobs: return job + @staticmethod + def set_expiration(job: Job, expiration_seconds: int) -> Job: + redis = RedisPool().get_connection() + key = _redis_key_from_uuid(job.uid) + if redis.exists(key): + redis.expire(key, expiration_seconds) + return job + @staticmethod def get_job(uid: str) -> typing.Optional[Job]: """ diff --git a/tests/test_jobs.py b/tests/test_jobs.py index 0a4271e..c0e2125 100644 --- a/tests/test_jobs.py +++ b/tests/test_jobs.py @@ -49,6 +49,12 @@ def test_remove_get_nonexistent(jobs_with_one_job): assert jobs_with_one_job.get_job(uid_str) is None +def test_set_zeroing_ttl(jobs_with_one_job): + test_job = jobs_with_one_job.get_jobs()[0] + jobs_with_one_job.set_expiration(test_job, 0) + assert jobs_with_one_job.get_jobs() == [] + + def test_jobs(jobs_with_one_job): jobs = jobs_with_one_job test_job = jobs_with_one_job.get_jobs()[0] From 26c0a8fafe3a76fddffedcbf2c5b8f6c21a78020 Mon Sep 17 00:00:00 2001 From: Houkime <> Date: Fri, 22 Sep 2023 18:09:39 +0000 Subject: [PATCH 2/3] feature(jobs): set 1 hour ttl for successful autobackup jobs --- selfprivacy_api/backup/__init__.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/selfprivacy_api/backup/__init__.py b/selfprivacy_api/backup/__init__.py index 72d1567..aa11f7f 100644 --- a/selfprivacy_api/backup/__init__.py +++ b/selfprivacy_api/backup/__init__.py @@ -56,6 +56,8 @@ BACKUP_PROVIDER_ENVS = { "location": "BACKUP_LOCATION", } +AUTOBACKUP_JOB_EXPIRATION_SECONDS = 60 * 60 # one hour + class NotDeadError(AssertionError): """ @@ -316,6 +318,8 @@ class Backups: raise error Jobs.update(job, status=JobStatus.FINISHED) + if reason in [BackupReason.AUTO, BackupReason.PRE_RESTORE]: + Jobs.set_expiration(job, AUTOBACKUP_JOB_EXPIRATION_SECONDS) return snapshot @staticmethod From ece3258c7858ebf42c08d339dbd2320152acab26 Mon Sep 17 00:00:00 2001 From: Houkime <> Date: Fri, 22 Sep 2023 18:13:22 +0000 Subject: [PATCH 3/3] test(jobs): test out setting ttl --- tests/test_jobs.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/test_jobs.py b/tests/test_jobs.py index c0e2125..64cf457 100644 --- a/tests/test_jobs.py +++ b/tests/test_jobs.py @@ -1,6 +1,7 @@ # pylint: disable=redefined-outer-name # pylint: disable=unused-argument import pytest +from time import sleep from selfprivacy_api.jobs import Jobs, JobStatus import selfprivacy_api.jobs as jobsmodule @@ -55,6 +56,14 @@ def test_set_zeroing_ttl(jobs_with_one_job): assert jobs_with_one_job.get_jobs() == [] +def test_not_zeroing_ttl(jobs_with_one_job): + test_job = jobs_with_one_job.get_jobs()[0] + jobs_with_one_job.set_expiration(test_job, 1) + assert len(jobs_with_one_job.get_jobs()) == 1 + sleep(1.2) + assert len(jobs_with_one_job.get_jobs()) == 0 + + def test_jobs(jobs_with_one_job): jobs = jobs_with_one_job test_job = jobs_with_one_job.get_jobs()[0]