HealthChecker.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import { ObjectId } from './mongodb.js'
  2. import settings from '@overleaf/settings'
  3. import logger from '@overleaf/logger'
  4. import OError from '@overleaf/o-error'
  5. import * as LockManager from './LockManager.js'
  6. import { fetchNothing } from '@overleaf/fetch-utils'
  7. import { callbackify } from '@overleaf/promise-utils'
  8. const { port } = settings.internal.history
  9. const checkCb = callbackify(check)
  10. export { checkCb as check }
  11. async function check() {
  12. const projectId = new ObjectId(settings.history.healthCheck.project_id)
  13. const url = `http://127.0.0.1:${port}/project/${projectId}`
  14. logger.debug({ projectId }, 'running health check')
  15. try {
  16. await fetchNothing(`http://127.0.0.1:${port}/check_lock`, {
  17. signal: AbortSignal.timeout(3_000),
  18. })
  19. } catch (err) {
  20. throw OError.tag(err, 'error checking lock for health check', {
  21. project_id: projectId,
  22. })
  23. }
  24. try {
  25. await fetchNothing(`${url}/flush`, {
  26. method: 'POST',
  27. signal: AbortSignal.timeout(10_000),
  28. })
  29. } catch (err) {
  30. throw OError.tag(err, 'error flushing for health check', {
  31. project_id: projectId,
  32. })
  33. }
  34. try {
  35. await fetchNothing(`${url}/updates`, {
  36. signal: AbortSignal.timeout(10_000),
  37. })
  38. } catch (err) {
  39. throw OError.tag(err, 'error getting updates for health check', {
  40. project_id: projectId,
  41. })
  42. }
  43. }
  44. export function checkLock(callback) {
  45. return LockManager.healthCheck(callback)
  46. }