CompileManager.coffee 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. return callback new Error("project was recently compiled so not continuing")
  26. CompileManager._ensureRootDocumentIsSet project_id, (error) ->
  27. return callback(error) if error?
  28. DocumentUpdaterHandler.flushProjectToMongo project_id, (error) ->
  29. return callback(error) if error?
  30. CompileManager.getProjectCompileLimits project_id, (error, limits) ->
  31. return callback(error) if error?
  32. for key, value of limits
  33. options[key] = value
  34. ClsiManager.sendRequest project_id, options, (error, status, outputFiles, output) ->
  35. return callback(error) if error?
  36. logger.log files: outputFiles, "output files"
  37. callback(null, status, outputFiles, output, limits)
  38. deleteAuxFiles: (project_id, callback = (error) ->) ->
  39. CompileManager.getProjectCompileLimits project_id, (error, limits) ->
  40. return callback(error) if error?
  41. ClsiManager.deleteAuxFiles project_id, limits, callback
  42. getProjectCompileLimits: (project_id, callback = (error, limits) ->) ->
  43. Project.findById project_id, {owner_ref: 1}, (error, project) ->
  44. return callback(error) if error?
  45. UserGetter.getUser project.owner_ref, {"features":1}, (err, owner)->
  46. return callback(error) if error?
  47. callback null, {
  48. timeout: owner.features?.compileTimeout || Settings.defaultFeatures.compileTimeout
  49. compileGroup: owner.features?.compileGroup || Settings.defaultFeatures.compileGroup
  50. }
  51. COMPILE_DELAY: 1 # seconds
  52. _checkIfRecentlyCompiled: (project_id, user_id, callback = (error, recentlyCompiled) ->) ->
  53. key = "compile:#{project_id}:#{user_id}"
  54. rclient.set key, true, "EX", @COMPILE_DELAY, "NX", (error, ok) ->
  55. return callback(error) if error?
  56. if ok == "OK"
  57. return callback null, false
  58. else
  59. return callback null, true
  60. _checkIfAutoCompileLimitHasBeenHit: (isAutoCompile, callback = (err, canCompile)->)->
  61. if !isAutoCompile
  62. return callback(null, true)
  63. opts =
  64. endpointName:"auto_compile"
  65. timeInterval:20
  66. subjectName:"everyone"
  67. throttle: 25
  68. rateLimiter.addCount opts, (err, canCompile)->
  69. if err?
  70. canCompile = false
  71. logger.log canCompile:canCompile, opts:opts, "checking if auto compile limit has been hit"
  72. callback err, canCompile
  73. _ensureRootDocumentIsSet: (project_id, callback = (error) ->) ->
  74. Project.findById project_id, 'rootDoc_id', (error, project)=>
  75. return callback(error) if error?
  76. if !project?
  77. return callback new Error("project not found")
  78. if project.rootDoc_id?
  79. callback()
  80. else
  81. ProjectRootDocManager.setRootDocAutomatically project_id, callback