Skip to content

Commit

Permalink
Merge pull request #63 from dabapps/parameterise-delete-old-jobs
Browse files Browse the repository at this point in the history
Add support for customising the age of jobs that are deleted by the delete_old_jobs command
  • Loading branch information
j4mie authored Apr 9, 2024
2 parents 2022b76 + 9bc282e commit 9ddb9a2
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 7 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,7 @@ in the dict returned by this method.
##### manage.py delete_old_jobs
There is a management command, `manage.py delete_old_jobs`, which deletes any
jobs from the database which are in state `COMPLETE` or `FAILED` and were
created more than 24 hours ago. This could be run, for example, as a cron task,
to ensure the jobs table remains at a reasonable size.
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.

##### manage.py worker
To start a worker:
Expand Down
11 changes: 10 additions & 1 deletion django_dbq/management/commands/delete_old_jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ class Command(BaseCommand):

help = "Delete old jobs"

def add_arguments(self, parser):
parser.add_argument(
"--hours",
help="Delete jobs older than this many hours",
default=None,
required=False,
type=int,
)

def handle(self, *args, **options):
Job.objects.delete_old()
Job.objects.delete_old(hours=options["hours"])
self.stdout.write("Deleted old jobs")
8 changes: 4 additions & 4 deletions django_dbq/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
logger = logging.getLogger(__name__)


DELETE_JOBS_AFTER_HOURS = 24
DEFAULT_DELETE_JOBS_AFTER_HOURS = 24


class JobManager(models.Manager):
Expand Down Expand Up @@ -49,17 +49,17 @@ def get_ready_or_none(self, queue_name, max_retries=3):
retries_left,
)

def delete_old(self):
def delete_old(self, hours=None):
"""
Delete all jobs older than DELETE_JOBS_AFTER_HOURS
Delete all jobs older than hours, or DEFAULT_DELETE_JOBS_AFTER_HOURS
"""
delete_jobs_in_states = [
Job.STATES.FAILED,
Job.STATES.COMPLETE,
Job.STATES.STOPPING,
]
delete_jobs_created_before = timezone.now() - datetime.timedelta(
hours=DELETE_JOBS_AFTER_HOURS
hours=hours or DEFAULT_DELETE_JOBS_AFTER_HOURS
)
logger.info(
"Deleting all job in states %s created before %s",
Expand Down
14 changes: 14 additions & 0 deletions django_dbq/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,3 +371,17 @@ def test_delete_old_jobs(self):
self.assertEqual(Job.objects.count(), 2)
self.assertTrue(j4 in Job.objects.all())
self.assertTrue(j5 in Job.objects.all())

def test_delete_old_jobs_with_custom_hours_argument(self):
j1 = Job.objects.create(name="testjob", state=Job.STATES.COMPLETE)
j1.created = timezone.now() - timedelta(days=5)
j1.save()

j2 = Job.objects.create(name="testjob", state=Job.STATES.COMPLETE)
j2.created = timezone.now() - timedelta(days=3)
j2.save()

Job.objects.delete_old(hours=24 * 4)

self.assertEqual(Job.objects.count(), 1)
self.assertTrue(j2 in Job.objects.all())

0 comments on commit 9ddb9a2

Please sign in to comment.