Commit 78ab67eb authored by Grant's avatar Grant
Browse files

add healthchecks

parent dcb8455a
Loading
Loading
Loading
Loading
Loading
+28 −0
Original line number Diff line number Diff line
import { Router } from "express";
import { redis } from "../lib/redis.js";

const app = Router();
export default Router;

app.get("/", (req, res) => {
  res.sendStatus(200);
});

app.get("/worker", async (req, res) => {
  const alive = await redis.get("fediauth:WORKER_ALIVE");

  if (!alive) {
    // the key expired (set every minute to expire after 5m)
    res.json({
      status: "DOWN",
    });
    return;
  }

  const diff = Math.floor(parseInt(alive) - Date.now());

  res.json({
    // key is updated every 60 seconds, we're giving an error of +- 10s
    status: diff < 70_000 ? "UP" : "DEGRADED",
  });
});
+13 −0
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@ import { CronJob } from "cron";
import { AuthSession } from "../models/AuthSession.js";
import { APub } from "../apub/utils.js";
import { prisma } from "../lib/prisma.js";
import { redis } from "../lib/redis.js";

export class Jobs {
  private static instance: Jobs;
@@ -9,6 +10,13 @@ export class Jobs {
  static start() {
    this.instance = new Jobs();

    CronJob.from({
      name: "Healthcheck",
      cronTime: "* * * * *",
      onTick: this.instance.handleHealthcheck.bind(this),
      start: true,
    });

    CronJob.from({
      name: "Destroy expired tokens",
      cronTime: "*/5 * * * *",
@@ -24,6 +32,11 @@ export class Jobs {
    });
  }

  async handleHealthcheck() {
    // set the key every 60s and expire every 5m
    await redis.setex("fediauth:WORKER_ALIVE", 60 * 5, Date.now());
  }

  async handleExpiredTokens() {
    const expired = await AuthSession.getExpired();
    console.debug(`Deleting ${expired.length} expired tokens`);
+3 −0
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@ import { APIAdminRouter } from "./api_admin.js";
import { Handoff } from "../handoff/index.js";
import { DocLinks } from "../lib/DocLinks.js";
import DevController from "../controllers/DevController.js";
import HealthController from "../controllers/health.controller.js";
import { Flags } from "../lib/flags.js";

export const app = express();
@@ -29,6 +30,8 @@ if (Flags.Dev.TrustProxies) {
  app.enable("trust proxy");
}

app.use("/health", HealthController);

app.use("/api/oidc", cors());
app.all("/api/oidc/*", oidc.callback());
app.get("/.well-known/openid-configuration", oidc.callback());