Merge pull request #13510 from overleaf/em-promisify-clsi-manager

Clean up of ClsiManager and ClsiCookieManager

GitOrigin-RevId: e5047b253613e87fd6cb4f12855b821028fcaf8e
This commit is contained in:
Eric Mc Sween
2023-06-26 13:42:11 +01:00
committed by Copybot
parent a59d0711f3
commit 33a8a90134
5 changed files with 1723 additions and 2088 deletions
@@ -1,32 +1,19 @@
/* eslint-disable
n/handle-callback-err,
max-len,
*/
// TODO: This file was created by bulk-decaffeinate.
// Fix any style issues and re-enable lint.
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* DS207: Consider shorter variations of null checks
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
let rclientSecondary
const { URL, URLSearchParams } = require('url')
const OError = require('@overleaf/o-error')
const Settings = require('@overleaf/settings')
const request = require('request').defaults({ timeout: 30 * 1000 })
const RedisWrapper = require('../../infrastructure/RedisWrapper')
const rclient = RedisWrapper.client('clsi_cookie')
if (Settings.redis.clsi_cookie_secondary != null) {
rclientSecondary = RedisWrapper.client('clsi_cookie_secondary')
}
const Cookie = require('cookie')
const logger = require('@overleaf/logger')
const Metrics = require('@overleaf/metrics')
const clsiCookiesEnabled =
(Settings.clsiCookie != null ? Settings.clsiCookie.key : undefined) != null &&
Settings.clsiCookie.key.length !== 0
const clsiCookiesEnabled = (Settings.clsiCookie?.key ?? '') !== ''
const rclient = RedisWrapper.client('clsi_cookie')
let rclientSecondary
if (Settings.redis.clsi_cookie_secondary != null) {
rclientSecondary = RedisWrapper.client('clsi_cookie_secondary')
}
module.exports = function (backendGroup) {
return {
@@ -45,15 +32,12 @@ module.exports = function (backendGroup) {
compileBackendClass,
callback
) {
if (callback == null) {
callback = function () {}
}
return rclient.get(this.buildKey(projectId, userId), (err, serverId) => {
if (err != null) {
rclient.get(this.buildKey(projectId, userId), (err, serverId) => {
if (err) {
return callback(err)
}
if (serverId == null || serverId === '') {
return this._populateServerIdViaRequest(
this._populateServerIdViaRequest(
projectId,
userId,
compileGroup,
@@ -61,7 +45,7 @@ module.exports = function (backendGroup) {
callback
)
} else {
return callback(null, serverId)
callback(null, serverId)
}
})
},
@@ -73,16 +57,13 @@ module.exports = function (backendGroup) {
compileBackendClass,
callback
) {
if (callback == null) {
callback = function () {}
}
const u = new URL(`${Settings.apis.clsi.url}/project/${projectId}/status`)
u.search = new URLSearchParams({
compileGroup,
compileBackendClass,
}).toString()
request.post(u.href, (err, res, body) => {
if (err != null) {
if (err) {
OError.tag(err, 'error getting initial server id for project', {
project_id: projectId,
})
@@ -96,25 +77,21 @@ module.exports = function (backendGroup) {
res,
null,
function (err, serverId) {
if (err != null) {
if (err) {
logger.warn(
{ err, projectId },
'error setting server id via populate request'
)
}
return callback(err, serverId)
callback(err, serverId)
}
)
})
},
_parseServerIdFromResponse(response) {
const cookies = Cookie.parse(
(response.headers['set-cookie'] != null
? response.headers['set-cookie'][0]
: undefined) || ''
)
return cookies != null ? cookies[Settings.clsiCookie.key] : undefined
const cookies = Cookie.parse(response.headers['set-cookie']?.[0] || '')
return cookies?.[Settings.clsiCookie.key]
},
checkIsLoadSheddingEvent(clsiserverid, compileGroup, compileBackendClass) {
@@ -155,9 +132,6 @@ module.exports = function (backendGroup) {
previous,
callback
) {
if (callback == null) {
callback = function () {}
}
if (!clsiCookiesEnabled) {
return callback()
}
@@ -195,9 +169,6 @@ module.exports = function (backendGroup) {
},
_setServerIdInRedis(rclient, projectId, userId, serverId, callback) {
if (callback == null) {
callback = function () {}
}
rclient.setex(
this.buildKey(projectId, userId),
this._getTTLInSeconds(serverId),
@@ -207,13 +178,10 @@ module.exports = function (backendGroup) {
},
clearServerId(projectId, userId, callback) {
if (callback == null) {
callback = function () {}
}
if (!clsiCookiesEnabled) {
return callback()
}
return rclient.del(this.buildKey(projectId, userId), callback)
rclient.del(this.buildKey(projectId, userId), callback)
},
getCookieJar(
@@ -223,13 +191,10 @@ module.exports = function (backendGroup) {
compileBackendClass,
callback
) {
if (callback == null) {
callback = function () {}
}
if (!clsiCookiesEnabled) {
return callback(null, request.jar(), undefined)
}
return this._getServerId(
this._getServerId(
projectId,
userId,
compileGroup,
@@ -246,7 +211,7 @@ module.exports = function (backendGroup) {
)
const jar = request.jar()
jar.setCookie(serverCookie, Settings.apis.clsi.url)
return callback(null, jar, serverId)
callback(null, jar, serverId)
}
)
},
File diff suppressed because it is too large Load Diff
+1 -1
View File
@@ -1272,7 +1272,7 @@ function initialize(webRouter, privateApiRouter, publicApiRouter) {
const sendRes = _.once(function (statusCode, message) {
res.status(statusCode)
plainTextResponse(res, message)
ClsiCookieManager.clearServerId(projectId)
ClsiCookieManager.clearServerId(projectId, () => {})
}) // force every compile to a new server
// set a timeout
let handler = setTimeout(function () {