CompileManager.coffee 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. Settings = require('settings-sharelatex')
  2. RedisWrapper = require("../../infrastructure/RedisWrapper")
  3. rclient = RedisWrapper.client("clsi_recently_compiled")
  4. Project = require("../../models/Project").Project
  5. ProjectRootDocManager = require "../Project/ProjectRootDocManager"
  6. UserGetter = require "../User/UserGetter"
  7. ClsiManager = require "./ClsiManager"
  8. Metrics = require('metrics-sharelatex')
  9. logger = require("logger-sharelatex")
  10. rateLimiter = require("../../infrastructure/RateLimiter")
  11. module.exports = CompileManager =
  12. compile: (project_id, user_id, options = {}, _callback = (error) ->) ->
  13. timer = new Metrics.Timer("editor.compile")
  14. callback = (args...) ->
  15. timer.done()
  16. _callback(args...)
  17. @_checkIfAutoCompileLimitHasBeenHit options.isAutoCompile, (err, canCompile)->
  18. if !canCompile
  19. return callback null, "autocompile-backoff", []
  20. logger.log project_id: project_id, user_id: user_id, "compiling project"
  21. CompileManager._checkIfRecentlyCompiled project_id, user_id, (error, recentlyCompiled) ->
  22. return callback(error) if error?
  23. if recentlyCompiled
  24. logger.warn {project_id, user_id}, "project was recently compiled so not continuing"
  25. return callback null, "too-recently-compiled", []
  26. CompileManager._ensureRootDocumentIsSet project_id, (error) ->
  27. return callback(error) if error?
  28. CompileManager.getProjectCompileLimits project_id, (error, limits) ->
  29. return callback(error) if error?
  30. for key, value of limits
  31. options[key] = value
  32. # only pass user_id down to clsi if this is a per-user compile
  33. compileAsUser = if Settings.disablePerUserCompiles then undefined else user_id
  34. ClsiManager.sendRequest project_id, compileAsUser, options, (error, status, outputFiles, clsiServerId, validationProblems) ->
  35. return callback(error) if error?
  36. logger.log files: outputFiles, "output files"
  37. callback(null, status, outputFiles, clsiServerId, limits, validationProblems)
  38. stopCompile: (project_id, user_id, callback = (error) ->) ->
  39. CompileManager.getProjectCompileLimits project_id, (error, limits) ->
  40. return callback(error) if error?
  41. ClsiManager.stopCompile project_id, user_id, limits, callback
  42. deleteAuxFiles: (project_id, user_id, callback = (error) ->) ->
  43. CompileManager.getProjectCompileLimits project_id, (error, limits) ->
  44. return callback(error) if error?
  45. ClsiManager.deleteAuxFiles project_id, user_id, limits, callback
  46. getProjectCompileLimits: (project_id, callback = (error, limits) ->) ->
  47. Project.findById project_id, {owner_ref: 1}, (error, project) ->
  48. return callback(error) if error?
  49. UserGetter.getUser project.owner_ref, {"features":1}, (err, owner)->
  50. return callback(error) if error?
  51. callback null, {
  52. timeout: owner.features?.compileTimeout || Settings.defaultFeatures.compileTimeout
  53. compileGroup: owner.features?.compileGroup || Settings.defaultFeatures.compileGroup
  54. }
  55. COMPILE_DELAY: 1 # seconds
  56. _checkIfRecentlyCompiled: (project_id, user_id, callback = (error, recentlyCompiled) ->) ->
  57. key = "compile:#{project_id}:#{user_id}"
  58. rclient.set key, true, "EX", @COMPILE_DELAY, "NX", (error, ok) ->
  59. return callback(error) if error?
  60. if ok == "OK"
  61. return callback null, false
  62. else
  63. return callback null, true
  64. _checkIfAutoCompileLimitHasBeenHit: (isAutoCompile, callback = (err, canCompile)->)->
  65. if !isAutoCompile
  66. return callback(null, true)
  67. opts =
  68. endpointName:"auto_compile"
  69. timeInterval:20
  70. subjectName:"everyone"
  71. throttle: 25
  72. rateLimiter.addCount opts, (err, canCompile)->
  73. if err?
  74. canCompile = false
  75. logger.log canCompile:canCompile, opts:opts, "checking if auto compile limit has been hit"
  76. callback err, canCompile
  77. _ensureRootDocumentIsSet: (project_id, callback = (error) ->) ->
  78. Project.findById project_id, 'rootDoc_id', (error, project)=>
  79. return callback(error) if error?
  80. if !project?
  81. return callback new Error("project not found")
  82. if project.rootDoc_id?
  83. callback()
  84. else
  85. ProjectRootDocManager.setRootDocAutomatically project_id, callback
  86. wordCount: (project_id, user_id, file, callback = (error) ->) ->
  87. CompileManager.getProjectCompileLimits project_id, (error, limits) ->
  88. return callback(error) if error?
  89. ClsiManager.wordCount project_id, user_id, file, limits, callback