Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Budi 8841 implement lame ducking on app and worker pods #15024

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions charts/budibase/templates/app-service-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ spec:
{{- toYaml .Values.services.apps.templateLabels | indent 8 -}}
{{ end }}
spec:
terminationGracePeriodSeconds: 60
containers:
- env:
- name: BUDIBASE_ENVIRONMENT
Expand Down
1 change: 1 addition & 0 deletions charts/budibase/templates/worker-service-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ spec:
{{- toYaml .Values.services.worker.templateLabels | indent 8 -}}
{{ end }}
spec:
terminationGracePeriodSeconds: 60
containers:
- env:
- name: BUDIBASE_ENVIRONMENT
Expand Down
1 change: 1 addition & 0 deletions packages/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
"form-data": "4.0.0",
"global-agent": "3.0.0",
"google-spreadsheet": "npm:@budibase/google-spreadsheet@4.1.5",
"http-graceful-shutdown": "^3.1.13",
"ioredis": "5.3.2",
"isolated-vm": "^4.7.2",
"jimp": "0.22.12",
Expand Down
58 changes: 22 additions & 36 deletions packages/server/src/koa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ import {
timers,
env as coreEnv,
} from "@budibase/backend-core"
import destroyable from "server-destroy"
import { userAgent } from "koa-useragent"
import destroyable from "server-destroy"
import gracefulShutdown from "http-graceful-shutdown"

export default function createKoaApp() {
const app = new Koa()
Expand Down Expand Up @@ -50,35 +51,27 @@ export default function createKoaApp() {

const server = http.createServer(app.callback())
destroyable(server)

let shuttingDown = false,
errCode = 0

server.on("close", async () => {
// already in process
if (shuttingDown) {
return
}
shuttingDown = true
console.log("Server Closed")
timers.cleanup()
await automations.shutdown()
await redis.shutdown()
events.shutdown()
await Thread.shutdown()
api.shutdown()
if (!env.isTest()) {
process.exit(errCode)
}
})
// let shuttingDown = false
let errCode = 0

const listener = server.listen(env.PORT || 0)

const shutdown = () => {
server.close()
// @ts-ignore
server.destroy()
}
gracefulShutdown(server, {
onShutdown: async () => {
console.log("Server is shutting down gracefully...")
timers.cleanup()
await automations.shutdown()
await redis.shutdown()
events.shutdown()
await Thread.shutdown()
api.shutdown()
},
finally: () => {
if (!env.isTest()) {
process.exit(errCode)
}
},
})

process.on("uncaughtException", err => {
// @ts-ignore
Expand All @@ -88,15 +81,8 @@ export default function createKoaApp() {
}
errCode = -1
logging.logAlert("Uncaught exception.", err)
shutdown()
})

process.on("SIGTERM", () => {
shutdown()
})

process.on("SIGINT", () => {
shutdown()
// Trigger graceful shutdown
process.kill(process.pid, "SIGTERM")
})

return { app, server: listener }
Expand Down
56 changes: 24 additions & 32 deletions packages/worker/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,18 @@ const koaSession = require("koa-session")

import { userAgent } from "koa-useragent"

import destroyable from "server-destroy"
import { initPro } from "./initPro"
import { handleScimBody } from "./middleware/handleScimBody"
import destroyable from "server-destroy"
import gracefulShutdown from "http-graceful-shutdown"

if (coreEnv.ENABLE_SSO_MAINTENANCE_MODE) {
console.warn(
"Warning: ENABLE_SSO_MAINTENANCE_MODE is set. It is recommended this flag is disabled if maintenance is not in progress"
)
}

// this will setup http and https proxies form env variables
// this will setup http and https proxies from env variables
bootstrap()

const app: Application = new Application()
Expand Down Expand Up @@ -71,28 +72,9 @@ app.use(api.routes())
const server = http.createServer(app.callback())
destroyable(server)

let shuttingDown = false,
errCode = 0
server.on("close", async () => {
if (shuttingDown) {
return
}
shuttingDown = true
console.log("Server Closed")
timers.cleanup()
events.shutdown()
await redis.clients.shutdown()
await queue.shutdown()
if (!env.isTest()) {
process.exit(errCode)
}
})

const shutdown = () => {
server.close()
server.destroy()
}
let errCode = 0

// Start the server
export default server.listen(parseInt(env.PORT || "4002"), async () => {
let startupLog = `Worker running on ${JSON.stringify(server.address())}`
if (env.BUDIBASE_ENVIRONMENT) {
Expand All @@ -108,16 +90,26 @@ export default server.listen(parseInt(env.PORT || "4002"), async () => {
await events.processors.init(proSdk.auditLogs.write)
})

gracefulShutdown(server, {
signals: "SIGINT SIGTERM",
timeout: 30000, // optional: timeout in ms for the shutdown process to finish
onShutdown: async () => {
console.log("Server is shutting down gracefully...")
timers.cleanup()
events.shutdown()
await redis.clients.shutdown()
await queue.shutdown()
},
finally: () => {
if (!env.isTest()) {
process.exit(errCode)
}
},
})

process.on("uncaughtException", err => {
errCode = -1
logging.logAlert("Uncaught exception.", err)
shutdown()
})

process.on("SIGTERM", () => {
shutdown()
})

process.on("SIGINT", () => {
shutdown()
// Trigger graceful shutdown
process.kill(process.pid, "SIGTERM")
})
Loading
Loading