Last active
December 14, 2015 11:01
-
-
Save philipnrmn/9796a29a8bba96298f0f to your computer and use it in GitHub Desktop.
Fails marathon healthchecks predictably
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/local/bin/python3 | |
# -*- coding: utf-8 -*- | |
from flask import Flask, redirect, url_for | |
from os import environ | |
''' | |
Usage: | |
$ pip3 install flask | |
$ PORT=8001 python3 malinger.py | |
''' | |
app = Flask(__name__) | |
html = '''<!doctype html> | |
<title>Malinger</title> | |
<h1>Healthy: {}</h1> | |
<form action="/toggle" method="post"> | |
<button type="submit">Toggle</button> | |
</form> | |
''' | |
port = int(environ.get('PORT', 8000)) | |
is_healthy = bool(int(environ.get('HEALTHY', '1'))) | |
@app.route('/') | |
def health(): | |
if is_healthy: | |
return html.format(is_healthy), 200 | |
else: | |
return html.format(is_healthy), 500 | |
@app.route('/toggle', methods=['POST']) | |
def toggle(): | |
global is_healthy | |
is_healthy = not is_healthy | |
return redirect(url_for('health')) | |
app.run(host="0.0.0.0", port=port) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment