ClsiManager.coffee 5.9 KB

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