2023-10-18 04:32:14 -04:00
|
|
|
// Metrics must be initialized before importing anything else
|
|
|
|
require('@overleaf/metrics/initialize')
|
2020-02-19 06:16:18 -05:00
|
|
|
|
|
|
|
const CompileController = require('./app/js/CompileController')
|
2021-05-13 09:07:54 -04:00
|
|
|
const ContentController = require('./app/js/ContentController')
|
2021-07-12 12:47:21 -04:00
|
|
|
const Settings = require('@overleaf/settings')
|
2022-03-01 10:09:36 -05:00
|
|
|
const logger = require('@overleaf/logger')
|
2020-02-19 06:16:18 -05:00
|
|
|
logger.initialize('clsi')
|
2022-06-06 07:41:07 -04:00
|
|
|
if (Settings.sentry.dsn != null) {
|
2020-02-19 06:16:18 -05:00
|
|
|
logger.initializeErrorReporting(Settings.sentry.dsn)
|
2020-02-19 06:16:17 -05:00
|
|
|
}
|
2023-10-18 04:32:14 -04:00
|
|
|
const Metrics = require('@overleaf/metrics')
|
2020-02-19 06:16:17 -05:00
|
|
|
|
2020-05-07 12:57:35 -04:00
|
|
|
const smokeTest = require('./test/smoke/js/SmokeTests')
|
2020-02-19 06:16:18 -05:00
|
|
|
const ContentTypeMapper = require('./app/js/ContentTypeMapper')
|
|
|
|
const Errors = require('./app/js/Errors')
|
2020-02-19 06:16:17 -05:00
|
|
|
|
2020-02-19 06:16:18 -05:00
|
|
|
const Path = require('path')
|
2020-02-19 06:16:17 -05:00
|
|
|
|
2023-05-22 06:36:51 -04:00
|
|
|
Metrics.open_sockets.monitor(true)
|
2020-02-19 06:16:18 -05:00
|
|
|
Metrics.memory.monitor(logger)
|
2023-05-30 09:25:50 -04:00
|
|
|
Metrics.leaked_sockets.monitor(logger)
|
2020-02-19 06:16:17 -05:00
|
|
|
|
2020-02-19 06:16:18 -05:00
|
|
|
const ProjectPersistenceManager = require('./app/js/ProjectPersistenceManager')
|
|
|
|
const OutputCacheManager = require('./app/js/OutputCacheManager')
|
2021-05-13 09:56:15 -04:00
|
|
|
const ContentCacheManager = require('./app/js/ContentCacheManager')
|
2020-02-19 06:16:17 -05:00
|
|
|
|
2021-10-06 04:12:09 -04:00
|
|
|
ProjectPersistenceManager.init()
|
2021-11-15 07:07:17 -05:00
|
|
|
OutputCacheManager.init()
|
2020-02-19 06:16:17 -05:00
|
|
|
|
2020-02-19 06:16:18 -05:00
|
|
|
const express = require('express')
|
|
|
|
const bodyParser = require('body-parser')
|
|
|
|
const app = express()
|
2020-02-19 06:16:17 -05:00
|
|
|
|
2020-02-19 06:16:18 -05:00
|
|
|
Metrics.injectMetricsRoute(app)
|
|
|
|
app.use(Metrics.http.monitor(logger))
|
2020-02-19 06:16:17 -05:00
|
|
|
|
|
|
|
// Compile requests can take longer than the default two
|
2020-02-19 06:16:18 -05:00
|
|
|
// minutes (including file download time), so bump up the
|
2020-02-19 06:16:17 -05:00
|
|
|
// timeout a bit.
|
2020-02-19 06:16:18 -05:00
|
|
|
const TIMEOUT = 10 * 60 * 1000
|
2020-08-10 12:01:11 -04:00
|
|
|
app.use(function (req, res, next) {
|
2020-02-19 06:16:18 -05:00
|
|
|
req.setTimeout(TIMEOUT)
|
|
|
|
res.setTimeout(TIMEOUT)
|
|
|
|
res.removeHeader('X-Powered-By')
|
2022-06-06 07:41:07 -04:00
|
|
|
next()
|
2020-02-19 06:16:18 -05:00
|
|
|
})
|
2020-02-19 06:16:17 -05:00
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
app.param('project_id', function (req, res, next, projectId) {
|
2022-06-06 07:41:07 -04:00
|
|
|
if (projectId?.match(/^[a-zA-Z0-9_-]+$/)) {
|
|
|
|
next()
|
2020-02-19 06:16:18 -05:00
|
|
|
} else {
|
2022-06-06 07:41:07 -04:00
|
|
|
next(new Error('invalid project id'))
|
2020-02-19 06:16:18 -05:00
|
|
|
}
|
|
|
|
})
|
2020-02-19 06:16:17 -05:00
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
app.param('user_id', function (req, res, next, userId) {
|
2022-06-06 07:41:07 -04:00
|
|
|
if (userId?.match(/^[0-9a-f]{24}$/)) {
|
|
|
|
next()
|
2020-02-19 06:16:18 -05:00
|
|
|
} else {
|
2022-06-06 07:41:07 -04:00
|
|
|
next(new Error('invalid user id'))
|
2020-02-19 06:16:18 -05:00
|
|
|
}
|
|
|
|
})
|
2020-02-19 06:16:17 -05:00
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
app.param('build_id', function (req, res, next, buildId) {
|
2022-06-06 07:41:07 -04:00
|
|
|
if (buildId?.match(OutputCacheManager.BUILD_REGEX)) {
|
|
|
|
next()
|
2020-02-19 06:16:18 -05:00
|
|
|
} else {
|
2022-06-06 07:41:07 -04:00
|
|
|
next(new Error(`invalid build id ${buildId}`))
|
2020-02-19 06:16:18 -05:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-05-13 09:56:15 -04:00
|
|
|
app.param('contentId', function (req, res, next, contentId) {
|
2022-06-06 07:41:07 -04:00
|
|
|
if (contentId?.match(OutputCacheManager.CONTENT_REGEX)) {
|
|
|
|
next()
|
2021-05-13 09:56:15 -04:00
|
|
|
} else {
|
2022-06-06 07:41:07 -04:00
|
|
|
next(new Error(`invalid content id ${contentId}`))
|
2021-05-13 09:56:15 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
app.param('hash', function (req, res, next, hash) {
|
2022-06-06 07:41:07 -04:00
|
|
|
if (hash?.match(ContentCacheManager.HASH_REGEX)) {
|
|
|
|
next()
|
2021-05-13 09:56:15 -04:00
|
|
|
} else {
|
2022-06-06 07:41:07 -04:00
|
|
|
next(new Error(`invalid hash ${hash}`))
|
2021-05-13 09:56:15 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-02-19 06:16:18 -05:00
|
|
|
app.post(
|
|
|
|
'/project/:project_id/compile',
|
|
|
|
bodyParser.json({ limit: Settings.compileSizeLimit }),
|
|
|
|
CompileController.compile
|
|
|
|
)
|
|
|
|
app.post('/project/:project_id/compile/stop', CompileController.stopCompile)
|
|
|
|
app.delete('/project/:project_id', CompileController.clearCache)
|
|
|
|
|
|
|
|
app.get('/project/:project_id/sync/code', CompileController.syncFromCode)
|
|
|
|
app.get('/project/:project_id/sync/pdf', CompileController.syncFromPdf)
|
|
|
|
app.get('/project/:project_id/wordcount', CompileController.wordcount)
|
|
|
|
app.get('/project/:project_id/status', CompileController.status)
|
2021-03-16 06:24:59 -04:00
|
|
|
app.post('/project/:project_id/status', CompileController.status)
|
2020-02-19 06:16:17 -05:00
|
|
|
|
|
|
|
// Per-user containers
|
2020-02-19 06:16:18 -05:00
|
|
|
app.post(
|
|
|
|
'/project/:project_id/user/:user_id/compile',
|
|
|
|
bodyParser.json({ limit: Settings.compileSizeLimit }),
|
|
|
|
CompileController.compile
|
|
|
|
)
|
|
|
|
app.post(
|
|
|
|
'/project/:project_id/user/:user_id/compile/stop',
|
|
|
|
CompileController.stopCompile
|
|
|
|
)
|
|
|
|
app.delete('/project/:project_id/user/:user_id', CompileController.clearCache)
|
|
|
|
|
|
|
|
app.get(
|
|
|
|
'/project/:project_id/user/:user_id/sync/code',
|
|
|
|
CompileController.syncFromCode
|
|
|
|
)
|
|
|
|
app.get(
|
|
|
|
'/project/:project_id/user/:user_id/sync/pdf',
|
|
|
|
CompileController.syncFromPdf
|
|
|
|
)
|
|
|
|
app.get(
|
|
|
|
'/project/:project_id/user/:user_id/wordcount',
|
|
|
|
CompileController.wordcount
|
|
|
|
)
|
|
|
|
|
|
|
|
const ForbidSymlinks = require('./app/js/StaticServerForbidSymlinks')
|
2020-02-19 06:16:17 -05:00
|
|
|
|
|
|
|
// create a static server which does not allow access to any symlinks
|
|
|
|
// avoids possible mismatch of root directory between middleware check
|
|
|
|
// and serving the files
|
2020-12-15 07:03:25 -05:00
|
|
|
const staticCompileServer = ForbidSymlinks(
|
|
|
|
express.static,
|
|
|
|
Settings.path.compilesDir,
|
|
|
|
{
|
|
|
|
setHeaders(res, path, stat) {
|
|
|
|
if (Path.basename(path) === 'output.pdf') {
|
|
|
|
// Calculate an etag in the same way as nginx
|
|
|
|
// https://github.com/tj/send/issues/65
|
|
|
|
const etag = (path, stat) =>
|
|
|
|
`"${Math.ceil(+stat.mtime / 1000).toString(16)}` +
|
|
|
|
'-' +
|
|
|
|
Number(stat.size).toString(16) +
|
|
|
|
'"'
|
|
|
|
res.set('Etag', etag(path, stat))
|
|
|
|
}
|
2022-06-06 07:41:07 -04:00
|
|
|
res.set('Content-Type', ContentTypeMapper.map(path))
|
2021-07-13 07:04:48 -04:00
|
|
|
},
|
2020-02-19 06:16:18 -05:00
|
|
|
}
|
2020-12-15 07:03:25 -05:00
|
|
|
)
|
2020-02-19 06:16:18 -05:00
|
|
|
|
2020-12-15 09:59:05 -05:00
|
|
|
const staticOutputServer = ForbidSymlinks(
|
|
|
|
express.static,
|
|
|
|
Settings.path.outputDir,
|
|
|
|
{
|
|
|
|
setHeaders(res, path, stat) {
|
|
|
|
if (Path.basename(path) === 'output.pdf') {
|
|
|
|
// Calculate an etag in the same way as nginx
|
|
|
|
// https://github.com/tj/send/issues/65
|
|
|
|
const etag = (path, stat) =>
|
|
|
|
`"${Math.ceil(+stat.mtime / 1000).toString(16)}` +
|
|
|
|
'-' +
|
|
|
|
Number(stat.size).toString(16) +
|
|
|
|
'"'
|
|
|
|
res.set('Etag', etag(path, stat))
|
|
|
|
}
|
2022-06-06 07:41:07 -04:00
|
|
|
res.set('Content-Type', ContentTypeMapper.map(path))
|
2021-07-13 07:04:48 -04:00
|
|
|
},
|
2020-12-15 09:59:05 -05:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
app.get(
|
|
|
|
'/project/:project_id/user/:user_id/build/:build_id/output/*',
|
|
|
|
function (req, res, next) {
|
|
|
|
// for specific build get the path from the OutputCacheManager (e.g. .clsi/buildId)
|
|
|
|
req.url =
|
|
|
|
`/${req.params.project_id}-${req.params.user_id}/` +
|
|
|
|
OutputCacheManager.path(req.params.build_id, `/${req.params[0]}`)
|
2022-06-06 07:41:07 -04:00
|
|
|
staticOutputServer(req, res, next)
|
2020-08-10 12:01:11 -04:00
|
|
|
}
|
|
|
|
)
|
2020-02-19 06:16:18 -05:00
|
|
|
|
2021-05-13 09:07:54 -04:00
|
|
|
app.get(
|
|
|
|
'/project/:projectId/content/:contentId/:hash',
|
|
|
|
ContentController.getPdfRange
|
|
|
|
)
|
|
|
|
app.get(
|
|
|
|
'/project/:projectId/user/:userId/content/:contentId/:hash',
|
|
|
|
ContentController.getPdfRange
|
|
|
|
)
|
|
|
|
|
2021-07-13 07:04:48 -04:00
|
|
|
app.get(
|
|
|
|
'/project/:project_id/build/:build_id/output/*',
|
|
|
|
function (req, res, next) {
|
|
|
|
// for specific build get the path from the OutputCacheManager (e.g. .clsi/buildId)
|
|
|
|
req.url =
|
|
|
|
`/${req.params.project_id}/` +
|
|
|
|
OutputCacheManager.path(req.params.build_id, `/${req.params[0]}`)
|
2022-06-06 07:41:07 -04:00
|
|
|
staticOutputServer(req, res, next)
|
2021-07-13 07:04:48 -04:00
|
|
|
}
|
|
|
|
)
|
2020-02-19 06:16:18 -05:00
|
|
|
|
2021-07-13 07:04:48 -04:00
|
|
|
app.get(
|
|
|
|
'/project/:project_id/user/:user_id/output/*',
|
|
|
|
function (req, res, next) {
|
|
|
|
// for specific user get the path to the top level file
|
|
|
|
logger.warn(
|
|
|
|
{ url: req.url },
|
|
|
|
'direct request for file in compile directory'
|
|
|
|
)
|
|
|
|
req.url = `/${req.params.project_id}-${req.params.user_id}/${req.params[0]}`
|
2022-06-06 07:41:07 -04:00
|
|
|
staticCompileServer(req, res, next)
|
2021-07-13 07:04:48 -04:00
|
|
|
}
|
|
|
|
)
|
2020-02-19 06:16:18 -05:00
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
app.get('/project/:project_id/output/*', function (req, res, next) {
|
2021-01-22 06:03:47 -05:00
|
|
|
logger.warn({ url: req.url }, 'direct request for file in compile directory')
|
2022-06-06 07:41:07 -04:00
|
|
|
if (req.query?.build?.match(OutputCacheManager.BUILD_REGEX)) {
|
2020-02-19 06:16:18 -05:00
|
|
|
// for specific build get the path from the OutputCacheManager (e.g. .clsi/buildId)
|
|
|
|
req.url =
|
|
|
|
`/${req.params.project_id}/` +
|
|
|
|
OutputCacheManager.path(req.query.build, `/${req.params[0]}`)
|
|
|
|
} else {
|
|
|
|
req.url = `/${req.params.project_id}/${req.params[0]}`
|
|
|
|
}
|
2022-06-06 07:41:07 -04:00
|
|
|
staticCompileServer(req, res, next)
|
2020-02-19 06:16:18 -05:00
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
app.get('/oops', function (req, res, next) {
|
2020-02-19 06:16:18 -05:00
|
|
|
logger.error({ err: 'hello' }, 'test error')
|
2022-06-06 07:41:07 -04:00
|
|
|
res.send('error\n')
|
2020-02-19 06:16:18 -05:00
|
|
|
})
|
|
|
|
|
2021-01-26 11:35:39 -05:00
|
|
|
app.get('/oops-internal', function (req, res, next) {
|
|
|
|
setTimeout(function () {
|
|
|
|
throw new Error('Test error')
|
|
|
|
}, 1)
|
|
|
|
})
|
|
|
|
|
2020-02-19 06:16:18 -05:00
|
|
|
app.get('/status', (req, res, next) => res.send('CLSI is alive\n'))
|
2020-02-19 06:16:17 -05:00
|
|
|
|
2020-05-11 07:08:13 -04:00
|
|
|
Settings.processTooOld = false
|
2020-04-23 06:32:33 -04:00
|
|
|
if (Settings.processLifespanLimitMs) {
|
2022-08-29 08:29:20 -04:00
|
|
|
// Pre-emp instances have a maximum lifespan of 24h after which they will be
|
|
|
|
// shutdown, with a 30s grace period.
|
|
|
|
// Spread cycling of VMs by up-to 2.4h _before_ their limit to avoid large
|
|
|
|
// numbers of VMs that are temporarily unavailable (while they reboot).
|
|
|
|
Settings.processLifespanLimitMs -=
|
2020-04-23 06:32:33 -04:00
|
|
|
Settings.processLifespanLimitMs * (Math.random() / 10)
|
2022-08-29 08:29:20 -04:00
|
|
|
logger.info(
|
|
|
|
{ target: new Date(Date.now() + Settings.processLifespanLimitMs) },
|
|
|
|
'Lifespan limited'
|
2020-05-07 12:57:35 -04:00
|
|
|
)
|
2020-04-23 06:32:33 -04:00
|
|
|
|
2020-05-07 12:57:35 -04:00
|
|
|
setTimeout(() => {
|
2022-08-29 08:29:20 -04:00
|
|
|
logger.info({}, 'shutting down, process is too old')
|
2020-05-11 07:08:13 -04:00
|
|
|
Settings.processTooOld = true
|
2020-05-07 12:57:35 -04:00
|
|
|
}, Settings.processLifespanLimitMs)
|
2020-03-20 09:37:58 -04:00
|
|
|
}
|
|
|
|
|
2020-06-02 04:51:34 -04:00
|
|
|
function runSmokeTest() {
|
|
|
|
if (Settings.processTooOld) return
|
2022-03-02 07:59:57 -05:00
|
|
|
const INTERVAL = 30 * 1000
|
|
|
|
if (
|
|
|
|
smokeTest.lastRunSuccessful() &&
|
2022-06-06 07:41:07 -04:00
|
|
|
CompileController.timeSinceLastSuccessfulCompile() < INTERVAL / 2
|
2022-03-02 07:59:57 -05:00
|
|
|
) {
|
2022-05-16 08:38:18 -04:00
|
|
|
logger.debug('skipping smoke tests, got recent successful user compile')
|
2022-03-02 07:59:57 -05:00
|
|
|
return setTimeout(runSmokeTest, INTERVAL / 2)
|
|
|
|
}
|
2022-05-16 08:38:18 -04:00
|
|
|
logger.debug('running smoke tests')
|
2021-07-13 07:04:48 -04:00
|
|
|
smokeTest.triggerRun(err => {
|
2020-06-02 04:51:34 -04:00
|
|
|
if (err) logger.error({ err }, 'smoke tests failed')
|
2022-03-02 07:59:57 -05:00
|
|
|
setTimeout(runSmokeTest, INTERVAL)
|
2020-06-02 04:51:34 -04:00
|
|
|
})
|
|
|
|
}
|
2020-02-19 06:16:17 -05:00
|
|
|
if (Settings.smokeTest) {
|
2020-03-20 09:37:58 -04:00
|
|
|
runSmokeTest()
|
2020-02-19 06:16:17 -05:00
|
|
|
}
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
app.get('/health_check', function (req, res) {
|
2020-05-11 07:08:13 -04:00
|
|
|
if (Settings.processTooOld) {
|
|
|
|
return res.status(500).json({ processTooOld: true })
|
|
|
|
}
|
2020-05-07 12:57:35 -04:00
|
|
|
smokeTest.sendLastResult(res)
|
2020-02-19 06:16:18 -05:00
|
|
|
})
|
|
|
|
|
2020-05-07 12:57:35 -04:00
|
|
|
app.get('/smoke_test_force', (req, res) => smokeTest.sendNewResult(res))
|
2020-02-19 06:16:18 -05:00
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
app.use(function (error, req, res, next) {
|
2020-02-19 06:16:18 -05:00
|
|
|
if (error instanceof Errors.NotFoundError) {
|
2022-05-16 08:38:18 -04:00
|
|
|
logger.debug({ err: error, url: req.url }, 'not found error')
|
2022-06-06 07:41:07 -04:00
|
|
|
res.sendStatus(404)
|
2020-06-11 05:54:26 -04:00
|
|
|
} else if (error.code === 'EPIPE') {
|
|
|
|
// inspect container returns EPIPE when shutting down
|
2022-06-06 07:41:07 -04:00
|
|
|
res.sendStatus(503) // send 503 Unavailable response
|
2020-02-19 06:16:18 -05:00
|
|
|
} else {
|
|
|
|
logger.error({ err: error, url: req.url }, 'server error')
|
2022-06-06 07:41:07 -04:00
|
|
|
res.sendStatus(error.statusCode || 500)
|
2020-02-19 06:16:18 -05:00
|
|
|
}
|
|
|
|
})
|
2020-02-19 06:16:17 -05:00
|
|
|
|
2020-02-19 06:16:18 -05:00
|
|
|
const net = require('net')
|
|
|
|
const os = require('os')
|
2020-02-19 06:16:17 -05:00
|
|
|
|
2020-02-19 06:16:18 -05:00
|
|
|
let STATE = 'up'
|
2020-02-19 06:16:17 -05:00
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
const loadTcpServer = net.createServer(function (socket) {
|
|
|
|
socket.on('error', function (err) {
|
2020-02-19 06:16:18 -05:00
|
|
|
if (err.code === 'ECONNRESET') {
|
|
|
|
// this always comes up, we don't know why
|
|
|
|
return
|
|
|
|
}
|
|
|
|
logger.err({ err }, 'error with socket on load check')
|
2022-06-06 07:41:07 -04:00
|
|
|
socket.destroy()
|
2020-02-19 06:16:18 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
if (STATE === 'up' && Settings.internal.load_balancer_agent.report_load) {
|
|
|
|
let availableWorkingCpus
|
|
|
|
const currentLoad = os.loadavg()[0]
|
|
|
|
|
|
|
|
// staging clis's have 1 cpu core only
|
|
|
|
if (os.cpus().length === 1) {
|
|
|
|
availableWorkingCpus = 1
|
|
|
|
} else {
|
|
|
|
availableWorkingCpus = os.cpus().length - 1
|
|
|
|
}
|
|
|
|
|
|
|
|
const freeLoad = availableWorkingCpus - currentLoad
|
2021-08-26 08:02:09 -04:00
|
|
|
const freeLoadPercentage = Math.round(
|
|
|
|
(freeLoad / availableWorkingCpus) * 100
|
|
|
|
)
|
2020-02-19 06:16:18 -05:00
|
|
|
if (freeLoadPercentage <= 0) {
|
2021-08-26 08:02:09 -04:00
|
|
|
// When its 0 the server is set to drain implicitly.
|
|
|
|
// Drain will move new projects to different servers.
|
|
|
|
// Drain will keep existing projects assigned to the same server.
|
|
|
|
// Maint will more existing and new projects to different servers.
|
|
|
|
socket.write(`maint, 0%\n`, 'ASCII')
|
|
|
|
} else {
|
|
|
|
// Ready will cancel the maint state.
|
|
|
|
socket.write(`up, ready, ${freeLoadPercentage}%\n`, 'ASCII')
|
2020-02-19 06:16:18 -05:00
|
|
|
}
|
2022-06-06 07:41:07 -04:00
|
|
|
socket.end()
|
2020-02-19 06:16:18 -05:00
|
|
|
} else {
|
|
|
|
socket.write(`${STATE}\n`, 'ASCII')
|
2022-06-06 07:41:07 -04:00
|
|
|
socket.end()
|
2020-02-19 06:16:18 -05:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
const loadHttpServer = express()
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
loadHttpServer.post('/state/up', function (req, res, next) {
|
2020-02-19 06:16:18 -05:00
|
|
|
STATE = 'up'
|
2022-05-16 08:38:18 -04:00
|
|
|
logger.debug('getting message to set server to down')
|
2022-06-06 07:41:07 -04:00
|
|
|
res.sendStatus(204)
|
2020-02-19 06:16:18 -05:00
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
loadHttpServer.post('/state/down', function (req, res, next) {
|
2020-02-19 06:16:18 -05:00
|
|
|
STATE = 'down'
|
2022-05-16 08:38:18 -04:00
|
|
|
logger.debug('getting message to set server to down')
|
2022-06-06 07:41:07 -04:00
|
|
|
res.sendStatus(204)
|
2020-02-19 06:16:18 -05:00
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
loadHttpServer.post('/state/maint', function (req, res, next) {
|
2020-02-19 06:16:18 -05:00
|
|
|
STATE = 'maint'
|
2022-05-16 08:38:18 -04:00
|
|
|
logger.debug('getting message to set server to maint')
|
2022-06-06 07:41:07 -04:00
|
|
|
res.sendStatus(204)
|
2020-02-19 06:16:18 -05:00
|
|
|
})
|
|
|
|
|
2024-04-11 06:41:51 -04:00
|
|
|
const port = Settings.internal.clsi.port
|
|
|
|
const host = Settings.internal.clsi.host
|
2020-02-19 06:16:18 -05:00
|
|
|
|
2020-06-02 04:51:34 -04:00
|
|
|
const loadTcpPort = Settings.internal.load_balancer_agent.load_port
|
|
|
|
const loadHttpPort = Settings.internal.load_balancer_agent.local_port
|
2020-02-19 06:16:18 -05:00
|
|
|
|
|
|
|
if (!module.parent) {
|
|
|
|
// Called directly
|
2021-01-26 09:08:29 -05:00
|
|
|
|
|
|
|
// handle uncaught exceptions when running in production
|
|
|
|
if (Settings.catchErrors) {
|
|
|
|
process.removeAllListeners('uncaughtException')
|
2021-07-13 07:04:48 -04:00
|
|
|
process.on('uncaughtException', error =>
|
2021-01-26 09:08:29 -05:00
|
|
|
logger.error({ err: error }, 'uncaughtException')
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-07-13 07:04:48 -04:00
|
|
|
app.listen(port, host, error => {
|
2020-06-02 04:51:34 -04:00
|
|
|
if (error) {
|
|
|
|
logger.fatal({ error }, `Error starting CLSI on ${host}:${port}`)
|
|
|
|
} else {
|
2022-05-16 08:38:18 -04:00
|
|
|
logger.debug(`CLSI starting up, listening on ${host}:${port}`)
|
2020-06-02 04:51:34 -04:00
|
|
|
}
|
|
|
|
})
|
2020-02-19 06:16:18 -05:00
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
loadTcpServer.listen(loadTcpPort, host, function (error) {
|
2020-02-19 06:16:18 -05:00
|
|
|
if (error != null) {
|
|
|
|
throw error
|
|
|
|
}
|
2022-06-06 07:41:07 -04:00
|
|
|
logger.debug(`Load tcp agent listening on load port ${loadTcpPort}`)
|
2020-02-19 06:16:18 -05:00
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
loadHttpServer.listen(loadHttpPort, host, function (error) {
|
2020-02-19 06:16:18 -05:00
|
|
|
if (error != null) {
|
|
|
|
throw error
|
|
|
|
}
|
2022-06-06 07:41:07 -04:00
|
|
|
logger.debug(`Load http agent listening on load port ${loadHttpPort}`)
|
2020-02-19 06:16:18 -05:00
|
|
|
})
|
2020-02-19 06:16:17 -05:00
|
|
|
}
|
2018-02-16 06:36:32 -05:00
|
|
|
|
2020-02-19 06:16:18 -05:00
|
|
|
module.exports = app
|