/
rabbitmq.py
46 lines (34 loc) · 1.35 KB
/
rabbitmq.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import logging
from typing import Dict
from amqp.exceptions import AccessRefused
from kombu import Connection
import conf
from checks.base import Check
from checks.results import Result
from options.registry.core import CELERY_BROKER_BACKEND, CELERY_BROKER_URL
logger = logging.getLogger(__name__)
class RabbitMQCheck(Check):
@staticmethod
def check() -> Result:
"""Open and close the broker channel."""
if conf.get(CELERY_BROKER_BACKEND) == 'redis':
return Result()
try:
# Context to release connection
with Connection(conf.get(CELERY_BROKER_URL)) as conn:
conn.connect()
except ConnectionRefusedError:
return Result(message='Service unable to connect, "Connection was refused".',
severity=Result.ERROR)
except AccessRefused:
return Result(message='Service unable to connect, "Authentication error".',
severity=Result.ERROR)
except IOError:
return Result(message='Service has an "IOError".', severity=Result.ERROR)
except Exception as e:
return Result(message='Service has an "{}" error.'.format(e), severity=Result.ERROR)
else:
return Result()
@classmethod
def run(cls) -> Dict:
return {'RABBITMQ': cls.check()}