ClsiManager.coffee 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. Path = require "path"
  2. async = require "async"
  3. Settings = require "settings-sharelatex"
  4. request = require('request')
  5. Project = require("../../models/Project").Project
  6. ProjectGetter = require("../Project/ProjectGetter")
  7. ProjectEntityHandler = require("../Project/ProjectEntityHandler")
  8. logger = require "logger-sharelatex"
  9. Url = require("url")
  10. ClsiCookieManager = require("./ClsiCookieManager")
  11. ClsiStateManager = require("./ClsiStateManager")
  12. _ = require("underscore")
  13. async = require("async")
  14. ClsiFormatChecker = require("./ClsiFormatChecker")
  15. DocumentUpdaterHandler = require "../DocumentUpdater/DocumentUpdaterHandler"
  16. module.exports = ClsiManager =
  17. sendRequest: (project_id, user_id, options = {}, callback) ->
  18. ClsiManager.sendRequestOnce project_id, user_id, _.clone(options), (error, status, result...) ->
  19. return callback(error) if error?
  20. if status is 'conflict'
  21. options.syncType = "full" # force full compile
  22. ClsiManager.sendRequestOnce project_id, user_id, options, callback # try again
  23. else
  24. callback(error, status, result...)
  25. sendRequestOnce: (project_id, user_id, options = {}, callback = (error, status, outputFiles, clsiServerId, validationProblems) ->) ->
  26. ClsiManager._buildRequest project_id, options, (error, req) ->
  27. return callback(error) if error?
  28. logger.log project_id: project_id, "sending compile to CLSI"
  29. ClsiFormatChecker.checkRecoursesForProblems req.compile?.resources, (err, validationProblems)->
  30. if err?
  31. logger.err err, project_id, "could not check resources for potential problems before sending to clsi"
  32. return callback(err)
  33. if validationProblems?
  34. logger.log project_id:project_id, validationProblems:validationProblems, "problems with users latex before compile was attempted"
  35. return callback(null, "validation-problems", null, null, validationProblems)
  36. ClsiManager._postToClsi project_id, user_id, req, options.compileGroup, (error, response) ->
  37. if error?
  38. logger.err err:error, project_id:project_id, "error sending request to clsi"
  39. return callback(error)
  40. logger.log project_id: project_id, outputFilesLength: response?.outputFiles?.length, status: response?.status, compile_status: response?.compile?.status, "received compile response from CLSI"
  41. ClsiCookieManager._getServerId project_id, (err, clsiServerId)->
  42. if err?
  43. logger.err err:err, project_id:project_id, "error getting server id"
  44. return callback(err)
  45. outputFiles = ClsiManager._parseOutputFiles(project_id, response?.compile?.outputFiles)
  46. callback(null, response?.compile?.status, outputFiles, clsiServerId)
  47. stopCompile: (project_id, user_id, options, callback = (error) ->) ->
  48. compilerUrl = @_getCompilerUrl(options?.compileGroup, project_id, user_id, "compile/stop")
  49. opts =
  50. url:compilerUrl
  51. method:"POST"
  52. ClsiManager._makeRequest project_id, opts, callback
  53. deleteAuxFiles: (project_id, user_id, options, callback = (error) ->) ->
  54. compilerUrl = @_getCompilerUrl(options?.compileGroup, project_id, user_id)
  55. opts =
  56. url:compilerUrl
  57. method:"DELETE"
  58. ClsiManager._makeRequest project_id, opts, callback
  59. _makeRequest: (project_id, opts, callback)->
  60. ClsiCookieManager.getCookieJar project_id, (err, jar)->
  61. if err?
  62. logger.err err:err, "error getting cookie jar for clsi request"
  63. return callback(err)
  64. opts.jar = jar
  65. request opts, (err, response, body)->
  66. if err?
  67. logger.err err:err, project_id:project_id, url:opts?.url, "error making request to clsi"
  68. return callback(err)
  69. ClsiCookieManager.setServerId project_id, response, (err)->
  70. if err?
  71. logger.warn err:err, project_id:project_id, "error setting server id"
  72. return callback err, response, body
  73. _getCompilerUrl: (compileGroup, project_id, user_id, action) ->
  74. host = Settings.apis.clsi.url
  75. path = "/project/#{project_id}"
  76. path += "/user/#{user_id}" if user_id?
  77. path += "/#{action}" if action?
  78. return "#{host}#{path}"
  79. _postToClsi: (project_id, user_id, req, compileGroup, callback = (error, response) ->) ->
  80. compileUrl = @_getCompilerUrl(compileGroup, project_id, user_id, "compile")
  81. opts =
  82. url: compileUrl
  83. json: req
  84. method: "POST"
  85. ClsiManager._makeRequest project_id, opts, (error, response, body) ->
  86. return callback(error) if error?
  87. if 200 <= response.statusCode < 300
  88. callback null, body
  89. else if response.statusCode == 413
  90. callback null, compile:status:"project-too-large"
  91. else if response.statusCode == 409
  92. callback null, compile:status:"conflict"
  93. else
  94. error = new Error("CLSI returned non-success code: #{response.statusCode}")
  95. logger.error err: error, project_id: project_id, "CLSI returned failure code"
  96. callback error, body
  97. _parseOutputFiles: (project_id, rawOutputFiles = []) ->
  98. outputFiles = []
  99. for file in rawOutputFiles
  100. outputFiles.push
  101. path: file.path # the clsi is now sending this to web
  102. url: Url.parse(file.url).path # the location of the file on the clsi, excluding the host part
  103. type: file.type
  104. build: file.build
  105. return outputFiles
  106. VALID_COMPILERS: ["pdflatex", "latex", "xelatex", "lualatex"]
  107. _buildRequest: (project_id, options={}, callback = (error, request) ->) ->
  108. ProjectGetter.getProject project_id, {compiler: 1, rootDoc_id: 1, imageName: 1, rootFolder:1}, (error, project) ->
  109. return callback(error) if error?
  110. return callback(new Errors.NotFoundError("project does not exist: #{project_id}")) if !project?
  111. if project.compiler not in ClsiManager.VALID_COMPILERS
  112. project.compiler = "pdflatex"
  113. ClsiStateManager.checkProjectStateMatch project_id, project, (error, projectStateUnchanged, projectStateHash) ->
  114. return callback(error) if error?
  115. logger.log project_id: project_id, projectStateUnchanged: projectStateUnchanged, "checked project state"
  116. # see if we can send an incremental update to the CLSI
  117. if projectStateUnchanged and options.syncType isnt "full"
  118. ClsiManager._getContentFromDocUpdater project_id, (error, docUpdaterDocs) ->
  119. return callback(error) if error?
  120. ProjectEntityHandler.getAllDocPathsFromProject project, (error, docPath) ->
  121. return callback(error) if error?
  122. docs = {}
  123. for doc in docUpdaterDocs or []
  124. path = docPath[doc._id]
  125. docs[path] = doc
  126. options.syncType = "incremental"
  127. options.syncState = projectStateHash
  128. # send new docs but not files as those are already on the clsi
  129. ClsiManager._finaliseRequest project_id, options, project, docs, [], callback
  130. else
  131. ClsiManager._getContentFromMongo project_id, (error, docs, files) ->
  132. return callback(error) if error?
  133. # FIXME want to store state after project has been sent to
  134. # clsi, but need to do it here.
  135. ClsiStateManager.setProjectState project_id, project, (error, projectStateHash) ->
  136. if error?
  137. logger.err err:error, project_id:project_id, "error storing state in redis"
  138. #return callback(error)
  139. options.syncType = "full"
  140. options.syncState = projectStateHash
  141. ClsiManager._finaliseRequest project_id, options, project, docs, files, callback
  142. _getContentFromDocUpdater: (project_id, callback = (error, docs) ->) ->
  143. DocumentUpdaterHandler.getProjectDocs project_id, (error, docs) ->
  144. return callback(error) if error?
  145. callback(null, docs)
  146. _getContentFromMongo: (project_id, callback = (error, docs, files) ->) ->
  147. DocumentUpdaterHandler.flushProjectToMongo project_id, (error) ->
  148. return callback(error) if error?
  149. ProjectEntityHandler.getAllDocs project_id, (error, docs = {}) ->
  150. return callback(error) if error?
  151. ProjectEntityHandler.getAllFiles project_id, (error, files = {}) ->
  152. return callback(error) if error?
  153. callback(null, docs, files)
  154. _finaliseRequest: (project_id, options, project, docs, files, callback = (error, params) -> ) ->
  155. resources = []
  156. rootResourcePath = null
  157. rootResourcePathOverride = null
  158. for path, doc of docs
  159. path = path.replace(/^\//, "") # Remove leading /
  160. resources.push
  161. path: path
  162. content: doc.lines.join("\n")
  163. rev: doc.rev
  164. if project.rootDoc_id? and doc._id.toString() == project.rootDoc_id.toString()
  165. rootResourcePath = path
  166. if options.rootDoc_id? and doc._id.toString() == options.rootDoc_id.toString()
  167. rootResourcePathOverride = path
  168. rootResourcePath = rootResourcePathOverride if rootResourcePathOverride?
  169. if !rootResourcePath?
  170. logger.warn {project_id}, "no root document found, setting to main.tex"
  171. rootResourcePath = "main.tex"
  172. for path, file of files
  173. path = path.replace(/^\//, "") # Remove leading /
  174. resources.push
  175. path: path
  176. url: "#{Settings.apis.filestore.url}/project/#{project._id}/file/#{file._id}"
  177. modified: file.created?.getTime()
  178. callback null, {
  179. compile:
  180. options:
  181. compiler: project.compiler
  182. timeout: options.timeout
  183. imageName: project.imageName
  184. draft: !!options.draft
  185. check: options.check
  186. syncType: options.syncType
  187. syncState: options.syncState
  188. rootResourcePath: rootResourcePath
  189. resources: resources
  190. }
  191. wordCount: (project_id, user_id, file, options, callback = (error, response) ->) ->
  192. ClsiManager._buildRequest project_id, options, (error, req) ->
  193. filename = file || req?.compile?.rootResourcePath
  194. wordcount_url = ClsiManager._getCompilerUrl(options?.compileGroup, project_id, user_id, "wordcount")
  195. opts =
  196. url: wordcount_url
  197. qs:
  198. file: filename
  199. image: req.compile.options.imageName
  200. method: "GET"
  201. ClsiManager._makeRequest project_id, opts, (error, response, body) ->
  202. return callback(error) if error?
  203. if 200 <= response.statusCode < 300
  204. callback null, body
  205. else
  206. error = new Error("CLSI returned non-success code: #{response.statusCode}")
  207. logger.error err: error, project_id: project_id, "CLSI returned failure code"
  208. callback error, body