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

42 lines
1.4 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
buildState = (project) ->
JSON.stringify project
clsiStateEnabled = Settings.clsiState
OneHour = 3600 * 1000
module.exports = ClsiStateManager =
checkState: (project_id, project, callback = (err, ok) ->) ->
2017-08-01 09:39:57 -04:00
newState = buildState(project)
@getState project_id, (err, oldState) ->
2017-08-01 09:39:57 -04:00
return callback(err) if err?
if newState is oldState
hash = crypto.createHash('sha1').update(newState, 'utf8').digest('hex')
callback(null,true,hash)
else
callback(null,false)
getState: (project_id, callback = (err, state)->)->
rclient.get buildKey(project_id), (err, state)->
2017-08-01 09:39:57 -04:00
return callback(err) if err?
logger.log project_id: project_id, state: state, "got project state from redis"
2017-08-01 09:39:57 -04:00
return callback(null, state)
setState: (project_id, project, callback = (err)->)->
2017-08-01 09:39:57 -04:00
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?
hash = crypto.createHash('sha1').update(projectState, 'utf8').digest('hex')
callback(null,hash)