Skip to content

Commit

Permalink
Merge pull request chrisspen#89 from chrisspen/87
Browse files Browse the repository at this point in the history
Fixed max log entries param not deleting logs.
  • Loading branch information
chrisspen authored May 2, 2017
2 parents b8438bd + 963b694 commit 55e4541
Show file tree
Hide file tree
Showing 7 changed files with 42 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, 4)
VERSION = (1, 0, 5)
__version__ = '.'.join(map(str, VERSION))
3 changes: 2 additions & 1 deletion chroniker/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ def view_logs_button(self, obj=None):
id=obj.id,
count=q.count(),
)
return '<a href="{url}?job={id}" target="_blank" class="button">View&nbsp;{count}</a>'\
return '<a href="{url}?job__id__exact={id}" target="_blank" class="button">View&nbsp;{count}</a>'\
.format(**kwargs)
view_logs_button.allow_tags = True
view_logs_button.short_description = 'Logs'
Expand Down Expand Up @@ -511,6 +511,7 @@ class LogAdmin(admin.ModelAdmin):
list_filter = (
'success',
'on_time',
'job',
)

search_fields = (
Expand Down
4 changes: 2 additions & 2 deletions chroniker/management/commands/cron.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ class Command(BaseCommand):
make_option('--sync',
action='store_true',
default=False,
help='If given, runs job one at a time.'),
help='If given, runs jobs one at a time.'),
)

def create_parser(self, prog_name, subcommand):
Expand Down Expand Up @@ -336,7 +336,7 @@ def create_parser(self, prog_name, subcommand):
parser.add_argument('--sync',
action='store_true',
default=False,
help='If given, runs job one at a time.')
help='If given, runs jobs one at a time.')
self.add_arguments(parser)
return parser

Expand Down
4 changes: 3 additions & 1 deletion chroniker/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -848,8 +848,10 @@ 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')
for o in log_q[self.maximum_log_entries:]: # pylint: disable=invalid-slice-index
qs = Log.objects.filter(id__in=log_q.values_list('id', flat=True)[cutoff:])
for o in qs:
o.delete()

def dependencies_met(self, running_ids=None):
Expand Down
2 changes: 1 addition & 1 deletion chroniker/templates/admin/chroniker/job/change_form.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
{% if change %}{% if not is_popup %}
<ul class="object-tools">
<li><a href="history/" class="historylink">{% trans "History" %}</a></li>
<li><a href="../../log/?job={{ object_id }}" class="historylink">{% trans "Logs" %}</a></li>
<li><a href="../../log/?job__id__exact={{ object_id }}" class="historylink">{% trans "Logs" %}</a></li>
<li><a href="{% get_run_job_url object_id %}" class="viewsitelink">{% trans "Run Job" %}</a></li>
<li><a href="{% url 'admin:chroniker_job_duration_graph' object_id %}" target="_blank" class="viewsitelink">{% trans "View Duration Graph" %}</a></li>
{% if has_absolute_url %}
Expand Down
29 changes: 28 additions & 1 deletion chroniker/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,8 @@ def testMigration(self):

def testErrorCallback(self):

job = Job.objects.all().update(enabled=False)

while CALLBACK_ERRORS:
CALLBACK_ERRORS.pop(0)
self.assertFalse(CALLBACK_ERRORS)
Expand All @@ -489,10 +491,35 @@ def testErrorCallback(self):
job.save()

self.assertEqual(job.logs.all().count(), 0)
job.run(update_heartbeat=0)
#job.run(update_heartbeat=0)
call_command('cron', update_heartbeat=0, sync=1)
self.assertEqual(job.logs.all().count(), 1)
self.assertEqual(len(CALLBACK_ERRORS), 1)

# Simulate running the job 10 times.
for _ in range(10):
Job.objects.update()
job = Job.objects.get(id=6)
job.force_run = True
job.save()
call_command('cron', update_heartbeat=0, sync=1)
for log in job.logs.all():
print('log1:', log.id, log)
self.assertEqual(job.logs.all().count(), 11)

# 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.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)

def testCronQueue(self):

jobs = Job.objects.all()
Expand Down
5 changes: 5 additions & 0 deletions test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash
# Runs all tests.
set -e
./pep8.sh
export TESTNAME=; tox

0 comments on commit 55e4541

Please sign in to comment.