From 9d813292de00b6c08c3d293accf17b110448f7ea Mon Sep 17 00:00:00 2001 From: Martin McKeaveney Date: Mon, 23 May 2022 16:24:29 +0100 Subject: [PATCH] enable clustering on server and worker services, better log output on user not found errors --- packages/backend-core/src/auth.js | 2 +- packages/backend-core/src/middleware/passport/local.js | 2 +- packages/builder/src/pages/builder/auth/login.svelte | 2 +- packages/server/Dockerfile | 4 +++- packages/server/docker_run.sh | 5 +++++ packages/server/package.json | 1 + packages/server/pm2.js | 9 +++++++++ packages/worker/Dockerfile | 4 +++- packages/worker/docker_run.sh | 5 +++++ packages/worker/package.json | 1 + packages/worker/pm2.js | 9 +++++++++ packages/worker/src/api/controllers/global/configs.js | 4 +--- packages/worker/src/environment.js | 7 +++++++ 13 files changed, 47 insertions(+), 8 deletions(-) create mode 100755 packages/server/docker_run.sh create mode 100644 packages/server/pm2.js create mode 100755 packages/worker/docker_run.sh create mode 100644 packages/worker/pm2.js diff --git a/packages/backend-core/src/auth.js b/packages/backend-core/src/auth.js index f6d53522d5..b13cd932c6 100644 --- a/packages/backend-core/src/auth.js +++ b/packages/backend-core/src/auth.js @@ -29,7 +29,7 @@ passport.deserializeUser(async (user, done) => { const user = await db.get(user._id) return done(null, user) } catch (err) { - console.error("User not found", err) + console.error(`User not found`, err) return done(null, false, { message: "User not found" }) } }) diff --git a/packages/backend-core/src/middleware/passport/local.js b/packages/backend-core/src/middleware/passport/local.js index 2149bd3e18..716ebc1755 100644 --- a/packages/backend-core/src/middleware/passport/local.js +++ b/packages/backend-core/src/middleware/passport/local.js @@ -30,7 +30,7 @@ exports.authenticate = async function (ctx, email, password, done) { const dbUser = await getGlobalUserByEmail(email) if (dbUser == null) { - return authError(done, "User not found") + return authError(done, `User not found: [${email}]`) } // check that the user is currently inactive, if this is the case throw invalid diff --git a/packages/builder/src/pages/builder/auth/login.svelte b/packages/builder/src/pages/builder/auth/login.svelte index 84af5b6338..c2ec547e38 100644 --- a/packages/builder/src/pages/builder/auth/login.svelte +++ b/packages/builder/src/pages/builder/auth/login.svelte @@ -28,7 +28,7 @@ async function login() { try { await auth.login({ - username, + username: username.trim(), password, }) if ($auth?.user?.forceResetPassword) { diff --git a/packages/server/Dockerfile b/packages/server/Dockerfile index 0ef0fe4244..e5063a88ed 100644 --- a/packages/server/Dockerfile +++ b/packages/server/Dockerfile @@ -16,6 +16,7 @@ ENV BUDIBASE_ENVIRONMENT=PRODUCTION # copy files and install dependencies COPY . ./ RUN yarn +RUN yarn global add pm2 RUN yarn build # Install client for oracle datasource @@ -28,4 +29,5 @@ EXPOSE 4001 # due to this causing yarn to stop installing dev dependencies # which are actually needed to get this environment up and running ENV NODE_ENV=production -CMD ["yarn", "run:docker"] +ENV CLUSTER_MODE=${CLUSTER_MODE} +CMD ["./docker_run.sh"] diff --git a/packages/server/docker_run.sh b/packages/server/docker_run.sh new file mode 100755 index 0000000000..0045fe0c44 --- /dev/null +++ b/packages/server/docker_run.sh @@ -0,0 +1,5 @@ +if [ -z $CLUSTER_MODE ]; then + yarn run:docker +else + yarn run:docker:cluster +fi diff --git a/packages/server/package.json b/packages/server/package.json index f428ad5328..8ebad9923b 100644 --- a/packages/server/package.json +++ b/packages/server/package.json @@ -18,6 +18,7 @@ "build:docker": "yarn run predocker && docker build . -t app-service --label version=$BUDIBASE_RELEASE_VERSION", "build:docs": "node ./scripts/docs/generate.js open", "run:docker": "node dist/index.js", + "run:docker:cluster": "pm2-runtime start pm2.js", "dev:stack:up": "node scripts/dev/manage.js up", "dev:stack:down": "node scripts/dev/manage.js down", "dev:stack:nuke": "node scripts/dev/manage.js nuke", diff --git a/packages/server/pm2.js b/packages/server/pm2.js new file mode 100644 index 0000000000..33829f327c --- /dev/null +++ b/packages/server/pm2.js @@ -0,0 +1,9 @@ +module.exports = { + apps: [ + { + script: "dist/index.js", + instances: "max", + exec_mode: "cluster", + }, + ], +} diff --git a/packages/worker/Dockerfile b/packages/worker/Dockerfile index 614a724ab6..b73efcc4a1 100644 --- a/packages/worker/Dockerfile +++ b/packages/worker/Dockerfile @@ -10,6 +10,7 @@ WORKDIR /app # copy files and install dependencies COPY . ./ RUN yarn +RUN yarn global add pm2 EXPOSE 4001 @@ -17,4 +18,5 @@ EXPOSE 4001 # due to this causing yarn to stop installing dev dependencies # which are actually needed to get this environment up and running ENV NODE_ENV=production -CMD ["yarn", "run:docker"] +ENV CLUSTER_MODE=${CLUSTER_MODE} +CMD ["./docker_run.sh"] diff --git a/packages/worker/docker_run.sh b/packages/worker/docker_run.sh new file mode 100755 index 0000000000..20694e5df0 --- /dev/null +++ b/packages/worker/docker_run.sh @@ -0,0 +1,5 @@ +if [[ -z $CLUSTER_MODE ]]; then + yarn run:docker +else + yarn run:docker:cluster +fi diff --git a/packages/worker/package.json b/packages/worker/package.json index 885f55003e..32b7610d8c 100644 --- a/packages/worker/package.json +++ b/packages/worker/package.json @@ -15,6 +15,7 @@ "build": "rimraf dist/ && tsc", "postbuild": "copyfiles -u 1 src/**/*.hbs dist/", "run:docker": "node dist/index.js", + "run:docker:cluster": "pm2-runtime start pm2.js", "build:docker": "docker build . -t worker-service --label version=$BUDIBASE_RELEASE_VERSION", "dev:stack:init": "node ./scripts/dev/manage.js init", "dev:builder": "npm run dev:stack:init && nodemon", diff --git a/packages/worker/pm2.js b/packages/worker/pm2.js new file mode 100644 index 0000000000..33829f327c --- /dev/null +++ b/packages/worker/pm2.js @@ -0,0 +1,9 @@ +module.exports = { + apps: [ + { + script: "dist/index.js", + instances: "max", + exec_mode: "cluster", + }, + ], +} diff --git a/packages/worker/src/api/controllers/global/configs.js b/packages/worker/src/api/controllers/global/configs.js index 571e102e4c..0301ec354f 100644 --- a/packages/worker/src/api/controllers/global/configs.js +++ b/packages/worker/src/api/controllers/global/configs.js @@ -250,11 +250,9 @@ exports.configChecklist = async function (ctx) { const tenantId = getTenantId() try { - const ONE_MINUTE = 600 - ctx.body = await withCache( `checklist:${tenantId}`, - ONE_MINUTE, + env.CHECKLIST_CACHE_TTL, async () => { let apps = [] if (!env.MULTI_TENANCY || tenantId) { diff --git a/packages/worker/src/environment.js b/packages/worker/src/environment.js index 8ef12e3877..790448d7e8 100644 --- a/packages/worker/src/environment.js +++ b/packages/worker/src/environment.js @@ -20,6 +20,12 @@ if (!LOADED && isDev() && !isTest()) { LOADED = true } +function parseIntSafe(number) { + if (number) { + return parseInt(number) + } +} + module.exports = { NODE_ENV: process.env.NODE_ENV, SELF_HOSTED: !!parseInt(process.env.SELF_HOSTED), @@ -46,6 +52,7 @@ module.exports = { SMTP_FROM_ADDRESS: process.env.SMTP_FROM_ADDRESS, PLATFORM_URL: process.env.PLATFORM_URL, COOKIE_DOMAIN: process.env.COOKIE_DOMAIN, + CHECKLIST_CACHE_TTL: parseIntSafe(process.env.CHECKLIST_CACHE_TTL) || 600, APPS_URL: process.env.APPS_URL, _set(key, value) { process.env[key] = value