From 7a4f43d1fac0d5e19b9499c47ae24587304ef8cb Mon Sep 17 00:00:00 2001 From: Alexandre Fayolle Date: Fri, 25 Oct 2019 14:44:47 +0200 Subject: [PATCH] [IMP] monitoring_status: check the db is reachable make a real SQL query on GET /monitoring/status and return a 503 if the query failed. --- monitoring_status/controllers/main.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/monitoring_status/controllers/main.py b/monitoring_status/controllers/main.py index b95c2ab..d6b0cdc 100644 --- a/monitoring_status/controllers/main.py +++ b/monitoring_status/controllers/main.py @@ -4,9 +4,12 @@ import logging import json +import psycopg2 + import werkzeug from odoo import http +from odoo.http import request from odoo.addons.web.controllers.main import ensure_db @@ -30,10 +33,24 @@ class Monitoring(http.Controller): @http.route('/monitoring/status', type='http', auth='none') def status(self): ensure_db() + http_status = 200 # TODO: add 'sub-systems' status and infos: # queue job, cron, database, ... headers = {'Content-Type': 'application/json'} info = {'status': 1} + # check the database connection + try: + cr = request.env.cr + cr.execute( + 'SELECT value ' + 'FROM ir_config_parameter ' + 'WHERE key=%s', + ('web.base.url',)) + result = cr.fetchone() + info['web.base.url'] = result or '' + except psycopg2.OperationalError as exc: + info['database_error'] = str(exc) + http_status = 503 session = http.request.session # We set a custom expiration of 1 second for this request, as we do a # lot of health checks, we don't want those anonymous sessions to be @@ -43,4 +60,6 @@ class Monitoring(http.Controller): # Redis. if not session.uid: session.expiration = 1 - return werkzeug.wrappers.Response(json.dumps(info), headers=headers) + return werkzeug.wrappers.Response( + json.dumps(info), status=http_status, headers=headers + )