From d6ef01c0c7f91665925a20fde4ec3c37e9e5a6dc Mon Sep 17 00:00:00 2001 From: Houkime <> Date: Wed, 23 Nov 2022 16:29:50 +0000 Subject: [PATCH] Add TTL to storage of finished or failed jobs Defaulting to 10 days. --- selfprivacy_api/jobs/__init__.py | 4 ++++ tests/test_jobs.py | 14 ++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/selfprivacy_api/jobs/__init__.py b/selfprivacy_api/jobs/__init__.py index 4fa820c..9e4d961 100644 --- a/selfprivacy_api/jobs/__init__.py +++ b/selfprivacy_api/jobs/__init__.py @@ -24,6 +24,8 @@ from pydantic import BaseModel from selfprivacy_api.utils.redis_pool import RedisPool +JOB_EXPIRATION_SECONDS = 10 * 24 * 60 * 60 # ten days + class JobStatus(Enum): """ @@ -150,6 +152,8 @@ class Jobs: key = redis_key_from_uuid(job.uid) if r.exists(key): store_job_as_hash(r, key, job) + if status in (JobStatus.FINISHED, JobStatus.ERROR): + r.expire(key, JOB_EXPIRATION_SECONDS) return job diff --git a/tests/test_jobs.py b/tests/test_jobs.py index 65d58e4..371dca4 100644 --- a/tests/test_jobs.py +++ b/tests/test_jobs.py @@ -3,6 +3,7 @@ import pytest from selfprivacy_api.jobs import Jobs, JobStatus +import selfprivacy_api.jobs as jobsmodule def test_jobs(authorized_client, jobs_file, shared_datadir): @@ -30,6 +31,19 @@ def test_jobs(authorized_client, jobs_file, shared_datadir): assert jobs.get_jobs() == [test_job] + backup = jobsmodule.JOB_EXPIRATION_SECONDS + jobsmodule.JOB_EXPIRATION_SECONDS = 0 + + jobs.update( + job=test_job, + status=JobStatus.FINISHED, + status_text="Yaaay!", + progress=100, + ) + + assert jobs.get_jobs() == [] + jobsmodule.JOB_EXPIRATION_SECONDS = backup + @pytest.fixture def mock_subprocess_run(mocker):