budibase/packages/worker/src/index.ts

113 lines
2.4 KiB
TypeScript
Raw Normal View History

if (process.env.DD_APM_ENABLED) {
require("./ddApm")
}
2022-08-30 11:59:27 +02:00
// need to load environment first
import env from "./environment"
2022-01-20 09:15:46 +01:00
import Application from "koa"
import { bootstrap } from "global-agent"
import * as db from "./db"
import { sdk as proSdk } from "@budibase/pro"
import {
auth,
logging,
events,
middleware,
queue,
env as coreEnv,
timers,
redis,
cache,
} from "@budibase/backend-core"
2023-11-20 21:52:29 +01:00
db.init()
import koaBody from "koa-body"
import http from "http"
import api from "./api"
2023-10-17 11:11:08 +02:00
2021-06-27 16:46:04 +02:00
const koaSession = require("koa-session")
2023-11-20 21:52:29 +01:00
2023-10-17 11:11:08 +02:00
import { userAgent } from "koa-useragent"
import destroyable from "server-destroy"
2023-03-13 14:25:19 +01:00
import { initPro } from "./initPro"
2023-03-15 12:23:55 +01:00
import { handleScimBody } from "./middleware/handleScimBody"
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
bootstrap()
2023-11-20 16:15:43 +01:00
const app: Application = new Application()
app.keys = ["secret", "key"]
2021-06-27 16:46:04 +02:00
// set up top level koa middleware
2023-03-15 12:23:55 +01:00
app.use(handleScimBody)
2023-03-15 13:34:17 +01:00
app.use(koaBody({ multipart: true }))
2023-03-15 12:23:55 +01:00
2021-06-27 16:46:04 +02:00
app.use(koaSession(app))
app.use(middleware.correlation)
app.use(middleware.pino)
app.use(userAgent)
// authentication
app.use(auth.passport.initialize())
app.use(auth.passport.session())
// api routes
app.use(api.routes())
const server = http.createServer(app.callback())
destroyable(server)
2022-05-31 11:16:22 +02:00
let shuttingDown = false,
errCode = 0
server.on("close", async () => {
if (shuttingDown) {
return
}
shuttingDown = true
2022-08-31 11:47:41 +02:00
console.log("Server Closed")
timers.cleanup()
events.shutdown()
await redis.clients.shutdown()
await queue.shutdown()
2022-05-31 11:16:22 +02:00
if (!env.isTest()) {
process.exit(errCode)
}
})
const shutdown = () => {
server.close()
server.destroy()
}
export default server.listen(parseInt(env.PORT || "4002"), async () => {
console.log(`Worker running on ${JSON.stringify(server.address())}`)
2023-03-13 14:25:19 +01:00
await initPro()
2023-11-17 17:37:00 +01:00
await redis.clients.init()
cache.docWritethrough.init()
// configure events to use the pro audit log write
// can't integrate directly into backend-core due to cyclic issues
await events.processors.init(proSdk.auditLogs.write)
})
process.on("uncaughtException", err => {
2022-05-31 11:16:22 +02:00
errCode = -1
logging.logAlert("Uncaught exception.", err)
shutdown()
})
process.on("SIGTERM", () => {
shutdown()
})
process.on("SIGINT", () => {
shutdown()
})