ClsiManager.coffee 5.7 KB

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