ProjectManager.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. const RedisManager = require('./RedisManager')
  2. const ProjectHistoryRedisManager = require('./ProjectHistoryRedisManager')
  3. const DocumentManager = require('./DocumentManager')
  4. const HistoryManager = require('./HistoryManager')
  5. const async = require('async')
  6. const logger = require('@overleaf/logger')
  7. const Metrics = require('./Metrics')
  8. const Errors = require('./Errors')
  9. const { promisifyAll } = require('@overleaf/promise-utils')
  10. module.exports = {
  11. flushProjectWithLocks,
  12. flushAndDeleteProjectWithLocks,
  13. queueFlushAndDeleteProject,
  14. getProjectDocsTimestamps,
  15. getProjectDocsAndFlushIfOld,
  16. clearProjectState,
  17. updateProjectWithLocks,
  18. }
  19. module.exports.promises = promisifyAll(module.exports)
  20. function flushProjectWithLocks(projectId, _callback) {
  21. const timer = new Metrics.Timer('projectManager.flushProjectWithLocks')
  22. const callback = function (...args) {
  23. timer.done()
  24. _callback(...args)
  25. }
  26. RedisManager.getDocIdsInProject(projectId, (error, docIds) => {
  27. if (error) {
  28. return callback(error)
  29. }
  30. const errors = []
  31. const jobs = docIds.map(docId => callback => {
  32. DocumentManager.flushDocIfLoadedWithLock(projectId, docId, error => {
  33. if (error instanceof Errors.NotFoundError) {
  34. logger.warn(
  35. { err: error, projectId, docId },
  36. 'found deleted doc when flushing'
  37. )
  38. callback()
  39. } else if (error) {
  40. logger.error({ err: error, projectId, docId }, 'error flushing doc')
  41. errors.push(error)
  42. callback()
  43. } else {
  44. callback()
  45. }
  46. })
  47. })
  48. logger.debug({ projectId, docIds }, 'flushing docs')
  49. async.series(jobs, () => {
  50. if (errors.length > 0) {
  51. callback(new Error('Errors flushing docs. See log for details'))
  52. } else {
  53. callback(null)
  54. }
  55. })
  56. })
  57. }
  58. function flushAndDeleteProjectWithLocks(projectId, options, _callback) {
  59. const timer = new Metrics.Timer(
  60. 'projectManager.flushAndDeleteProjectWithLocks'
  61. )
  62. const callback = function (...args) {
  63. timer.done()
  64. _callback(...args)
  65. }
  66. RedisManager.getDocIdsInProject(projectId, (error, docIds) => {
  67. if (error) {
  68. return callback(error)
  69. }
  70. const errors = []
  71. const jobs = docIds.map(docId => callback => {
  72. DocumentManager.flushAndDeleteDocWithLock(projectId, docId, {}, error => {
  73. if (error) {
  74. logger.error({ err: error, projectId, docId }, 'error deleting doc')
  75. errors.push(error)
  76. }
  77. callback()
  78. })
  79. })
  80. logger.debug({ projectId, docIds }, 'deleting docs')
  81. async.series(jobs, () =>
  82. // When deleting the project here we want to ensure that project
  83. // history is completely flushed because the project may be
  84. // deleted in web after this call completes, and so further
  85. // attempts to flush would fail after that.
  86. HistoryManager.flushProjectChanges(projectId, options, error => {
  87. if (errors.length > 0) {
  88. callback(new Error('Errors deleting docs. See log for details'))
  89. } else if (error) {
  90. callback(error)
  91. } else {
  92. callback(null)
  93. }
  94. })
  95. )
  96. })
  97. }
  98. function queueFlushAndDeleteProject(projectId, callback) {
  99. RedisManager.queueFlushAndDeleteProject(projectId, error => {
  100. if (error) {
  101. logger.error(
  102. { projectId, error },
  103. 'error adding project to flush and delete queue'
  104. )
  105. return callback(error)
  106. }
  107. Metrics.inc('queued-delete')
  108. callback()
  109. })
  110. }
  111. function getProjectDocsTimestamps(projectId, callback) {
  112. RedisManager.getDocIdsInProject(projectId, (error, docIds) => {
  113. if (error) {
  114. return callback(error)
  115. }
  116. if (docIds.length === 0) {
  117. return callback(null, [])
  118. }
  119. RedisManager.getDocTimestamps(docIds, (error, timestamps) => {
  120. if (error) {
  121. return callback(error)
  122. }
  123. callback(null, timestamps)
  124. })
  125. })
  126. }
  127. function getProjectDocsAndFlushIfOld(
  128. projectId,
  129. projectStateHash,
  130. excludeVersions,
  131. _callback
  132. ) {
  133. const timer = new Metrics.Timer('projectManager.getProjectDocsAndFlushIfOld')
  134. const callback = function (...args) {
  135. timer.done()
  136. _callback(...args)
  137. }
  138. RedisManager.checkOrSetProjectState(
  139. projectId,
  140. projectStateHash,
  141. (error, projectStateChanged) => {
  142. if (error) {
  143. logger.error(
  144. { err: error, projectId },
  145. 'error getting/setting project state in getProjectDocsAndFlushIfOld'
  146. )
  147. return callback(error)
  148. }
  149. // we can't return docs if project structure has changed
  150. if (projectStateChanged) {
  151. return callback(
  152. Errors.ProjectStateChangedError('project state changed')
  153. )
  154. }
  155. // project structure hasn't changed, return doc content from redis
  156. RedisManager.getDocIdsInProject(projectId, (error, docIds) => {
  157. if (error) {
  158. logger.error(
  159. { err: error, projectId },
  160. 'error getting doc ids in getProjectDocs'
  161. )
  162. return callback(error)
  163. }
  164. // get the doc lines from redis
  165. const jobs = docIds.map(docId => cb => {
  166. DocumentManager.getDocAndFlushIfOldWithLock(
  167. projectId,
  168. docId,
  169. (err, lines, version) => {
  170. if (err) {
  171. logger.error(
  172. { err, projectId, docId },
  173. 'error getting project doc lines in getProjectDocsAndFlushIfOld'
  174. )
  175. return cb(err)
  176. }
  177. const doc = { _id: docId, lines, v: version } // create a doc object to return
  178. cb(null, doc)
  179. }
  180. )
  181. })
  182. async.series(jobs, (error, docs) => {
  183. if (error) {
  184. return callback(error)
  185. }
  186. callback(null, docs)
  187. })
  188. })
  189. }
  190. )
  191. }
  192. function clearProjectState(projectId, callback) {
  193. RedisManager.clearProjectState(projectId, callback)
  194. }
  195. function updateProjectWithLocks(
  196. projectId,
  197. projectHistoryId,
  198. userId,
  199. updates,
  200. projectVersion,
  201. source,
  202. _callback
  203. ) {
  204. const timer = new Metrics.Timer('projectManager.updateProject')
  205. const callback = function (...args) {
  206. timer.done()
  207. _callback(...args)
  208. }
  209. let projectSubversion = 0 // project versions can have multiple operations
  210. let projectOpsLength = 0
  211. function handleUpdate(update, cb) {
  212. update.version = `${projectVersion}.${projectSubversion++}`
  213. switch (update.type) {
  214. case 'add-doc':
  215. ProjectHistoryRedisManager.queueAddEntity(
  216. projectId,
  217. projectHistoryId,
  218. 'doc',
  219. update.id,
  220. userId,
  221. update,
  222. source,
  223. (error, count) => {
  224. projectOpsLength = count
  225. cb(error)
  226. }
  227. )
  228. break
  229. case 'rename-doc':
  230. if (!update.newPathname) {
  231. // an empty newPathname signifies a delete, so there is no need to
  232. // update the pathname in redis
  233. ProjectHistoryRedisManager.queueRenameEntity(
  234. projectId,
  235. projectHistoryId,
  236. 'doc',
  237. update.id,
  238. userId,
  239. update,
  240. source,
  241. (error, count) => {
  242. projectOpsLength = count
  243. cb(error)
  244. }
  245. )
  246. } else {
  247. // rename the doc in redis before queuing the update
  248. DocumentManager.renameDocWithLock(
  249. projectId,
  250. update.id,
  251. userId,
  252. update,
  253. projectHistoryId,
  254. error => {
  255. if (error) {
  256. return cb(error)
  257. }
  258. ProjectHistoryRedisManager.queueRenameEntity(
  259. projectId,
  260. projectHistoryId,
  261. 'doc',
  262. update.id,
  263. userId,
  264. update,
  265. source,
  266. (error, count) => {
  267. projectOpsLength = count
  268. cb(error)
  269. }
  270. )
  271. }
  272. )
  273. }
  274. break
  275. case 'add-file':
  276. ProjectHistoryRedisManager.queueAddEntity(
  277. projectId,
  278. projectHistoryId,
  279. 'file',
  280. update.id,
  281. userId,
  282. update,
  283. source,
  284. (error, count) => {
  285. projectOpsLength = count
  286. cb(error)
  287. }
  288. )
  289. break
  290. case 'rename-file':
  291. ProjectHistoryRedisManager.queueRenameEntity(
  292. projectId,
  293. projectHistoryId,
  294. 'file',
  295. update.id,
  296. userId,
  297. update,
  298. source,
  299. (error, count) => {
  300. projectOpsLength = count
  301. cb(error)
  302. }
  303. )
  304. break
  305. default:
  306. cb(new Error(`Unknown update type: ${update.type}`))
  307. }
  308. }
  309. async.eachSeries(updates, handleUpdate, error => {
  310. if (error) {
  311. return callback(error)
  312. }
  313. if (
  314. HistoryManager.shouldFlushHistoryOps(
  315. projectOpsLength,
  316. updates.length,
  317. HistoryManager.FLUSH_PROJECT_EVERY_N_OPS
  318. )
  319. ) {
  320. HistoryManager.flushProjectChangesAsync(projectId)
  321. }
  322. callback()
  323. })
  324. }