Skip to content

Commit

Permalink
Health handler pings db (#43)
Browse files Browse the repository at this point in the history
* [add] db check to health handler

* [change] set dev instances to 3

* [add] simplify dev deploy task

* [change] simplify health logic

* [change] make low-cost db query in health handler
  • Loading branch information
tngzng authored Apr 8, 2022
1 parent 24529f0 commit 61deda0
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 16 deletions.
14 changes: 10 additions & 4 deletions Karfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,16 @@ DOCKER_OPTS=$(cat <<END_HEREDOC
END_HEREDOC
)

#@cdk
#+Run cdk.
task-cdk() {
(cd cdk && npx cdk $@)
#@deploy
#+CDK deploy.
task-deploy() {
echo "Deploying all dev stacks..."
cd cdk
for stack in $(npx cdk list | grep Dev)
do
npx cdk deploy $stack
done
cd ..
}

#@build
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ kar test
For dev deployment, run:

```
kar cdk deploy DevArticleRecAPI
kar deploy
```

Each pull request to main will trigger a new prod deployment when merged.
Expand Down
10 changes: 0 additions & 10 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,6 @@ async def empty_metric_buffers():
)


async def restart_db_connection():
INTERVAL_MIN = 1
while True:
await asyncio.sleep(INTERVAL_MIN * 60)
if db_proxy.is_closed():
logging.info("Resetting db conn...")
db_proxy.connect()


if __name__ == "__main__":
if config.get("DEBUG") is True:
tornado.autoreload.start()
Expand All @@ -90,5 +81,4 @@ async def restart_db_connection():
http_server.listen(port)
io_loop = tornado.ioloop.IOLoop.current()
io_loop.add_callback(empty_metric_buffers)
io_loop.add_callback(restart_db_connection)
io_loop.start()
2 changes: 1 addition & 1 deletion cdk/lib/app-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export class AppStack extends cdk.Stack {
cluster,
cpu: 256,
memoryLimitMiB: 256,
desiredCount: props.stage === helpers.STAGE.PRODUCTION ? 3 : 1,
desiredCount: 3,
domainName,
domainZone,
certificate,
Expand Down
11 changes: 11 additions & 0 deletions handlers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

from lib.metrics import write_metric, Unit
from lib.config import config
from db.mappings.model import Model


DEFAULT_PAGE_SIZE = config.get("DEFAULT_PAGE_SIZE")
# buffer of latency values for each handler/site combination
Expand Down Expand Up @@ -104,6 +106,8 @@ def push_latency(self, latency, handler_name: str, site_name: str) -> None:
LATENCY_BUFFERS[key].push(latency)

def on_finish(self):
if self.handler_name == "Health":
return
latency = (time.time() - self.start_time) * 1000
if not 200 <= self._status_code < 300:
self.write_error_metric(latency)
Expand All @@ -124,6 +128,13 @@ class HealthHandler(BaseHandler):
"""Return 200 OK."""

def get(self):
try:
Model.select().limit(1).count()
except:
msg = "Can't connect to db"
logging.exception(msg)
raise tornado.web.HTTPError(status_code=500, log_message=msg)

self.finish("OK")


Expand Down

0 comments on commit 61deda0

Please sign in to comment.