allow alternate scripts to run in docker, fix performance issue with workers

This commit is contained in:
Grant 2024-07-10 10:16:54 -06:00
parent a7a9b35da6
commit 93dc27b17a
5 changed files with 29 additions and 12 deletions

View File

@ -109,5 +109,7 @@ ENV SERVE_CLIENT /home/node/app/packages/client
ENV SERVE_ADMIN /home/node/app/packages/admin ENV SERVE_ADMIN /home/node/app/packages/admin
EXPOSE 3000 EXPOSE 3000
# profiler port, only used if profiler is explicity running
EXPOSE 9229
ENTRYPOINT [ "/bin/sh" ] ENTRYPOINT [ "/bin/sh" ]
CMD [ "./docker-start.sh" ] CMD [ "./docker-start.sh" ]

View File

@ -9,9 +9,11 @@ services:
build: . build: .
ports: ports:
- "3000:3000" - "3000:3000"
# - "9229:9229"
environment: environment:
- REDIS_HOST=redis://redis - REDIS_HOST=redis://redis
- DATABASE_URL=postgres://postgres@postgres/canvas - DATABASE_URL=postgres://postgres@postgres/canvas
# - SCRIPT_TO_RUN=profiler
env_file: env_file:
- .env.local - .env.local
depends_on: depends_on:

View File

@ -3,5 +3,8 @@
# This script runs when the docker image starts # This script runs when the docker image starts
# It just forces all migrations to run and then starts lol # It just forces all migrations to run and then starts lol
# Allow running of other scripts via environment variable (eg. profiler)
SCRIPT_TO_RUN="${SCRIPT_TO_RUN:-start}"
npx -w packages/server npx prisma migrate deploy npx -w packages/server npx prisma migrate deploy
npm -w packages/server run start npm -w packages/server run $SCRIPT_TO_RUN

View File

@ -4,6 +4,7 @@
"scripts": { "scripts": {
"dev": "DOTENV_CONFIG_PATH=.env.local tsx watch -r dotenv/config src/index.ts", "dev": "DOTENV_CONFIG_PATH=.env.local tsx watch -r dotenv/config src/index.ts",
"start": "node --enable-source-maps dist/index.js", "start": "node --enable-source-maps dist/index.js",
"profiler": "node --inspect=0.0.0.0:9229 --enable-source-maps dist/index.js",
"build": "tsc", "build": "tsc",
"lint": "eslint .", "lint": "eslint .",
"prisma:studio": "BROWSER=none prisma studio", "prisma:studio": "BROWSER=none prisma studio",

View File

@ -5,24 +5,33 @@ import { getLogger } from "../lib/Logger";
const Logger = getLogger("WORKER_ROOT"); const Logger = getLogger("WORKER_ROOT");
export const spawnWorker = (file: string, wkOpts: WorkerOptions = {}) => { export const spawnWorker = (file: string, wkOpts: WorkerOptions = {}) => {
if (process.env.NODE_ENV === "production") file = file.replace(".ts", ".js"); if (process.env.NODE_ENV === "production") {
// when compiled we no longer need ts-node as it's already raw JS
// replace the file extension so it can load it directly
// https://github.com/TypeStrong/ts-node/issues/676#issuecomment-531620154 file = path.join(__dirname, file.replace(".ts", ".js"));
wkOpts.eval = true; return new Worker(file, wkOpts);
if (!wkOpts.workerData) { } else {
wkOpts.workerData = {}; // when in development we just have TS files
} // this loads TS dynamically when the worker is created
wkOpts.workerData.__filename = path.join(__dirname, file);
return new Worker( // https://github.com/TypeStrong/ts-node/issues/676#issuecomment-531620154
` wkOpts.eval = true;
if (!wkOpts.workerData) {
wkOpts.workerData = {};
}
wkOpts.workerData.__filename = path.join(__dirname, file);
return new Worker(
`
const wk = require('worker_threads'); const wk = require('worker_threads');
require('ts-node').register(); require('ts-node').register();
let file = wk.workerData.__filename; let file = wk.workerData.__filename;
delete wk.workerData.__filename; delete wk.workerData.__filename;
require(file); require(file);
`, `,
wkOpts wkOpts
); );
}
}; };
const AllWorkers = { const AllWorkers = {