HealthChecker.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import mongodb from './mongodb.js'
  2. import _ from 'lodash'
  3. import crypto from 'node:crypto'
  4. import settings from '@overleaf/settings'
  5. import logger from '@overleaf/logger'
  6. import { fetchNothing, fetchJson } from '@overleaf/fetch-utils'
  7. const { db, ObjectId } = mongodb
  8. const { port } = settings.internal.docstore
  9. async function check() {
  10. const docId = new ObjectId()
  11. const projectId = new ObjectId(settings.docstore.healthCheck.project_id)
  12. const url = `http://127.0.0.1:${port}/project/${projectId}/doc/${docId}`
  13. const lines = [
  14. 'smoke test - delete me',
  15. `${crypto.randomBytes(32).toString('hex')}`,
  16. ]
  17. logger.debug({ lines, url, docId, projectId }, 'running health check')
  18. let body
  19. try {
  20. await fetchNothing(url, {
  21. method: 'POST',
  22. json: { lines, version: 42, ranges: {} },
  23. signal: AbortSignal.timeout(3_000),
  24. })
  25. body = await fetchJson(url, { signal: AbortSignal.timeout(3_000) })
  26. } finally {
  27. await db.docs.deleteOne({ _id: docId, project_id: projectId })
  28. }
  29. if (!_.isEqual(body?.lines, lines)) {
  30. throw new Error(`health check lines not equal ${body.lines} != ${lines}`)
  31. }
  32. }
  33. export default {
  34. check,
  35. }