Skip to content

Commit

Permalink
Add ECS health checks for API tasks (#3632)
Browse files Browse the repository at this point in the history
* Refactor GET /api/health endpoint handler

* Add healthcheck script

* Configure ECS healthcheck for API tasks

* Update tests for healthcheck endpoint

* Use request logger

* Bump query execution timeout to 1s
  • Loading branch information
TylerHendrickson authored Dec 13, 2024
1 parent 1babe4f commit 58eed8b
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 7 deletions.
2 changes: 1 addition & 1 deletion packages/server/__tests__/api/health.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ describe('/api/health', () => {
await server
.get('/api/health')
.expect(200)
.expect({ success: true, db: 'OK' });
.expect({ success: true });
});
});
24 changes: 18 additions & 6 deletions packages/server/src/routes/health.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,24 @@ const router = express.Router();
const knex = require('../db/connection');

router.get('/', async (req, res) => {
// if DB call fails, this will throw and health route will 500
const dbResult = await knex
.raw('SELECT \'OK\' AS ok')
.timeout(500, { cancel: true });

res.json({ success: true, db: dbResult.rows[0].ok });
const logger = req.log.child({ ip: req.ip, healthcheck: true });
logger.debug('starting healthcheck');
let success = false;
try {
// if DB call fails, this will throw and health route will 500
const dbHealth = await knex
.raw(`SELECT 'ok' AS healthcheck_result`)
.timeout(1000, { cancel: true });
success = true;
logger.debug(
{ success, db_result: dbHealth.rows[0] },
'received healthcheck result from database',
);
} catch (err) {
logger.error({ success: false, err }, 'healthcheck failed');
throw err;
}
res.json({ success });
});

module.exports = router;
20 changes: 20 additions & 0 deletions packages/server/src/scripts/healthcheck.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Usage: node healthcheck.js [url] [timeout]
// Exits with error (status code 1) when HTTP request errors or responds with non-200 status.
const got = require('got');

const url = process.argv[2] || 'http://localhost:3000/api/health';
const timeout = Number.parseInt(process.argv[3], 10) || 5000;

console.log(url);

got(url, { timeout }).then((res) => {
const { statusCode } = res;
console.log(res.statusCode);
if (statusCode !== 200) {
process.exit(1);
}
process.exit(0);
}).catch((e) => {
console.error(e);
process.exit(1);
});
12 changes: 12 additions & 0 deletions terraform/modules/gost_api/task.tf
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@ module "api_container_definition" {
condition = "START"
}]

healthcheck = {
command = ["CMD-SHELL", join(" ", [
"node ./src/scripts/healthcheck.js",
"http://localhost:${local.api_container_port}/api/health 5000",
"|| exit 1"
])]
startPeriod = 10
interval = 30
timeout = 10
retries = 2
}

linux_parameters = {
capabilities = {
add = []
Expand Down

0 comments on commit 58eed8b

Please sign in to comment.