HealthChecker.js 1.1 KB

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