From 757e46633592c382fdcaabf4f21e1a9a9c84d2f3 Mon Sep 17 00:00:00 2001 From: Jens Diemer Date: Fri, 18 Aug 2023 09:10:42 +0200 Subject: [PATCH] Fix #127: Catch error getting HUEY counts Fix for https://github.com/boxine/django-huey-monitor/issues/127 --- huey_monitor/__init__.py | 2 +- huey_monitor/templatetags/huey_monitor.py | 22 ++++++++++++++----- .../tests/test_templatetags.py | 15 +++++++++++++ 3 files changed, 32 insertions(+), 7 deletions(-) diff --git a/huey_monitor/__init__.py b/huey_monitor/__init__.py index c6cc3f4..d2598a6 100644 --- a/huey_monitor/__init__.py +++ b/huey_monitor/__init__.py @@ -4,5 +4,5 @@ Django based tool for monitoring huey task queue: https://github.com/coleifer/huey """ -__version__ = '0.7.0' +__version__ = '0.7.1' __author__ = 'Jens Diemer ' diff --git a/huey_monitor/templatetags/huey_monitor.py b/huey_monitor/templatetags/huey_monitor.py index d0fd613..6e565fa 100644 --- a/huey_monitor/templatetags/huey_monitor.py +++ b/huey_monitor/templatetags/huey_monitor.py @@ -1,16 +1,26 @@ +import logging + from django import template from django.template.loader import render_to_string from huey.contrib.djhuey import HUEY +logger = logging.getLogger(__name__) register = template.Library() @register.simple_tag def huey_counts_info(): - context = dict( - huey_pending_count=HUEY.pending_count(), - huey_scheduled_count=HUEY.scheduled_count(), - huey_result_count=HUEY.result_count(), - ) - return render_to_string('admin/huey_monitor/huey_counts_info.html', context) + try: + context = dict( + huey_pending_count=HUEY.pending_count(), + huey_scheduled_count=HUEY.scheduled_count(), + huey_result_count=HUEY.result_count(), + ) + except OSError as err: + # e.g.: Redis down or other setup used see #127 + logger.exception('Failed to get counts from HUEY: %s', err) + + return f'

Huey counts: ({type(err).__name__}: {err})

' + else: + return render_to_string('admin/huey_monitor/huey_counts_info.html', context) diff --git a/huey_monitor_project/tests/test_templatetags.py b/huey_monitor_project/tests/test_templatetags.py index 974c18d..fa12dc7 100644 --- a/huey_monitor_project/tests/test_templatetags.py +++ b/huey_monitor_project/tests/test_templatetags.py @@ -11,3 +11,18 @@ def test_huey_counts_info(self): with patch('huey_monitor.templatetags.huey_monitor.HUEY', MemoryHuey()): html = huey_counts_info() self.assertHTMLEqual(html, '

Huey counts: Pending: 0, Scheduled: 0, Result: 0

') + + def test_huey_counts_info_redis_down(self): + class HueyError: + def __getattribute__(self, item): + raise OSError('Redis down!') + + with patch('huey_monitor.templatetags.huey_monitor.HUEY', HueyError()), self.assertLogs( + 'huey_monitor' + ) as logs: + html = huey_counts_info() + + self.assertHTMLEqual(html, '

Huey counts: (OSError: Redis down!)

') + log_message = logs.output[0] + self.assertIn('Traceback', log_message) + self.assertIn('raise OSError', log_message)