Skip to content

Commit

Permalink
healthcheck refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
gquadrati committed Sep 13, 2021
1 parent 3163c6f commit b7da8bc
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 14 deletions.
15 changes: 8 additions & 7 deletions Info/__tests__/handler.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as TE from "fp-ts/lib/TaskEither";

import { HealthCheck, HealthProblem } from "../../utils/healthcheck";
import * as healthcheck from "@pagopa/io-functions-commons/dist/src/utils/healthcheck";

import { InfoHandler } from "../handler";

afterEach(() => {
Expand All @@ -9,20 +10,20 @@ afterEach(() => {

describe("InfoHandler", () => {
it("should return an internal error if the application is not healthy", async () => {
const healthCheck: HealthCheck = TE.left([
"failure 1" as HealthProblem<"Config">,
"failure 2" as HealthProblem<"Config">
const healthCheck: healthcheck.HealthCheck = TE.left([
"failure 1" as healthcheck.HealthProblem<"Config">,
"failure 2" as healthcheck.HealthProblem<"Config">
]);
const handler = InfoHandler(healthCheck);
const handler = InfoHandler(() => healthCheck);

const response = await handler();

expect(response.kind).toBe("IResponseErrorInternal");
});

it("should return a success if the application is healthy", async () => {
const healthCheck: HealthCheck = TE.of(true);
const handler = InfoHandler(healthCheck);
const healthCheck: healthcheck.HealthCheck = TE.of(true);
const handler = InfoHandler(() => healthCheck);

const response = await handler();

Expand Down
19 changes: 16 additions & 3 deletions Info/handler.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/explicit-function-return-type */
import { wrapRequestHandler } from "@pagopa/io-functions-commons/dist/src/utils/request_middleware";
import * as express from "express";
import {
Expand All @@ -10,8 +11,9 @@ import {
import * as TE from "fp-ts/lib/TaskEither";
import { pipe } from "fp-ts/lib/function";

import * as healthcheck from "@pagopa/io-functions-commons/dist/src/utils/healthcheck";
import * as packageJson from "../package.json";
import { checkApplicationHealth, HealthCheck } from "../utils/healthcheck";
import { envConfig, IConfig } from "../utils/config";

interface IInfo {
readonly name: string;
Expand All @@ -22,11 +24,16 @@ type InfoHandler = () => Promise<
IResponseSuccessJson<IInfo> | IResponseErrorInternal
>;

type HealthChecker = (
config: unknown
) => healthcheck.HealthCheck<healthcheck.ProblemSource, true>;

// eslint-disable-next-line prefer-arrow/prefer-arrow-functions
export function InfoHandler(healthCheck: HealthCheck): InfoHandler {
export function InfoHandler(healthCheck: HealthChecker): InfoHandler {
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
return () =>
pipe(
envConfig,
healthCheck,
TE.mapLeft(problems => ResponseErrorInternal(problems.join("\n\n"))),
TE.map(_ =>
Expand All @@ -41,7 +48,13 @@ export function InfoHandler(healthCheck: HealthCheck): InfoHandler {

// eslint-disable-next-line prefer-arrow/prefer-arrow-functions
export function Info(): express.RequestHandler {
const handler = InfoHandler(checkApplicationHealth());
const handler = InfoHandler(
healthcheck.checkApplicationHealth(IConfig, [
c => healthcheck.checkAzureCosmosDbHealth(c.COSMOSDB_URI, c.COSMOSDB_KEY),
c => healthcheck.checkAzureStorageHealth(c.QueueStorageConnection),
c => healthcheck.checkUrlHealth(c.WEBHOOK_CHANNEL_URL)
])
);

return wrapRequestHandler(handler);
}
9 changes: 5 additions & 4 deletions utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ export const IConfig = t.intersection([

EMAIL_NOTIFICATION_SERVICE_BLACKLIST: CommaSeparatedListOf(ServiceId),
WEBHOOK_NOTIFICATION_SERVICE_BLACKLIST: CommaSeparatedListOf(ServiceId),

// eslint-disable-next-line sort-keys
IO_FUNCTIONS_ADMIN_API_TOKEN: NonEmptyString,
IO_FUNCTIONS_ADMIN_BASE_URL: NonEmptyString,
Expand Down Expand Up @@ -61,8 +60,7 @@ export const IConfig = t.intersection([
// This means that Date representation is in the past compared to the effectively switch Date we want to set
const DEFAULT_OPT_OUT_EMAIL_SWITCH_DATE = 1625781600;

// No need to re-evaluate this object for each call
const errorOrConfig: t.Validation<IConfig> = IConfig.decode({
export const envConfig = {
...process.env,

FF_DISABLE_INCOMPLETE_SERVICES: pipe(
Expand Down Expand Up @@ -93,7 +91,10 @@ const errorOrConfig: t.Validation<IConfig> = IConfig.decode({
E.toUnion
),
isProduction: process.env.NODE_ENV === "production"
});
};

// No need to re-evaluate this object for each call
const errorOrConfig: t.Validation<IConfig> = IConfig.decode(envConfig);

/**
* Read the application configuration and check for invalid values.
Expand Down

0 comments on commit b7da8bc

Please sign in to comment.