CompileManager.coffee 4.2 KB

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