From f1c81436cc5bb1028296da0c5130ca9ea4b1bb78 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Tue, 31 May 2022 10:16:22 +0100 Subject: [PATCH] Review comments and test fix. --- packages/backend-core/logging.js | 1 + packages/backend-core/src/logging.js | 16 ++++++++++++++++ packages/server/src/app.ts | 14 +++++++++----- packages/worker/src/index.ts | 11 ++++++++--- 4 files changed, 34 insertions(+), 8 deletions(-) create mode 100644 packages/backend-core/logging.js create mode 100644 packages/backend-core/src/logging.js diff --git a/packages/backend-core/logging.js b/packages/backend-core/logging.js new file mode 100644 index 0000000000..da40fe3100 --- /dev/null +++ b/packages/backend-core/logging.js @@ -0,0 +1 @@ +module.exports = require("./src/logging") diff --git a/packages/backend-core/src/logging.js b/packages/backend-core/src/logging.js new file mode 100644 index 0000000000..425d7f8133 --- /dev/null +++ b/packages/backend-core/src/logging.js @@ -0,0 +1,16 @@ +const NonErrors = ["AccountError"] + +function isSuppressed(e) { + return e && e["suppressAlert"] +} + +module.exports.logAlert = (message, e = null) => { + if (e && NonErrors.includes(e.name) && isSuppressed(e)) { + return + } + let errorJson = "" + if (e) { + errorJson = ": " + JSON.stringify(e, Object.getOwnPropertyNames(e)) + } + console.error(`bb-alert: ${message} ${errorJson}`) +} diff --git a/packages/server/src/app.ts b/packages/server/src/app.ts index 16912754b4..f73c90c895 100644 --- a/packages/server/src/app.ts +++ b/packages/server/src/app.ts @@ -14,7 +14,7 @@ const automations = require("./automations/index") const Sentry = require("@sentry/node") const fileSystem = require("./utilities/fileSystem") const bullboard = require("./automations/bullboard") -const context = require("@budibase/backend-core/context") +const { logAlert } = require("@budibase/backend-core/logging") const { Thread } = require("./threads") import redis from "./utilities/redis" import * as migrations from "./migrations" @@ -70,7 +70,8 @@ if (env.isProd()) { const server = http.createServer(app.callback()) destroyable(server) -let shuttingDown = false +let shuttingDown = false, + errCode = 0 server.on("close", async () => { // already in process if (shuttingDown) { @@ -84,7 +85,9 @@ server.on("close", async () => { await redis.shutdown() await Thread.shutdown() api.shutdown() - process.exit() + if (!env.isTest()) { + process.exit(errCode) + } }) module.exports = server.listen(env.PORT || 0, async () => { @@ -107,7 +110,8 @@ process.on("uncaughtException", err => { if (err && err["code"] === "ERR_INVALID_CHAR") { return } - console.error(err) + errCode = -1 + logAlert("Uncaught exception.", err) shutdown() }) @@ -119,7 +123,7 @@ process.on("SIGTERM", () => { // not recommended in a clustered environment if (!env.HTTP_MIGRATIONS) { migrations.migrate().catch(err => { - console.error("Error performing migrations. Exiting.\n", err) + logAlert("Error performing migrations. Exiting.", err) shutdown() }) } diff --git a/packages/worker/src/index.ts b/packages/worker/src/index.ts index 32c4dd4ad5..fb395a7e6a 100644 --- a/packages/worker/src/index.ts +++ b/packages/worker/src/index.ts @@ -12,6 +12,7 @@ const destroyable = require("server-destroy") const koaBody = require("koa-body") const koaSession = require("koa-session") const { passport } = require("@budibase/backend-core/auth") +const { logAlert } = require("@budibase/backend-core/logging") const logger = require("koa-pino-logger") const http = require("http") const api = require("./api") @@ -61,7 +62,8 @@ if (env.isProd()) { const server = http.createServer(app.callback()) destroyable(server) -let shuttingDown = false +let shuttingDown = false, + errCode = 0 server.on("close", async () => { if (shuttingDown) { return @@ -71,7 +73,9 @@ server.on("close", async () => { console.log("Server Closed") } await redis.shutdown() - process.exit() + if (!env.isTest()) { + process.exit(errCode) + } }) const shutdown = () => { @@ -85,7 +89,8 @@ module.exports = server.listen(parseInt(env.PORT || 4002), async () => { }) process.on("uncaughtException", err => { - console.error(err) + errCode = -1 + logAlert("Uncaught exception.", err) shutdown() })