HealthChecker.js 1.2 KB

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