ClsiManager.coffee 6.0 KB

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