Skip to content

Instantly share code, notes, and snippets.

@JeanBarriere
Last active November 5, 2019 11:05
Show Gist options
  • Save JeanBarriere/37b7557db219a60571ad8143418796e7 to your computer and use it in GitHub Desktop.
Save JeanBarriere/37b7557db219a60571ad8143418796e7 to your computer and use it in GitHub Desktop.
pingdom as a story
# 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 <inkie@storyscript.io>"
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."
@JeanBarriere
Copy link
Author

JeanBarriere commented Oct 23, 2019

This story requires the following env variables:

  • psql
    • POSTGRES_DSN
  • jean/mailgun-that-just-works
    • DOMAIN
    • API_KEY

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment