CompileManager.coffee 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. Settings = require('settings-sharelatex')
  2. redis = require('redis')
  3. rclient = redis.createClient(Settings.redis.web.port, Settings.redis.web.host)
  4. rclient.auth(Settings.redis.web.password)
  5. DocumentUpdaterHandler = require "../DocumentUpdater/DocumentUpdaterHandler"
  6. Project = require("../../models/Project").Project
  7. ProjectRootDocManager = require "../Project/ProjectRootDocManager"
  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, opt = {}, _callback = (error) ->) ->
  14. timer = new Metrics.Timer("editor.compile")
  15. callback = (args...) ->
  16. timer.done()
  17. _callback(args...)
  18. @_checkIfAutoCompileLimitHasBeenHit opt.isAutoCompile, (err, canCompile)->
  19. if !canCompile
  20. err = {rateLimitHit:true}
  21. return callback(err)
  22. logger.log project_id: project_id, user_id: user_id, "compiling project"
  23. CompileManager._checkIfRecentlyCompiled project_id, user_id, (error, recentlyCompiled) ->
  24. return callback(error) if error?
  25. if recentlyCompiled
  26. return callback new Error("project was recently compiled so not continuing")
  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. ClsiManager.sendRequest project_id, (error, success, outputFiles) ->
  32. return callback(error) if error?
  33. logger.log files: outputFiles, "output files"
  34. callback(null, success, outputFiles)
  35. getLogLines: (project_id, callback)->
  36. Metrics.inc "editor.raw-logs"
  37. ClsiManager.getLogLines project_id, (error, logLines)->
  38. return callback(error) if error?
  39. callback null, logLines
  40. COMPILE_DELAY: 1 # seconds
  41. _checkIfRecentlyCompiled: (project_id, user_id, callback = (error, recentlyCompiled) ->) ->
  42. key = "compile:#{project_id}:#{user_id}"
  43. rclient.set key, true, "EX", @COMPILE_DELAY, "NX", (error, ok) ->
  44. return callback(error) if error?
  45. if ok == "OK"
  46. return callback null, false
  47. else
  48. return callback null, true
  49. _checkIfAutoCompileLimitHasBeenHit: (isAutoCompile, callback = (err, canCompile)->)->
  50. if !isAutoCompile
  51. return callback(null, true)
  52. opts =
  53. endpointName:"auto_compile"
  54. timeInterval:15
  55. subjectName:"everyone"
  56. throttle: 10
  57. rateLimiter.addCount opts, (err, canCompile)->
  58. if err?
  59. canCompile = false
  60. logger.log canCompile:canCompile, opts:opts, "checking if auto compile limit has been hit"
  61. callback err, canCompile
  62. _ensureRootDocumentIsSet: (project_id, callback = (error) ->) ->
  63. Project.findById project_id, 'rootDoc_id', (error, project)=>
  64. return callback(error) if error?
  65. if !project?
  66. return callback new Error("project not found")
  67. if project.rootDoc_id?
  68. callback()
  69. else
  70. ProjectRootDocManager.setRootDocAutomatically project_id, callback