From 2384eb83d935d691871e653be0e2ddba4f61ff3b Mon Sep 17 00:00:00 2001 From: Henry Oswald Date: Thu, 28 Jun 2018 16:04:34 +0100 Subject: [PATCH] add load balance http endpoints to shut box down --- services/clsi/app.coffee | 32 ++++++++++++++----- services/clsi/config/settings.defaults.coffee | 9 ++++-- 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/services/clsi/app.coffee b/services/clsi/app.coffee index 11981a72aa..cf01b47943 100644 --- a/services/clsi/app.coffee +++ b/services/clsi/app.coffee @@ -174,7 +174,11 @@ os = require "os" STATE = "up" -server = net.createServer (socket) -> +process.on "SIGHUP", -> + console.log "got SIGHUP event" + STATE = "down" + +loadTcpServer = net.createServer (socket) -> socket.on "error", (err)-> if err.code == "ECONNRESET" # this always comes up, we don't know why @@ -182,7 +186,7 @@ server = net.createServer (socket) -> logger.err err:err, "error with socket on load check" socket.destroy() - if STATE == "up" and Settings.load_balancer_agent.report_load + if STATE == "up" and Settings.internal.load_balancer_agent.report_load currentLoad = os.loadavg()[0] # staging clis's have 1 cpu core only @@ -201,26 +205,38 @@ server = net.createServer (socket) -> socket.write("#{STATE}\n", "ASCII") socket.end() +loadHttpServer = express() +loadHttpServer.post "/state/up", (req, res, next) -> + STATE = "up" + logger.info "getting message to set server to down" + res.sendStatus 204 +loadHttpServer.post "/state/down", (req, res, next) -> + STATE = "down" + logger.info "getting message to set server to down" + res.sendStatus 204 port = (Settings.internal?.clsi?.port or 3013) host = (Settings.internal?.clsi?.host or "localhost") -load_port = Settings.internal.clsi.load_port or 3048 - +load_tcp_port = Settings.internal.load_balancer_agent.load_port +load_http_port = Settings.internal.load_balancer_agent.local_port if !module.parent # Called directly app.listen port, host, (error) -> logger.info "CLSI starting up, listening on #{host}:#{port}" - server.listen load_port, host, (error) -> + loadTcpServer.listen load_tcp_port, host, (error) -> throw error if error? - logger.info "Load agent listening on load port #{load_port}" + logger.info "Load tcp agent listening on load port #{load_tcp_port}" + + loadHttpServer.listen load_http_port, host, (error) -> + throw error if error? + logger.info "Load http agent listening on load port #{load_http_port}" + module.exports = app - - setInterval () -> ProjectPersistenceManager.clearExpiredProjects() , tenMinutes = 10 * 60 * 1000 diff --git a/services/clsi/config/settings.defaults.coffee b/services/clsi/config/settings.defaults.coffee index da4aa82c17..a64941f866 100644 --- a/services/clsi/config/settings.defaults.coffee +++ b/services/clsi/config/settings.defaults.coffee @@ -20,7 +20,11 @@ module.exports = clsi: port: 3013 host: process.env["LISTEN_ADDRESS"] or "localhost" - + + load_balancer_agent: + report_load:true + load_port: 3048 + local_port: 3049 apis: clsi: url: "http://#{process.env['CLSI_HOST'] or 'localhost'}:3013" @@ -29,6 +33,7 @@ module.exports = project_cache_length_ms: 1000 * 60 * 60 * 24 parallelFileDownloads:1 + if process.env["DOCKER_RUNNER"] module.exports.clsi = dockerRunner: process.env["DOCKER_RUNNER"] == "true" @@ -52,5 +57,3 @@ if process.env["DOCKER_RUNNER"] module.exports.path.sandboxedCompilesHostDir = process.env["COMPILES_HOST_DIR"] module.exports.path.synctexBinHostPath = process.env["SYNCTEX_BIN_HOST_PATH"] -console.log "configggggg" -console.log module.exports