Skip to content

Commit

Permalink
Clarify API health checks and smoke tests (#332)
Browse files Browse the repository at this point in the history
Resolves #116.
  • Loading branch information
72636c authored Jan 26, 2021
1 parent d5afa4d commit e7254c1
Show file tree
Hide file tree
Showing 16 changed files with 75 additions and 25 deletions.
5 changes: 5 additions & 0 deletions .changeset/gorgeous-dancers-buy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'skuba': patch
---

**template/\*-rest-api:** Explicitly register `listen.ts`
5 changes: 5 additions & 0 deletions .changeset/pink-bikes-roll.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'skuba': patch
---

**template/\*-rest-api:** Clarify health checks and smoke tests
4 changes: 2 additions & 2 deletions template/express-rest-api/gantry.apply.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pagerDutyEndpoint: '{{values "pagerDutyEndpoint"}}'
authentication:
ignoreRoutes:
- /health
- /
- /smoke

autoScaling:
maxCount: {{values "maxInstanceCount"}}
Expand Down Expand Up @@ -112,7 +112,7 @@ deployment:
# Ignore alarms otherwise we can't deploy to a failed environment (SEEK-Jobs/gantry#488)
ignoreAlarms: true
smokeTest:
path: /health
path: /smoke
useExternalDns: true

tags:
Expand Down
11 changes: 11 additions & 0 deletions template/express-rest-api/src/api/healthCheck.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Handler } from 'express';

/**
* Signifies that the API is available to serve requests.
*
* The deployment environment calls this endpoint to see if the container is
* unhealthy and needs to be recycled.
*/
export const healthCheckHandler: Handler = (_req, res) => {
res.send('');
};
10 changes: 10 additions & 0 deletions template/express-rest-api/src/api/smokeTest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Handler } from 'express';

/**
* Tests connectivity to ensure appropriate access and network configuration.
*/
export const smokeTestHandler: Handler = async (_req, res) => {
await Promise.all([]);

res.send('');
};
2 changes: 0 additions & 2 deletions template/express-rest-api/src/app.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,5 @@ import app from './app';
const agent = request.agent(app);

describe('app', () => {
it('Hello World!', () => agent.get('/').expect(200, 'Hello World!'));

it('has a happy health check', () => agent.get('/health').expect(200, ''));
});
21 changes: 7 additions & 14 deletions template/express-rest-api/src/app.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
import 'skuba-dive/register';
import './register';

import express, { Application, Request, Response } from 'express';
import express from 'express';

import { healthCheckHandler } from './api/healthCheck';
import { smokeTestHandler } from './api/smokeTest';
import { config } from './config';
import { rootLogger } from './framework/logging';

const app: Application = express();

app.get('/', (_req: Request, res: Response) => {
rootLogger.debug('greeting...');

res.send('Hello World!');
});

app.get('/health', (_req: Request, res: Response) => {
res.send('');
});
const app = express()
.get('/health', healthCheckHandler)
.get('/smoke', smokeTestHandler);

export default Object.assign(app, {
port: config.port,
Expand Down
2 changes: 2 additions & 0 deletions template/express-rest-api/src/listen.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import './register';

import app from './app';
import { rootLogger } from './framework/logging';

Expand Down
1 change: 1 addition & 0 deletions template/express-rest-api/src/register.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import 'skuba-dive/register';
1 change: 0 additions & 1 deletion template/koa-rest-api/gantry.apply.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ authentication:
ignoreRoutes:
- /health
- /smoke
- /jobs{/**}

autoScaling:
maxCount: {{values "maxInstanceCount"}}
Expand Down
6 changes: 6 additions & 0 deletions template/koa-rest-api/src/api/healthCheck.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { Middleware } from 'src/types/koa';

/**
* Signifies that the API is available to serve requests.
*
* The deployment environment calls this endpoint to see if the container is
* unhealthy and needs to be recycled.
*/
export const healthCheckHandler: Middleware = (ctx) => {
ctx.state.skipRequestLogging = true;

Expand Down
10 changes: 9 additions & 1 deletion template/koa-rest-api/src/api/smokeTest.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
import { smokeTestJobStorage } from 'src/storage/jobs';
import { Middleware } from 'src/types/koa';

export const smokeTestHandler: Middleware = (ctx) => (ctx.body = '');
/**
* Tests connectivity to ensure appropriate access and network configuration.
*/
export const smokeTestHandler: Middleware = async (ctx) => {
await Promise.all([smokeTestJobStorage]);

ctx.body = '';
};
12 changes: 7 additions & 5 deletions template/koa-rest-api/src/app.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import 'skuba-dive/register';
import './register';

import { router } from 'src/api';
import { config } from 'src/config';
import { createApp } from 'src/framework/server';
import { router } from './api';
import { config } from './config';
import { createApp } from './framework/server';

const app = createApp(router.allowedMethods(), router.routes());

export default Object.assign(app, { port: config.port });
export default Object.assign(app, {
port: config.port,
});
2 changes: 2 additions & 0 deletions template/koa-rest-api/src/listen.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import './register';

import app from './app';
import { rootLogger } from './framework/logging';

Expand Down
1 change: 1 addition & 0 deletions template/koa-rest-api/src/register.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import 'skuba-dive/register';
7 changes: 7 additions & 0 deletions template/koa-rest-api/src/storage/jobs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,10 @@ export const readJobs = (): Promise<Job[]> => {

return Promise.resolve(jobs);
};

/**
* Tests that we have access to read from and write to our storage.
*/
export const smokeTestJobStorage = async (): Promise<void> => {
await Promise.resolve();
};

0 comments on commit e7254c1

Please sign in to comment.