Last active
November 5, 2019 11:05
-
-
Save JeanBarriere/37b7557db219a60571ad8143418796e7 to your computer and use it in GitHub Desktop.
pingdom as a story
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
# pingdom as a story | |
### | |
# there is 2 levels on this story | |
# - CRUD on entries to ping | |
# - CRON on entries every minute | |
### | |
# crud /entries | |
http server as server | |
when server listen path:"/entries" as request | |
queries = psql exec query:"SELECT uuid,url,email,up FROM pingdom.entries" | |
request write content:queries | |
when server listen path:"/entry" method:"post" as request | |
email = request.body["email"] | |
url = request.body["url"] | |
try | |
query = (psql exec query:"INSERT INTO pingdom.entries (url, email) VALUES (%(url)s, %(email)s) RETURNING uuid" data:{ "email": email, "url": url }) to List[Map[string, string]] | |
request write content:{ "uuid": query[0]["uuid"] } to string | |
catch | |
request set_status code:420 | |
when server listen path:"/entry" method:"delete" as request | |
uuid = request.query_params["uuid"] | |
try | |
queries = psql exec query:"DELETE FROM pingdom.entries WHERE uuid=%(uuid)s" data:{ "uuid": uuid } | |
request set_status code:200 | |
catch | |
request set_status code:404 | |
# cron every minute | |
jean/cron job as job | |
when job schedule pattern:"0 * * * * *" | |
queries = psql exec query:"SELECT uuid,url,email,up FROM pingdom.entries" | |
from = "Inkie from Storyscript <[email protected]>" | |
foreach queries as entry | |
e = entry to Map[string, string] | |
url = e["url"] | |
to = e["email"] | |
uuid = e["uuid"] | |
up = e["up"] == "true" | |
try | |
http fetch url:url | |
if not up | |
query = psql exec query:"UPDATE pingdom.entries SET up=TRUE WHERE uuid=%(uuid)s RETURNING uuid" data:{ "uuid": uuid } | |
jean/mailgun-that-just-works send from:from :to subject:"{url} is up!" text:"Your domain {url} is responding normally." | |
catch | |
if up | |
query = psql exec query:"UPDATE pingdom.entries SET up=FALSE WHERE uuid=%(uuid)s RETURNING uuid" data:{ "uuid": uuid } | |
email = jean/mailgun-that-just-works send from:from :to subject:"{url} is down !" text:"We tried to access {url} but it didn't responded correctly." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This story requires the following env variables:
POSTGRES_DSN
DOMAIN
API_KEY