HttpController.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. const DocumentManager = require('./DocumentManager')
  2. const HistoryManager = require('./HistoryManager')
  3. const ProjectManager = require('./ProjectManager')
  4. const Errors = require('./Errors')
  5. const logger = require('logger-sharelatex')
  6. const Settings = require('settings-sharelatex')
  7. const Metrics = require('./Metrics')
  8. const ProjectFlusher = require('./ProjectFlusher')
  9. const DeleteQueueManager = require('./DeleteQueueManager')
  10. const async = require('async')
  11. module.exports = {
  12. getDoc,
  13. getProjectDocsAndFlushIfOld,
  14. clearProjectState,
  15. setDoc,
  16. flushDocIfLoaded,
  17. deleteDoc,
  18. flushProject,
  19. deleteProject,
  20. deleteMultipleProjects,
  21. acceptChanges,
  22. deleteComment,
  23. updateProject,
  24. resyncProjectHistory,
  25. flushAllProjects,
  26. flushQueuedProjects
  27. }
  28. function getDoc(req, res, next) {
  29. let fromVersion
  30. const docId = req.params.doc_id
  31. const projectId = req.params.project_id
  32. logger.log({ projectId, docId }, 'getting doc via http')
  33. const timer = new Metrics.Timer('http.getDoc')
  34. if (req.query.fromVersion != null) {
  35. fromVersion = parseInt(req.query.fromVersion, 10)
  36. } else {
  37. fromVersion = -1
  38. }
  39. DocumentManager.getDocAndRecentOpsWithLock(
  40. projectId,
  41. docId,
  42. fromVersion,
  43. (error, lines, version, ops, ranges, pathname) => {
  44. timer.done()
  45. if (error) {
  46. return next(error)
  47. }
  48. logger.log({ projectId, docId }, 'got doc via http')
  49. if (lines == null || version == null) {
  50. return next(new Errors.NotFoundError('document not found'))
  51. }
  52. res.json({
  53. id: docId,
  54. lines,
  55. version,
  56. ops,
  57. ranges,
  58. pathname
  59. })
  60. }
  61. )
  62. }
  63. function _getTotalSizeOfLines(lines) {
  64. let size = 0
  65. for (const line of lines) {
  66. size += line.length + 1
  67. }
  68. return size
  69. }
  70. function getProjectDocsAndFlushIfOld(req, res, next) {
  71. const projectId = req.params.project_id
  72. const projectStateHash = req.query.state
  73. // exclude is string of existing docs "id:version,id:version,..."
  74. const excludeItems =
  75. req.query.exclude != null ? req.query.exclude.split(',') : []
  76. logger.log({ projectId, exclude: excludeItems }, 'getting docs via http')
  77. const timer = new Metrics.Timer('http.getAllDocs')
  78. const excludeVersions = {}
  79. for (const item of excludeItems) {
  80. const [id, version] = item.split(':')
  81. excludeVersions[id] = version
  82. }
  83. logger.log(
  84. { projectId, projectStateHash, excludeVersions },
  85. 'excluding versions'
  86. )
  87. ProjectManager.getProjectDocsAndFlushIfOld(
  88. projectId,
  89. projectStateHash,
  90. excludeVersions,
  91. (error, result) => {
  92. timer.done()
  93. if (error instanceof Errors.ProjectStateChangedError) {
  94. res.sendStatus(409) // conflict
  95. } else if (error) {
  96. next(error)
  97. } else {
  98. logger.log(
  99. {
  100. projectId,
  101. result: result.map((doc) => `${doc._id}:${doc.v}`)
  102. },
  103. 'got docs via http'
  104. )
  105. res.send(result)
  106. }
  107. }
  108. )
  109. }
  110. function clearProjectState(req, res, next) {
  111. const projectId = req.params.project_id
  112. const timer = new Metrics.Timer('http.clearProjectState')
  113. logger.log({ projectId }, 'clearing project state via http')
  114. ProjectManager.clearProjectState(projectId, (error) => {
  115. timer.done()
  116. if (error) {
  117. next(error)
  118. } else {
  119. res.sendStatus(200)
  120. }
  121. })
  122. }
  123. function setDoc(req, res, next) {
  124. const docId = req.params.doc_id
  125. const projectId = req.params.project_id
  126. const { lines, source, user_id: userId, undoing } = req.body
  127. const lineSize = _getTotalSizeOfLines(lines)
  128. if (lineSize > Settings.max_doc_length) {
  129. logger.log(
  130. { projectId, docId, source, lineSize, userId },
  131. 'document too large, returning 406 response'
  132. )
  133. return res.sendStatus(406)
  134. }
  135. logger.log(
  136. { projectId, docId, lines, source, userId, undoing },
  137. 'setting doc via http'
  138. )
  139. const timer = new Metrics.Timer('http.setDoc')
  140. DocumentManager.setDocWithLock(
  141. projectId,
  142. docId,
  143. lines,
  144. source,
  145. userId,
  146. undoing,
  147. (error) => {
  148. timer.done()
  149. if (error) {
  150. return next(error)
  151. }
  152. logger.log({ projectId, docId }, 'set doc via http')
  153. res.sendStatus(204) // No Content
  154. }
  155. )
  156. }
  157. function flushDocIfLoaded(req, res, next) {
  158. const docId = req.params.doc_id
  159. const projectId = req.params.project_id
  160. logger.log({ projectId, docId }, 'flushing doc via http')
  161. const timer = new Metrics.Timer('http.flushDoc')
  162. DocumentManager.flushDocIfLoadedWithLock(projectId, docId, (error) => {
  163. timer.done()
  164. if (error) {
  165. return next(error)
  166. }
  167. logger.log({ projectId, docId }, 'flushed doc via http')
  168. res.sendStatus(204) // No Content
  169. })
  170. }
  171. function deleteDoc(req, res, next) {
  172. const docId = req.params.doc_id
  173. const projectId = req.params.project_id
  174. const ignoreFlushErrors = req.query.ignore_flush_errors === 'true'
  175. const timer = new Metrics.Timer('http.deleteDoc')
  176. logger.log({ projectId, docId }, 'deleting doc via http')
  177. DocumentManager.flushAndDeleteDocWithLock(
  178. projectId,
  179. docId,
  180. { ignoreFlushErrors },
  181. (error) => {
  182. timer.done()
  183. // There is no harm in flushing project history if the previous call
  184. // failed and sometimes it is required
  185. HistoryManager.flushProjectChangesAsync(projectId)
  186. if (error) {
  187. return next(error)
  188. }
  189. logger.log({ projectId, docId }, 'deleted doc via http')
  190. res.sendStatus(204) // No Content
  191. }
  192. )
  193. }
  194. function flushProject(req, res, next) {
  195. const projectId = req.params.project_id
  196. logger.log({ projectId }, 'flushing project via http')
  197. const timer = new Metrics.Timer('http.flushProject')
  198. ProjectManager.flushProjectWithLocks(projectId, (error) => {
  199. timer.done()
  200. if (error) {
  201. return next(error)
  202. }
  203. logger.log({ projectId }, 'flushed project via http')
  204. res.sendStatus(204) // No Content
  205. })
  206. }
  207. function deleteProject(req, res, next) {
  208. const projectId = req.params.project_id
  209. logger.log({ projectId }, 'deleting project via http')
  210. const options = {}
  211. if (req.query.background) {
  212. options.background = true
  213. } // allow non-urgent flushes to be queued
  214. if (req.query.shutdown) {
  215. options.skip_history_flush = true
  216. } // don't flush history when realtime shuts down
  217. if (req.query.background) {
  218. ProjectManager.queueFlushAndDeleteProject(projectId, (error) => {
  219. if (error) {
  220. return next(error)
  221. }
  222. logger.log({ projectId }, 'queue delete of project via http')
  223. res.sendStatus(204)
  224. }) // No Content
  225. } else {
  226. const timer = new Metrics.Timer('http.deleteProject')
  227. ProjectManager.flushAndDeleteProjectWithLocks(
  228. projectId,
  229. options,
  230. (error) => {
  231. timer.done()
  232. if (error) {
  233. return next(error)
  234. }
  235. logger.log({ projectId }, 'deleted project via http')
  236. res.sendStatus(204) // No Content
  237. }
  238. )
  239. }
  240. }
  241. function deleteMultipleProjects(req, res, next) {
  242. const projectIds = req.body.project_ids || []
  243. logger.log({ projectIds }, 'deleting multiple projects via http')
  244. async.eachSeries(
  245. projectIds,
  246. (projectId, cb) => {
  247. logger.log({ projectId }, 'queue delete of project via http')
  248. ProjectManager.queueFlushAndDeleteProject(projectId, cb)
  249. },
  250. (error) => {
  251. if (error) {
  252. return next(error)
  253. }
  254. res.sendStatus(204) // No Content
  255. }
  256. )
  257. }
  258. function acceptChanges(req, res, next) {
  259. const { project_id: projectId, doc_id: docId } = req.params
  260. let changeIds = req.body.change_ids
  261. if (changeIds == null) {
  262. changeIds = [req.params.change_id]
  263. }
  264. logger.log(
  265. { projectId, docId },
  266. `accepting ${changeIds.length} changes via http`
  267. )
  268. const timer = new Metrics.Timer('http.acceptChanges')
  269. DocumentManager.acceptChangesWithLock(
  270. projectId,
  271. docId,
  272. changeIds,
  273. (error) => {
  274. timer.done()
  275. if (error) {
  276. return next(error)
  277. }
  278. logger.log(
  279. { projectId, docId },
  280. `accepted ${changeIds.length} changes via http`
  281. )
  282. res.sendStatus(204) // No Content
  283. }
  284. )
  285. }
  286. function deleteComment(req, res, next) {
  287. const {
  288. project_id: projectId,
  289. doc_id: docId,
  290. comment_id: commentId
  291. } = req.params
  292. logger.log({ projectId, docId, commentId }, 'deleting comment via http')
  293. const timer = new Metrics.Timer('http.deleteComment')
  294. DocumentManager.deleteCommentWithLock(
  295. projectId,
  296. docId,
  297. commentId,
  298. (error) => {
  299. timer.done()
  300. if (error) {
  301. return next(error)
  302. }
  303. logger.log({ projectId, docId, commentId }, 'deleted comment via http')
  304. res.sendStatus(204) // No Content
  305. }
  306. )
  307. }
  308. function updateProject(req, res, next) {
  309. const timer = new Metrics.Timer('http.updateProject')
  310. const projectId = req.params.project_id
  311. const { projectHistoryId, userId, updates = [], version } = req.body
  312. logger.log({ projectId, updates, version }, 'updating project via http')
  313. ProjectManager.updateProjectWithLocks(
  314. projectId,
  315. projectHistoryId,
  316. userId,
  317. updates,
  318. version,
  319. (error) => {
  320. timer.done()
  321. if (error) {
  322. return next(error)
  323. }
  324. logger.log({ projectId }, 'updated project via http')
  325. res.sendStatus(204) // No Content
  326. }
  327. )
  328. }
  329. function resyncProjectHistory(req, res, next) {
  330. const projectId = req.params.project_id
  331. const { projectHistoryId, docs, files } = req.body
  332. logger.log(
  333. { projectId, docs, files },
  334. 'queuing project history resync via http'
  335. )
  336. HistoryManager.resyncProjectHistory(
  337. projectId,
  338. projectHistoryId,
  339. docs,
  340. files,
  341. (error) => {
  342. if (error) {
  343. return next(error)
  344. }
  345. logger.log({ projectId }, 'queued project history resync via http')
  346. res.sendStatus(204)
  347. }
  348. )
  349. }
  350. function flushAllProjects(req, res, next) {
  351. res.setTimeout(5 * 60 * 1000)
  352. const options = {
  353. limit: req.query.limit || 1000,
  354. concurrency: req.query.concurrency || 5,
  355. dryRun: req.query.dryRun || false
  356. }
  357. ProjectFlusher.flushAllProjects(options, (err, projectIds) => {
  358. if (err) {
  359. logger.err({ err }, 'error bulk flushing projects')
  360. res.sendStatus(500)
  361. } else {
  362. res.send(projectIds)
  363. }
  364. })
  365. }
  366. function flushQueuedProjects(req, res, next) {
  367. res.setTimeout(10 * 60 * 1000)
  368. const options = {
  369. limit: req.query.limit || 1000,
  370. timeout: 5 * 60 * 1000,
  371. min_delete_age: req.query.min_delete_age || 5 * 60 * 1000
  372. }
  373. DeleteQueueManager.flushAndDeleteOldProjects(options, (err, flushed) => {
  374. if (err) {
  375. logger.err({ err }, 'error flushing old projects')
  376. res.sendStatus(500)
  377. } else {
  378. logger.log({ flushed }, 'flush of queued projects completed')
  379. res.send({ flushed })
  380. }
  381. })
  382. }