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"
|
|
|
|
|
2017-08-02 11:25:47 -04:00
|
|
|
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 =
|
|
|
|
|
2017-08-02 11:25:47 -04:00
|
|
|
checkState: (project_id, project, callback = (err, ok) ->) ->
|
2017-08-01 09:39:57 -04:00
|
|
|
newState = buildState(project)
|
2017-08-02 11:25:47 -04:00
|
|
|
@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)
|
|
|
|
|
2017-08-02 11:25:47 -04:00
|
|
|
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?
|
2017-08-02 11:25:47 -04:00
|
|
|
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)
|
|
|
|
|
2017-08-02 11:25:47 -04:00
|
|
|
setState: (project_id, project, callback = (err)->)->
|
2017-08-01 09:39:57 -04:00
|
|
|
projectState = buildState project
|
2017-08-02 11:25:47 -04:00
|
|
|
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)
|