Skip to content

Commit

Permalink
Merge pull request chrisspen#91 from chrisspen/90
Browse files Browse the repository at this point in the history
Refactored log deletion code to work with MySQL.
  • Loading branch information
chrisspen authored May 4, 2017
2 parents 55e4541 + 13fbcf7 commit b77fa54
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
2 changes: 1 addition & 1 deletion chroniker/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
VERSION = (1, 0, 5)
VERSION = (1, 0, 6)
__version__ = '.'.join(map(str, VERSION))
10 changes: 6 additions & 4 deletions chroniker/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -849,10 +849,12 @@ def save(self, *args, **kwargs):
# Delete expired logs.
if self.maximum_log_entries:
cutoff = self.maximum_log_entries - 1
log_q = self.logs.all().order_by('-run_start_datetime')
qs = Log.objects.filter(id__in=log_q.values_list('id', flat=True)[cutoff:])
for o in qs:
o.delete()
log_q = self.logs.all().order_by('-run_start_datetime')[cutoff:]
if log_q.exists():
cutoff_dt = log_q[0].run_start_datetime
qs = Log.objects.filter(run_start_datetime__lte=cutoff_dt)
for o in qs:
o.delete()

def dependencies_met(self, running_ids=None):
"""
Expand Down
10 changes: 8 additions & 2 deletions chroniker/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
from django.utils import timezone
from django.contrib.auth.models import User
from django.conf import settings
from django.db.models import Max

from chroniker.models import Job, Log
# from chroniker.tests.commands import Sleeper, InfiniteWaiter, ErrorThrower
Expand Down Expand Up @@ -506,19 +507,24 @@ def testErrorCallback(self):
for log in job.logs.all():
print('log1:', log.id, log)
self.assertEqual(job.logs.all().count(), 11)
max_dt0 = job.logs.all().aggregate(Max('run_start_datetime'))['run_start_datetime__max']

# Set max log entries to very low number, and confirm all old log entries are deleted.
Job.objects.update()
job = Job.objects.get(id=6)
job.force_run = True
job.maximum_log_entries = 1
job.maximum_log_entries = 3
job.save()
call_command('cron', update_heartbeat=0, sync=1)
Job.objects.update()
Log.objects.update()
for log in job.logs.all():
print('log2:', log.id, log)
self.assertEqual(job.logs.all().count(), 1)
self.assertEqual(job.logs.all().count(), 3)
max_dt1 = job.logs.all().aggregate(Max('run_start_datetime'))['run_start_datetime__max']
print('max_dt0:', max_dt0)
print('max_dt1:', max_dt1)
self.assertTrue(max_dt1 > max_dt0)

def testCronQueue(self):

Expand Down

0 comments on commit b77fa54

Please sign in to comment.