-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add graceful shutdown of API when receiving SIGTERM
- Loading branch information
1 parent
0f35274
commit 534991a
Showing
12 changed files
with
207 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import express from 'express'; | ||
import morgan from 'morgan'; | ||
import compression from 'compression'; | ||
import cors from 'cors'; | ||
import { json } from 'body-parser'; | ||
import { logger } from './loggers/logger'; | ||
import { createRouter } from './routes'; | ||
import { authMiddleware } from './authMiddleware'; | ||
import apolloServer from './apolloServer'; | ||
|
||
export const app = express(); | ||
|
||
// Use compression (gzip) for responses. | ||
app.use(compression()); | ||
|
||
// Automatically decode json. | ||
app.use(json()); | ||
|
||
// Add custom configured logger (morgan through winston). | ||
app.use( | ||
morgan('combined', { | ||
stream: { | ||
write: message => logger.info(message) | ||
} | ||
}) | ||
); | ||
|
||
// TODO: Restrict requests to lagoon domains? | ||
app.use(cors()); | ||
|
||
app.use(authMiddleware); | ||
|
||
// Add routes. | ||
app.use('/', createRouter()); | ||
|
||
apolloServer.applyMiddleware({ app }); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import 'newrelic'; | ||
import { Server } from 'http'; | ||
import { promisify } from 'util'; | ||
import { initSendToLagoonLogs } from '@lagoon/commons/dist/logs'; | ||
import { initSendToLagoonTasks } from '@lagoon/commons/dist/tasks'; | ||
import { waitForKeycloak } from './util/waitForKeycloak'; | ||
import { envHasConfig } from './util/config'; | ||
import { logger } from './loggers/logger'; | ||
import { createServer } from './server'; | ||
|
||
initSendToLagoonLogs(); | ||
initSendToLagoonTasks(); | ||
|
||
const makeGracefulShutdown = (server: Server) => { | ||
return async (signal: NodeJS.Signals) => { | ||
logger.debug(`${signal}: API Shutting Down`); | ||
|
||
logger.debug('Closing sqlClientPool'); | ||
const { sqlClientPool } = await import('./clients/sqlClient'); | ||
await sqlClientPool.end(); | ||
|
||
process.kill(process.pid, signal); | ||
}; | ||
}; | ||
|
||
(async () => { | ||
await waitForKeycloak(); | ||
|
||
logger.debug('Starting to boot the application.'); | ||
|
||
try { | ||
if (!envHasConfig('JWTSECRET')) { | ||
throw new Error( | ||
'Required environment variable JWTSECRET is undefined or null!' | ||
); | ||
} | ||
|
||
if (!envHasConfig('JWTAUDIENCE')) { | ||
throw new Error( | ||
'Required environment variable JWTAUDIENCE is undefined or null!' | ||
); | ||
} | ||
|
||
const server = await createServer(); | ||
const gracefulShutdown = makeGracefulShutdown(server); | ||
|
||
// Shutdown on tsc-watch restart, docker-compose restart/kill, and k8s pod kills | ||
process.once('SIGTERM', gracefulShutdown); | ||
// Shutdown on ctrl-c | ||
process.once('SIGINT', gracefulShutdown); | ||
|
||
logger.debug('Finished booting the application.'); | ||
} catch (e) { | ||
logger.error('Error occurred while starting the application'); | ||
logger.error(e.stack); | ||
} | ||
})(); |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import http from 'http'; | ||
import util from 'util'; | ||
import { logger } from './loggers/logger'; | ||
import { toNumber } from './util/func'; | ||
import { getConfigFromEnv } from './util/config'; | ||
import { app } from './app'; | ||
import apolloServer from './apolloServer'; | ||
|
||
export const createServer = async () => { | ||
logger.debug('Starting to boot the server.'); | ||
|
||
const port = toNumber(getConfigFromEnv('PORT', '3000')); | ||
const server = http.createServer(app); | ||
server.setTimeout(900000); // higher Server timeout: 15min instead of default 2min | ||
|
||
apolloServer.installSubscriptionHandlers(server); | ||
|
||
const listen = util.promisify(server.listen).bind(server); | ||
await listen(port); | ||
|
||
logger.debug( | ||
`Finished booting the server. The server is reachable at port ${port.toString()}.` | ||
); | ||
|
||
return server; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.