Browse Source

MySQL version of trigger, setup instructions in README

pull/7/head
Pēteris Caune 9 years ago
parent
commit
71b5e1ef87
2 changed files with 80 additions and 21 deletions
  1. +44
    -4
      README.md
  2. +36
    -17
      hc/api/management/commands/ensuretriggers.py

+ 44
- 4
README.md View File

@ -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 [email protected]: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

+ 36
- 17
hc/api/management/commands/ensuretriggers.py View File

@ -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")

Loading…
Cancel
Save