From 71b5e1ef87313b1c122116b56a03bee600b6f17a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C4=93teris=20Caune?= Date: Mon, 20 Jul 2015 23:56:54 +0300 Subject: [PATCH] MySQL version of trigger, setup instructions in README --- README.md | 48 ++++++++++++++++-- hc/api/management/commands/ensuretriggers.py | 53 +++++++++++++------- 2 files changed, 80 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index a261af0b..3aa4bd87 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,53 @@ # healthchecks -healthchecks is a watchdog for your cron jobs. It's a web server that listens for pings from your cron jobs, plus a web interface. +healthchecks is a watchdog for your cron jobs. It's a web server that listens for pings from your cron jobs, plus a web interface. It is live here: [http://healthchecks.io/](http://healthchecks.io/) The building blocks are: -* Python 3 -* virtualenv +* Python 2 or Python 3 * Django 1.8 -* PostgreSQL +* PostgreSQL or MySQL + +## Setting Up for Development + +These are instructions for setting up HealthChecks Django app +in development environment. + +* prepare directory for project code and virtualenv: + + $ mkdir -p ~/webapps + $ cd ~/webapps + +* prepare virtual environment + (with virtualenv you get pip, we'll use it soon to install requirements): + + $ virtualenv --python=python3 hc-venv + $ source hc-venv/bin/activate + +* check out project code: + + $ git clone git@github.com:healthchecks/healthchecks.git + +* install requirements (Django, ...) into virtualenv: + + $ pip install -r healthchecks/requirements.txt + +* make sure PostgreSQL server is installed and running, create + database "hc": + + $ psql --user postgres + postgres=# create database hc; + +* create database tables, triggers, superuser: + + $ cd ~/webapps/healthchecks + $ ./manage.py migrate + $ ./manage.py ensuretriggers + $ ./manage.py createsuperuser + +* run development server: + + $ ./manage.py runserver diff --git a/hc/api/management/commands/ensuretriggers.py b/hc/api/management/commands/ensuretriggers.py index f9406aea..87321dd3 100644 --- a/hc/api/management/commands/ensuretriggers.py +++ b/hc/api/management/commands/ensuretriggers.py @@ -2,26 +2,45 @@ from django.core.management.base import BaseCommand from django.db import connection +def _pg(cursor): + cursor.execute(""" + CREATE OR REPLACE FUNCTION update_alert_after() + RETURNS trigger AS $update_alert_after$ + BEGIN + IF NEW.last_ping IS NOT NULL THEN + NEW.alert_after := NEW.last_ping + NEW.timeout + NEW.grace; + END IF; + RETURN NEW; + END; + $update_alert_after$ LANGUAGE plpgsql; + + DROP TRIGGER IF EXISTS update_alert_after ON api_check; + + CREATE TRIGGER update_alert_after + BEFORE INSERT OR UPDATE OF last_ping, timeout, grace ON api_check + FOR EACH ROW EXECUTE PROCEDURE update_alert_after(); + """) + + +def _mysql(cursor): + cursor.execute(""" + DROP TRIGGER IF EXISTS update_alert_after; + + CREATE TRIGGER update_alert_after + BEFORE UPDATE ON api_check + FOR EACH ROW SET NEW.alert_after = NEW.last_ping + INTERVAL (NEW.timeout + NEW.grace) MICROSECOND; + """) + + class Command(BaseCommand): help = 'Ensures triggers exist in database' def handle(self, *args, **options): cursor = connection.cursor() - cursor.execute(""" -CREATE OR REPLACE FUNCTION update_alert_after() -RETURNS trigger AS $update_alert_after$ - BEGIN - IF NEW.last_ping IS NOT NULL THEN - NEW.alert_after := NEW.last_ping + NEW.timeout + NEW.grace; - END IF; - RETURN NEW; - END; -$update_alert_after$ LANGUAGE plpgsql; - -DROP TRIGGER IF EXISTS update_alert_after ON api_check; - -CREATE TRIGGER update_alert_after -BEFORE INSERT OR UPDATE OF last_ping, timeout, grace ON api_check -FOR EACH ROW EXECUTE PROCEDURE update_alert_after(); - """) + if connection.vendor == "postgresql": + _pg(cursor) + print("Created PostgreSQL trigger") + if connection.vendor == "mysql": + _mysql(cursor) + print("Created MySQL trigger")