Skip to content

Commit

Permalink
add sqs and rmq checks
Browse files Browse the repository at this point in the history
  • Loading branch information
ut112 committed Oct 23, 2017
1 parent 0b5bc51 commit 4579469
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 5 deletions.
4 changes: 4 additions & 0 deletions health_check/contrib/rmq/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# -*- coding: utf-8 -*-


default_app_config = 'health_check.contrib.rmq.apps.HealthCheckConfig'
12 changes: 12 additions & 0 deletions health_check/contrib/rmq/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# -*- coding: utf-8 -*-
from django.apps import AppConfig

from health_check.plugins import plugin_dir


class HealthCheckConfig(AppConfig):
name = 'health_check.contrib.rmq'

def ready(self):
from .backends import RMQHealthCheck
plugin_dir.register(RMQHealthCheck)
16 changes: 16 additions & 0 deletions health_check/contrib/rmq/backends.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# -*- coding: utf-8 -*-
import logging
import pika

from django.conf import settings

from health_check.backends import BaseHealthCheckBackend
from health_check.exceptions import ServiceUnavailable

class RMQHealthCheck(BaseHealthCheckBackend):
def check_status(self):
logger = logging.getLogger(__name__)
try:
pika.BlockingConnection(pika.ConnectionParameters(settings.HEALTH_CHECK_CONF['rmq_host']))
except Exception, e:
raise ServiceUnavailable("Connection Error")
4 changes: 4 additions & 0 deletions health_check/contrib/sqs/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# -*- coding: utf-8 -*-


default_app_config = 'health_check.contrib.sqs.apps.HealthCheckConfig'
12 changes: 12 additions & 0 deletions health_check/contrib/sqs/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# -*- coding: utf-8 -*-
from django.apps import AppConfig

from health_check.plugins import plugin_dir


class HealthCheckConfig(AppConfig):
name = 'health_check.contrib.sqs'

def ready(self):
from .backends import SQSHealthCheck
plugin_dir.register(SQSHealthCheck)
22 changes: 22 additions & 0 deletions health_check/contrib/sqs/backends.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# -*- coding: utf-8 -*-
import logging
from django.conf import settings
from health_check.backends import BaseHealthCheckBackend
from health_check.exceptions import ServiceUnavailable
import boto3


class SQSHealthCheck(BaseHealthCheckBackend):
logger = logging.getLogger(__name__)

def check_status(self):
try:
sqs = boto3.resource('sqs',
region_name=settings.HEALTH_CHECK_CONF['region_name'],
aws_secret_access_key=settings.HEALTH_CHECK_CONF['aws_secret_access_key'],
aws_access_key_id=settings.HEALTH_CHECK_CONF['aws_access_key_id']
)
q = sqs.get_queue_by_name(QueueName=settings.HEALTH_CHECK_CONF['sqs_queue_name'])
except Exception, e:
ServiceUnavailable("connection error")

6 changes: 5 additions & 1 deletion health_check/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from django.http import JsonResponse
from django.views.decorators.cache import never_cache
from django.views.generic import TemplateView
from django.conf import settings

from health_check.plugins import plugin_dir

Expand All @@ -15,13 +16,16 @@ class MainView(TemplateView):
def get(self, request, *args, **kwargs):
plugins = []
errors = []
hard_dependency_errors = []
for plugin_class, options in plugin_dir._registry:
plugin = plugin_class(**copy.deepcopy(options))
plugin.run_check()
plugins.append(plugin)
errors += plugin.errors
if str(plugin.identifier()) not in settings.HEALTH_CHECK_CONF['soft_dependencies']:
hard_dependency_errors += plugin.errors
plugins.sort(key=lambda x: x.identifier())
status_code = 500 if errors else 200
status_code = 500 if hard_dependency_errors else 200

if 'application/json' in request.META.get('HTTP_ACCEPT', ''):
return self.render_to_response_json(plugins, status_code)
Expand Down
2 changes: 2 additions & 0 deletions requirements-dev.in
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ pep8
pep8-naming
pytest
pytest-django
pika
boto3
20 changes: 16 additions & 4 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,34 @@
#
amqp==2.2.2 # via kombu
billiard==3.5.0.3 # via celery
boto3==1.4.7
botocore==1.7.33 # via boto3, s3transfer
celery==4.1.0
configparser==3.5.0 # via flake8, pydocstyle
coverage==4.4.1
docutils==0.14 # via botocore
enum34==1.1.6 # via flake8
flake8==3.4.1
funcsigs==1.0.2 # via mock
futures==3.1.1 # via s3transfer
isort==4.2.15
jmespath==0.9.3 # via boto3, botocore
kombu==4.1.0 # via celery
mccabe==0.6.1
mock==2.0.0
pbr==3.1.1 # via mock
pbr==3.1.1 # via mock
pep8-naming==0.4.1
pep8==1.7.0
pika==0.11.0
py==1.4.34 # via pytest
pycodestyle==2.3.1 # via flake8
pydocstyle==2.1.1
pyflakes==1.6.0 # via flake8
pyflakes==1.5.0 # via flake8
pytest-django==3.1.2
pytest==3.2.3
pytz==2017.2 # via celery
six==1.11.0 # via mock
python-dateutil==2.6.1 # via botocore
pytz==2017.2 # via celery
s3transfer==0.1.11 # via boto3
six==1.11.0 # via mock, pydocstyle, python-dateutil
snowballstemmer==1.2.1 # via pydocstyle
vine==1.1.4 # via amqp

0 comments on commit 4579469

Please sign in to comment.