1import * as Kubernetes from "alchemy/Kubernetes";
2import * as Output from "alchemy/Output";
3import * as Effect from "effect/Effect";
4import * as Schedule from "effect/Schedule";
5import * as HttpClient from "effect/http/HttpClient";
6import * as HttpClientResponse from "effect/http/HttpClientResponse";
7import { Cluster, Namespace, Web } from "./infra.ts";
8
9export default Kubernetes.Job(
10 "HealthCheck",
11 Effect.gen(function* () {
12 const cluster = yield* Cluster;
13 const namespace = yield* Namespace;
14 return {
15 cluster,
16 main: import.meta.url,
17 name: "health-check",
18 namespace: namespace.name,
19 schedule: "*/5 * * * *",
20 };
21 }),
22 Effect.gen(function* () {
23 const web = yield* Web;
24 const webUrl =
25 yield* Output.interpolate`http://${web.serviceName}.${web.namespace}.svc.cluster.local:${web.port}`;
26
27 return {
28 run: Effect.gen(function* () {
29 const url = yield* webUrl;
30 const response = yield* HttpClient.get(`${url}/healthz`).pipe(
31 Effect.flatMap(HttpClientResponse.filterStatusOk),
32 Effect.retry({
33 schedule: Schedule.exponential("1 second"),
34 times: 5,
35 }),
36 );
37 yield* Effect.log(`${url} responded ${response.status}`);
38 }).pipe(Effect.orDie),
39 };
40 }),
41);