@@ -0,0 +1,2127 @@
+from celery.task.control import inspect%0Afrom errno import errorcode%0Afrom mariner import models%0Afrom mariner.util import which%0Afrom pecan import render%0Afrom pecan.hooks import PecanHook%0Afrom sqlalchemy.exc import OperationalError%0Afrom webob.exc import WSGIHTTPException%0Aimport logging%0A%0A%0Alogger = logging.getLogger(__name__)%0A%0A%0Aclass SystemCheckError(Exception):%0A%0A    def __init__(self, message):%0A        self.message = message%0A%0A%0Adef ansible_exists():%0A    ansible_path = which('ansible-playbook')%0A    if not ansible_path:%0A        raise SystemCheckError('Could not find ansible in system paths')%0A%0A%0Adef celery_has_workers():%0A    stats = inspect().stats()%0A    if not stats:%0A        raise SystemCheckError('No running Celery worker were found')%0A%0A%0Adef rabbitmq_is_running():%0A    try:%0A        celery_has_workers()%0A    except IOError as e:%0A        msg = %22Error connecting to rabbitmq: %22 + str(e)%0A        if len(e.args):%0A            if errorcode.get(e.args%5B0%5D) == 'ECONNREFUSED':%0A                msg = %22RabbitMQ is not running or not reachable%22%0A        raise SystemCheckError(msg)%0A%0A%0Adef database_connection():%0A    try:%0A        models.Task.get(1)%0A    except OperationalError:%0A        raise SystemCheckError(%22Could not connect or retrieve information from the database%22)%0A%0A%0Aclass SystemCheckHook(PecanHook):%0A    %22%22%22%0A    Perform a series of system checks which prevents the service from doing any%0A    work unless everything required checks out.%0A    %22%22%22%0A%0A    def before(self, state):%0A        %22%22%22%0A        Executed before a controller gets called. When an error condition is%0A        detected (one of the callables raises a %60%60SystemCheckError%60%60) it sets%0A        the response status to 500 and returns a JSON response with the%0A        appropriate reason.%0A        %22%22%22%0A        for check in %5Bansible_exists, rabbitmq_is_running, celery_has_workers,%0A                      database_connection%5D:%0A            try:%0A                check()%0A            except SystemCheckError as system_error:%0A                message = render('json', %7B'message': system_error.message%7D)%0A                raise WSGIHTTPException(content_type='application/json', body=message)%0A

create a system check hook with some checks