Skip to content

Commit

Permalink
Merge pull request #232 from uktrade/LTD-46684-separate-healthchecks-…
Browse files Browse the repository at this point in the history
…to-critical-non-critical

[LTD-4684] Ensure non critical healthchecks do not result in a failed health check response
  • Loading branch information
currycoder authored Feb 12, 2024
2 parents e6a3be4 + b2f46a1 commit bc7c064
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 15 deletions.
16 changes: 5 additions & 11 deletions healthcheck/checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@

from django.conf import settings
from django.utils import timezone

from health_check.backends import BaseHealthCheckBackend
from health_check.exceptions import HealthCheckException

from mail.enums import ReceptionStatusEnum
from mail.libraries.routing_controller import get_hmrc_to_dit_mailserver, get_spire_to_dit_mailserver
from mail.models import LicencePayload, Mail


logger = logging.getLogger(__name__)


Expand All @@ -28,35 +27,30 @@ def check_status(self):
except poplib.error_proto as e:
response, *_ = e.args
error_message = f"Failed to connect to mailbox: {mailserver.hostname} ({response})"
logger.error(error_message)
self.add_error(HealthCheckException(error_message))
finally:
mailserver.quit_pop3_connection()


class LicencePayloadsHealthCheck(BaseHealthCheckBackend):
critical_service = False

def check_status(self):
dt = timezone.now() + datetime.timedelta(seconds=settings.LICENSE_POLL_INTERVAL)
unprocessed_payloads = LicencePayload.objects.filter(is_processed=False, received_at__lte=dt)

for unprocessed_payload in unprocessed_payloads:
error_message = f"Payload object has been unprocessed for over {settings.LICENSE_POLL_INTERVAL} seconds: {unprocessed_payload}"
logger.error(error_message)
self.add_error(HealthCheckException(error_message))

if unprocessed_payloads.exists():
raise HealthCheckException("There are unprocessed licence payloads.")


class PendingMailHealthCheck(BaseHealthCheckBackend):
critical_service = False

def check_status(self):
dt = timezone.now() - datetime.timedelta(seconds=settings.EMAIL_AWAITING_REPLY_TIME)
pending_mails = Mail.objects.exclude(status=ReceptionStatusEnum.REPLY_SENT).filter(sent_at__lte=dt)

for pending_mail in pending_mails:
error_message = f"The following Mail has been pending for over {settings.EMAIL_AWAITING_REPLY_TIME} seconds: {pending_mail}"
logger.error(error_message)
self.add_error(HealthCheckException(error_message))

if pending_mails.exists():
raise HealthCheckException("There are pending mails unprocessed.")
10 changes: 6 additions & 4 deletions healthcheck/tests/test_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@ def test_unprocessed_payloads(self):
is_processed=False,
)
check = LicencePayloadsHealthCheck()
with self.assertRaises(HealthCheckException):
check.check_status()
check.check_status()
assert len(check.errors) == 1
assert "Payload object has been unprocessed for over" in check.errors[0].message

def test_all_payloads_processed(self):
LicencePayload.objects.create(
Expand All @@ -95,8 +96,9 @@ def test_unprocessed_pending_mails(self):
)

check = PendingMailHealthCheck()
with self.assertRaises(HealthCheckException):
check.check_status()
check.check_status()
assert len(check.errors) == 1
assert "The following Mail has been pending for over" in check.errors[0].message

def test_no_unprocessed_pending_mails(self):
Mail.objects.create(
Expand Down

0 comments on commit bc7c064

Please sign in to comment.