Skip to content

Commit

Permalink
Add support for customising the age of jobs that are deleted by the d…
Browse files Browse the repository at this point in the history
…elete_old_jobs command
  • Loading branch information
j4mie committed Apr 9, 2024
1 parent 2022b76 commit 4261e8d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
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 4261e8d

Please sign in to comment.