ClsiManager.coffee 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. Path = require "path"
  2. async = require "async"
  3. Settings = require "settings-sharelatex"
  4. request = require('request')
  5. Project = require("../../models/Project").Project
  6. ProjectEntityHandler = require("../Project/ProjectEntityHandler")
  7. logger = require "logger-sharelatex"
  8. Url = require("url")
  9. ClsiCookieManager = require("./ClsiCookieManager")
  10. module.exports = ClsiManager =
  11. sendRequest: (project_id, user_id, options = {}, callback = (error, success) ->) ->
  12. ClsiManager._buildRequest project_id, options, (error, req) ->
  13. return callback(error) if error?
  14. logger.log project_id: project_id, "sending compile to CLSI"
  15. ClsiManager._postToClsi project_id, user_id, req, options.compileGroup, (error, response) ->
  16. if error?
  17. logger.err err:error, project_id:project_id, "error sending request to clsi"
  18. return callback(error)
  19. logger.log project_id: project_id, outputFilesLength: response?.outputFiles?.length, status: response?.status, "received compile response from CLSI"
  20. ClsiCookieManager._getServerId project_id, (err, clsiServerId)->
  21. if err?
  22. logger.err err:err, project_id:project_id, "error getting server id"
  23. return callback(err)
  24. outputFiles = ClsiManager._parseOutputFiles(project_id, response?.compile?.outputFiles)
  25. callback(null, response?.compile?.status, outputFiles, clsiServerId)
  26. deleteAuxFiles: (project_id, user_id, options, callback = (error) ->) ->
  27. compilerUrl = @_getCompilerUrl(options?.compileGroup, project_id, user_id)
  28. opts =
  29. url:compilerUrl
  30. method:"DELETE"
  31. ClsiManager._makeRequest project_id, opts, callback
  32. _makeRequest: (project_id, opts, callback)->
  33. ClsiCookieManager.getCookieJar project_id, (err, jar)->
  34. if err?
  35. logger.err err:err, "error getting cookie jar for clsi request"
  36. return callback(err)
  37. opts.jar = jar
  38. request opts, (err, response, body)->
  39. if err?
  40. logger.err err:err, project_id:project_id, url:opts?.url, "error making request to clsi"
  41. return callback(err)
  42. ClsiCookieManager.setServerId project_id, response, (err)->
  43. if err?
  44. logger.warn err:err, project_id:project_id, "error setting server id"
  45. return callback err, response, body
  46. _getCompilerUrl: (compileGroup, project_id, user_id, action) ->
  47. host = Settings.apis.clsi.url
  48. path = "/project/#{project_id}"
  49. path += "/user/#{user_id}" if user_id?
  50. path += "/#{action}" if action?
  51. return "#{host}#{path}"
  52. _postToClsi: (project_id, user_id, req, compileGroup, callback = (error, response) ->) ->
  53. compileUrl = @_getCompilerUrl(compileGroup, project_id, user_id, "compile")
  54. opts =
  55. url: compileUrl
  56. json: req
  57. method: "POST"
  58. ClsiManager._makeRequest project_id, opts, (error, response, body) ->
  59. return callback(error) if error?
  60. if 200 <= response.statusCode < 300
  61. callback null, body
  62. else if response.statusCode == 413
  63. callback null, compile:status:"project-too-large"
  64. else
  65. error = new Error("CLSI returned non-success code: #{response.statusCode}")
  66. logger.error err: error, project_id: project_id, "CLSI returned failure code"
  67. callback error, body
  68. _parseOutputFiles: (project_id, rawOutputFiles = []) ->
  69. outputFiles = []
  70. for file in rawOutputFiles
  71. outputFiles.push
  72. path: file.path # the clsi is now sending this to web
  73. url: Url.parse(file.url).path # the location of the file on the clsi, excluding the host part
  74. type: file.type
  75. build: file.build
  76. return outputFiles
  77. VALID_COMPILERS: ["pdflatex", "latex", "xelatex", "lualatex"]
  78. _buildRequest: (project_id, options={}, callback = (error, request) ->) ->
  79. Project.findById project_id, {compiler: 1, rootDoc_id: 1, imageName: 1}, (error, project) ->
  80. return callback(error) if error?
  81. return callback(new Errors.NotFoundError("project does not exist: #{project_id}")) if !project?
  82. if project.compiler not in ClsiManager.VALID_COMPILERS
  83. project.compiler = "pdflatex"
  84. ProjectEntityHandler.getAllDocs project_id, (error, docs = {}) ->
  85. return callback(error) if error?
  86. ProjectEntityHandler.getAllFiles project_id, (error, files = {}) ->
  87. return callback(error) if error?
  88. resources = []
  89. rootResourcePath = null
  90. rootResourcePathOverride = null
  91. for path, doc of docs
  92. path = path.replace(/^\//, "") # Remove leading /
  93. resources.push
  94. path: path
  95. content: doc.lines.join("\n")
  96. if project.rootDoc_id? and doc._id.toString() == project.rootDoc_id.toString()
  97. rootResourcePath = path
  98. if options.rootDoc_id? and doc._id.toString() == options.rootDoc_id.toString()
  99. rootResourcePathOverride = path
  100. rootResourcePath = rootResourcePathOverride if rootResourcePathOverride?
  101. if !rootResourcePath?
  102. logger.warn {project_id}, "no root document found, setting to main.tex"
  103. rootResourcePath = "main.tex"
  104. for path, file of files
  105. path = path.replace(/^\//, "") # Remove leading /
  106. resources.push
  107. path: path
  108. url: "#{Settings.apis.filestore.url}/project/#{project._id}/file/#{file._id}"
  109. modified: file.created?.getTime()
  110. callback null, {
  111. compile:
  112. options:
  113. compiler: project.compiler
  114. timeout: options.timeout
  115. imageName: project.imageName
  116. draft: !!options.draft
  117. rootResourcePath: rootResourcePath
  118. resources: resources
  119. }
  120. wordCount: (project_id, user_id, file, options, callback = (error, response) ->) ->
  121. ClsiManager._buildRequest project_id, options, (error, req) ->
  122. filename = file || req?.compile?.rootResourcePath
  123. wordcount_url = ClsiManager._getCompilerUrl(options?.compileGroup, project_id, user_id, "wordcount")
  124. opts =
  125. url: wordcount_url
  126. qs:
  127. file: filename
  128. image: req.compile.options.imageName
  129. method: "GET"
  130. ClsiManager._makeRequest project_id, opts, (error, response, body) ->
  131. return callback(error) if error?
  132. if 200 <= response.statusCode < 300
  133. callback null, body
  134. else
  135. error = new Error("CLSI returned non-success code: #{response.statusCode}")
  136. logger.error err: error, project_id: project_id, "CLSI returned failure code"
  137. callback error, body