CompileManager.coffee 4.0 KB

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