Skip to content

Commit 9ddb9a2

Browse files
authored
Merge pull request #63 from dabapps/parameterise-delete-old-jobs
Add support for customising the age of jobs that are deleted by the delete_old_jobs command
2 parents 2022b76 + 9bc282e commit 9ddb9a2

File tree

4 files changed

+29
-7
lines changed

4 files changed

+29
-7
lines changed

README.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,7 @@ in the dict returned by this method.
267267
##### manage.py delete_old_jobs
268268
There is a management command, `manage.py delete_old_jobs`, which deletes any
269269
jobs from the database which are in state `COMPLETE` or `FAILED` and were
270-
created more than 24 hours ago. This could be run, for example, as a cron task,
271-
to ensure the jobs table remains at a reasonable size.
270+
created more than (by default) 24 hours ago. This could be run, for example, as a cron task, to ensure the jobs table remains at a reasonable size. Use the `--hours` argument to control the age of jobs that will be deleted.
272271

273272
##### manage.py worker
274273
To start a worker:

django_dbq/management/commands/delete_old_jobs.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@ class Command(BaseCommand):
66

77
help = "Delete old jobs"
88

9+
def add_arguments(self, parser):
10+
parser.add_argument(
11+
"--hours",
12+
help="Delete jobs older than this many hours",
13+
default=None,
14+
required=False,
15+
type=int,
16+
)
17+
918
def handle(self, *args, **options):
10-
Job.objects.delete_old()
19+
Job.objects.delete_old(hours=options["hours"])
1120
self.stdout.write("Deleted old jobs")

django_dbq/models.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
logger = logging.getLogger(__name__)
1616

1717

18-
DELETE_JOBS_AFTER_HOURS = 24
18+
DEFAULT_DELETE_JOBS_AFTER_HOURS = 24
1919

2020

2121
class JobManager(models.Manager):
@@ -49,17 +49,17 @@ def get_ready_or_none(self, queue_name, max_retries=3):
4949
retries_left,
5050
)
5151

52-
def delete_old(self):
52+
def delete_old(self, hours=None):
5353
"""
54-
Delete all jobs older than DELETE_JOBS_AFTER_HOURS
54+
Delete all jobs older than hours, or DEFAULT_DELETE_JOBS_AFTER_HOURS
5555
"""
5656
delete_jobs_in_states = [
5757
Job.STATES.FAILED,
5858
Job.STATES.COMPLETE,
5959
Job.STATES.STOPPING,
6060
]
6161
delete_jobs_created_before = timezone.now() - datetime.timedelta(
62-
hours=DELETE_JOBS_AFTER_HOURS
62+
hours=hours or DEFAULT_DELETE_JOBS_AFTER_HOURS
6363
)
6464
logger.info(
6565
"Deleting all job in states %s created before %s",

django_dbq/tests.py

+14
Original file line numberDiff line numberDiff line change
@@ -371,3 +371,17 @@ def test_delete_old_jobs(self):
371371
self.assertEqual(Job.objects.count(), 2)
372372
self.assertTrue(j4 in Job.objects.all())
373373
self.assertTrue(j5 in Job.objects.all())
374+
375+
def test_delete_old_jobs_with_custom_hours_argument(self):
376+
j1 = Job.objects.create(name="testjob", state=Job.STATES.COMPLETE)
377+
j1.created = timezone.now() - timedelta(days=5)
378+
j1.save()
379+
380+
j2 = Job.objects.create(name="testjob", state=Job.STATES.COMPLETE)
381+
j2.created = timezone.now() - timedelta(days=3)
382+
j2.save()
383+
384+
Job.objects.delete_old(hours=24 * 4)
385+
386+
self.assertEqual(Job.objects.count(), 1)
387+
self.assertTrue(j2 in Job.objects.all())

0 commit comments

Comments
 (0)