TpdsProjectFlusher.mjs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import { callbackify } from 'node:util'
  2. import logger from '@overleaf/logger'
  3. import DocumentUpdaterHandler from '../DocumentUpdater/DocumentUpdaterHandler.mjs'
  4. import ProjectGetter from '../Project/ProjectGetter.mjs'
  5. import ProjectEntityHandler from '../Project/ProjectEntityHandler.mjs'
  6. import { Project } from '../../models/Project.mjs'
  7. import TpdsUpdateSender from './TpdsUpdateSender.mjs'
  8. import OError from '@overleaf/o-error'
  9. export default {
  10. flushProjectToTpds: callbackify(flushProjectToTpds),
  11. deferProjectFlushToTpds: callbackify(deferProjectFlushToTpds),
  12. flushProjectToTpdsIfNeeded: callbackify(flushProjectToTpdsIfNeeded),
  13. promises: {
  14. flushProjectToTpds,
  15. deferProjectFlushToTpds,
  16. flushProjectToTpdsIfNeeded,
  17. },
  18. }
  19. /**
  20. * Flush a complete project to the TPDS.
  21. */
  22. async function flushProjectToTpds(projectId) {
  23. const project = await ProjectGetter.promises.getProject(projectId, {
  24. name: true,
  25. deferredTpdsFlushCounter: true,
  26. 'overleaf.history.id': 1,
  27. })
  28. await _flushProjectToTpds(project)
  29. }
  30. /**
  31. * Flush a project to TPDS if a flush is pending. This is called when
  32. * projects are loaded in the editor and triggers a sync to dropbox for
  33. * projects that were imported from Overleaf v1.
  34. */
  35. async function flushProjectToTpdsIfNeeded(projectId) {
  36. const project = await ProjectGetter.promises.getProject(projectId, {
  37. name: true,
  38. deferredTpdsFlushCounter: true,
  39. 'overleaf.history.id': 1,
  40. })
  41. if (project.deferredTpdsFlushCounter > 0) {
  42. await _flushProjectToTpds(project)
  43. }
  44. }
  45. async function _flushProjectToTpds(project) {
  46. const historyId = project?.overleaf?.history?.id
  47. if (!historyId) {
  48. const projectId = project._id
  49. throw new OError('project does not have a history id', { projectId })
  50. }
  51. logger.debug({ projectId: project._id }, 'flushing project to TPDS')
  52. logger.debug({ projectId: project._id }, 'finished flushing project to TPDS')
  53. await DocumentUpdaterHandler.promises.flushProjectToMongo(project._id)
  54. const [docs, files] = await Promise.all([
  55. ProjectEntityHandler.promises.getAllDocs(project._id),
  56. ProjectEntityHandler.promises.getAllFiles(project._id),
  57. ])
  58. for (const [docPath, doc] of Object.entries(docs)) {
  59. await TpdsUpdateSender.promises.addDoc({
  60. projectId: project._id,
  61. docId: doc._id,
  62. path: docPath,
  63. projectName: project.name,
  64. rev: doc.rev || 0,
  65. folderId: doc.folder._id,
  66. })
  67. }
  68. for (const [filePath, file] of Object.entries(files)) {
  69. await TpdsUpdateSender.promises.addFile({
  70. projectId: project._id,
  71. historyId,
  72. fileId: file._id,
  73. hash: file.hash,
  74. path: filePath,
  75. projectName: project.name,
  76. rev: file.rev,
  77. folderId: file.folder._id,
  78. })
  79. }
  80. await _resetDeferredTpdsFlushCounter(project)
  81. }
  82. /**
  83. * Reset the TPDS pending flush counter.
  84. *
  85. * To avoid concurrency problems, the flush counter is not reset if it has been
  86. * incremented since we fetched it from the database.
  87. */
  88. async function _resetDeferredTpdsFlushCounter(project) {
  89. if (project.deferredTpdsFlushCounter > 0) {
  90. await Project.updateOne(
  91. {
  92. _id: project._id,
  93. deferredTpdsFlushCounter: { $lte: project.deferredTpdsFlushCounter },
  94. },
  95. { $set: { deferredTpdsFlushCounter: 0 } }
  96. ).exec()
  97. }
  98. }
  99. /**
  100. * Mark a project as pending a flush to TPDS.
  101. * This was called as part of the import process for Overleaf v1 projects.
  102. * We no longer use this method, but imported v1 projects have the
  103. * deferredTpdsFlushCounter set and will trigger a flush when loaded in
  104. * the editor.
  105. */
  106. async function deferProjectFlushToTpds(projectId) {
  107. await Project.updateOne(
  108. { _id: projectId },
  109. { $inc: { deferredTpdsFlushCounter: 1 } }
  110. ).exec()
  111. }