ClsiStateManager.coffee 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. # The idea is that it will change if any doc or file is
  12. # created/renamed/deleted, and also if the content of any file (not
  13. # doc) changes.
  14. #
  15. # When the hash changes the full set of files on the CLSI will need to
  16. # be updated. If it doesn't change then we can overwrite changed docs
  17. # in place on the clsi, getting them from the docupdater.
  18. #
  19. # The docupdater is also responsible for unsetting the key in redis if
  20. # it removes any documents from the doc updater.
  21. buildState = (project) ->
  22. json = JSON.stringify(project.rootFolder)
  23. return crypto.createHash('sha1').update(json, 'utf8').digest('hex')
  24. clsiStateEnabled = Settings.clsiState
  25. OneHour = 3600 * 1000
  26. module.exports = ClsiStateManager =
  27. checkProjectStateMatch: (project_id, project, callback = (err, ok) ->) ->
  28. newState = buildState(project)
  29. rclient.get buildKey(project_id), (err, oldState) ->
  30. return callback(err) if err?
  31. logger.log project_id: project_id, new_state: newState, old_state: oldState, "got project state from redis"
  32. if newState is oldState
  33. callback(null,true,oldState)
  34. else
  35. callback(null,false)
  36. setProjectState: (project_id, project, callback = (err)->)->
  37. projectState = buildState(project)
  38. logger.log project_id: project_id, projectState: projectState, "setting project state in redis"
  39. rclient.set buildKey(project_id), projectState, "PX", OneHour, (err) ->
  40. return callback(err) if err?
  41. callback(null,projectState)