Add TTL to storage of finished or failed jobs

Defaulting to 10 days.
This commit is contained in:
Houkime 2022-11-23 16:29:50 +00:00
parent 244851c7cc
commit d6ef01c0c7
2 changed files with 18 additions and 0 deletions

View file

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

View file

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