Skip to content

Commit

Permalink
Merge pull request #128 from boxine/dev
Browse files Browse the repository at this point in the history
Fix #127: Catch error getting HUEY counts
  • Loading branch information
phihag authored Aug 18, 2023
2 parents c77df28 + 757e466 commit a6bfcc8
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
2 changes: 1 addition & 1 deletion huey_monitor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>'
22 changes: 16 additions & 6 deletions huey_monitor/templatetags/huey_monitor.py
Original file line number Diff line number Diff line change
@@ -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'<p>Huey counts: ({type(err).__name__}: {err})</p>'
else:
return render_to_string('admin/huey_monitor/huey_counts_info.html', context)
15 changes: 15 additions & 0 deletions huey_monitor_project/tests/test_templatetags.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, '<p>Huey counts: Pending: 0, Scheduled: 0, Result: 0</p>')

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, '<p>Huey counts: (OSError: Redis down!)</p>')
log_message = logs.output[0]
self.assertIn('Traceback', log_message)
self.assertIn('raise OSError', log_message)

0 comments on commit a6bfcc8

Please sign in to comment.