ClsiManager.coffee 4.9 KB

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