ClsiStateManager.coffee 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. Settings = require "settings-sharelatex"
  2. RedisWrapper = require("../../infrastructure/RedisWrapper")
  3. rclient = RedisWrapper.client("clsi_state")
  4. logger = require "logger-sharelatex"
  5. crypto = require "crypto"
  6. buildKey = (project_id)->
  7. return "clsistate:#{project_id}" # FIXME: should we cluster these on project??
  8. # the "state" of a project is a hash of the relevant attributes in the
  9. # project object in this case we only need the rootFolder.
  10. #
  11. # When it changes the full set of files on the CLSI will need to be
  12. # updated. If it doesn't change then we can overwrite changed docs in
  13. # place on the clsi, getting them from the docupdater.
  14. #
  15. # The docupdater is also responsible for unsetting the key in redis if
  16. # it removes any documents from the doc updater.
  17. buildState = (project) ->
  18. json = JSON.stringify(project.rootFolder)
  19. return crypto.createHash('sha1').update(json, 'utf8').digest('hex')
  20. clsiStateEnabled = Settings.clsiState
  21. OneHour = 3600 * 1000
  22. module.exports = ClsiStateManager =
  23. checkProjectStateMatch: (project_id, project, callback = (err, ok) ->) ->
  24. newState = buildState(project)
  25. rclient.get buildKey(project_id), (err, oldState) ->
  26. return callback(err) if err?
  27. logger.log project_id: project_id, new_state: newState, old_state: oldState, "got project state from redis"
  28. if newState is oldState
  29. callback(null,true,oldState)
  30. else
  31. callback(null,false)
  32. setProjectState: (project_id, project, callback = (err)->)->
  33. projectState = buildState(project)
  34. logger.log project_id: project_id, projectState: projectState, "setting project state in redis"
  35. rclient.set buildKey(project_id), projectState, "PX", OneHour, (err) ->
  36. return callback(err) if err?
  37. callback(null,projectState)