summaryrefslogtreecommitdiff
path: root/u/server/activity/health.ts
blob: b9efa3a9009836e8800bc8baa0d87c8113dde618 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import {
  type IEither,
  type ITraceable,
  JsonResponse,
  LogLevel,
  type Mapper,
  Metric,
  type PenguenoRequest,
  type ServerTrace,
  TraceUtil,
} from "@emprespresso/pengueno";

export enum HealthCheckInput {
  CHECK,
}
export enum HealthCheckOutput {
  YAASQUEEN,
}

const healthCheckMetric = Metric.fromName("Health");
export const HealthCheckActivity = (
  check: Mapper<
    ITraceable<HealthCheckInput, ServerTrace>,
    Promise<IEither<Error, HealthCheckOutput>>
  >,
) =>
(req: ITraceable<PenguenoRequest, ServerTrace>) =>
  req
    .bimap(TraceUtil.withFunctionTrace(HealthCheckActivity))
    .bimap(TraceUtil.withMetricTrace(healthCheckMetric))
    .flatMap((r) => r.move(HealthCheckInput.CHECK).map(check))
    .map(TraceUtil.promiseify((h) => {
      const health = h.get();
      health.mapBoth((e) => {
        h.trace.trace(healthCheckMetric.failure);
        h.trace.addTrace(LogLevel.ERROR).trace(`${e}`);
        return new JsonResponse(
          req,
          "oh no, i need to eat more vegetables (。•́︿•̀。)...",
          { status: 500 },
        );
      }, (_healthy) => {
        h.trace.trace(healthCheckMetric.success);
        const msg = `think im healthy!! (✿˘◡˘) ready to do work~`;
        h.trace.trace(msg);
        return new JsonResponse(
          req,
          msg,
          { status: 200 },
        );
      });
    }));