From 0912ac183198f7882d60dafd1d3d1333043752d9 Mon Sep 17 00:00:00 2001 From: Houkime <> Date: Fri, 22 Sep 2023 17:56:04 +0000 Subject: [PATCH] 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]