<commit_before>import { Router } from 'express';
import { sql } from 'slonik';

import { withSlonik } from '~api/modules/db';
import logger from '~api/modules/logger';

const checkReadiness = withSlonik(async (db) => {
  const healthCheck = await db.query<{ version: string }>(sql`select version()`);

  if (!healthCheck.rows.length) {
    throw new Error('output of select version() is empty');
  }
});

export function healthRoutes(): Router {
  const router = Router();

  router.get('/liveness', (_, res) => {
    res.json({ ok: true });
  });

  router.get('/readiness', async (_, res) => {
    try {
      await checkReadiness();

      res.json({
        ok: true,
      });
    } catch (err) {
      logger.error('readiness check failed: %s', {
        err: err.message,
      });

      res.status(500);
      res.json({ ok: false });
    }
  });

  return router;
}
<commit_msg>chore: only check database health once per pod<commit_after>import { Router } from 'express';
import { sql } from 'slonik';

import { withSlonik } from '~api/modules/db';
import logger from '~api/modules/logger';

let healthCheckPassed = false;

const checkReadiness = withSlonik(async (db) => {
  const healthCheck = await db.query<{ version: string }>(sql`select version()`);

  if (!healthCheck.rows.length) {
    throw new Error('output of select version() is empty');
  }
});

export function healthRoutes(): Router {
  const router = Router();

  router.get('/liveness', (_, res) => {
    res.json({ ok: true });
  });

  router.get('/readiness', async (_, res) => {
    try {
      if (!healthCheckPassed) {
        await checkReadiness();
        healthCheckPassed = true;
      }

      res.json({
        ok: true,
      });
    } catch (err) {
      logger.error('readiness check failed: %s', {
        err: err.message,
      });

      res.status(500);
      res.json({ ok: false });
    }
  });

  return router;
}

{'fraction_non_alphanumeric': 0.16588419405320814, 'fraction_numerical': 0.003129890453834116, 'mean_word_length': 2.8514056224899598, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4355431', 'n_tokens_mistral': 628, 'n_tokens_neox': 613, 'n_words': 160}