Skip to content

Commit

Permalink
Add simple Good-to-Go healthcheck
Browse files Browse the repository at this point in the history
Only check the flask app and the mongo database is available
  • Loading branch information
satterly committed Dec 2, 2016
1 parent 41ff129 commit 645952f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
11 changes: 10 additions & 1 deletion alerta/app/database/mongo.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def connect(self):
mongo_uri = mongo_uri or app.config['MONGO_URI'] # use app config if no env var overrides

try:
self.connection = MongoClient(mongo_uri, connect=False)
self.connection = MongoClient(mongo_uri, serverSelectionTimeoutMS=2000, connect=False)
except Exception as e:
LOG.error('MongoDB Client: %s : %s', mongo_uri, e)
sys.exit(1)
Expand Down Expand Up @@ -80,6 +80,15 @@ def get_version(self):

return self.db.client.server_info()['version']

def is_alive(self):

from pymongo.errors import ConnectionFailure
try:
self.db.client.admin.command('ismaster')
except ConnectionFailure:
return False
return True

def disconnect(self):

self.connection.close()
Expand Down
10 changes: 10 additions & 0 deletions alerta/app/management/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,16 @@ def switchboard():
return render_template('management/switchboard.html', switches=switches)


@app.route('/management/gtg', methods=['OPTIONS', 'GET'])
@cross_origin()
def good_to_go():

if db.is_alive():
return 'OK'
else:
return 'FAILED', 503


@app.route('/management/healthcheck', methods=['OPTIONS', 'GET'])
@cross_origin()
def health_check():
Expand Down

0 comments on commit 645952f

Please sign in to comment.