overleaf/services/web/app/coffee/Features/Compile/ClsiStateManager.coffee

46 lines
1.7 KiB
CoffeeScript
Raw Normal View History

2017-08-01 09:39:57 -04:00
Settings = require "settings-sharelatex"
RedisWrapper = require("../../infrastructure/RedisWrapper")
rclient = RedisWrapper.client("clsi_state")
logger = require "logger-sharelatex"
crypto = require "crypto"
buildKey = (project_id)->
return "clsistate:#{project_id}" # FIXME: should we cluster these on project??
2017-08-01 09:39:57 -04:00
2017-08-03 05:51:21 -04:00
# the "state" of a project is a hash of the relevant attributes in the
# project object in this case we only need the rootFolder.
#
# When it changes the full set of files on the CLSI will need to be
# updated. If it doesn't change then we can overwrite changed docs in
# place on the clsi, getting them from the docupdater.
#
# The docupdater is also responsible for unsetting the key in redis if
# it removes any documents from the doc updater.
2017-08-01 09:39:57 -04:00
buildState = (project) ->
2017-08-03 05:51:21 -04:00
json = JSON.stringify(project.rootFolder)
return crypto.createHash('sha1').update(json, 'utf8').digest('hex')
2017-08-01 09:39:57 -04:00
clsiStateEnabled = Settings.clsiState
OneHour = 3600 * 1000
module.exports = ClsiStateManager =
2017-08-03 05:51:21 -04:00
checkProjectStateMatch: (project_id, project, callback = (err, ok) ->) ->
2017-08-01 09:39:57 -04:00
newState = buildState(project)
2017-08-03 05:51:21 -04:00
rclient.get buildKey(project_id), (err, oldState) ->
2017-08-01 09:39:57 -04:00
return callback(err) if err?
2017-08-03 05:51:21 -04:00
logger.log project_id: project_id, new_state: newState, old_state: oldState, "got project state from redis"
2017-08-01 09:39:57 -04:00
if newState is oldState
2017-08-03 05:51:21 -04:00
callback(null,true,oldState)
2017-08-01 09:39:57 -04:00
else
callback(null,false)
2017-08-03 05:51:21 -04:00
setProjectState: (project_id, project, callback = (err)->)->
projectState = buildState(project)
logger.log project_id: project_id, projectState: projectState, "setting project state in redis"
rclient.set buildKey(project_id), projectState, "PX", OneHour, (err) ->
2017-08-01 09:39:57 -04:00
return callback(err) if err?
2017-08-03 05:51:21 -04:00
callback(null,projectState)